1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
import { useLanguageProfiles } from "@/apis/hooks";
import { MultiSelector, Selector } from "@/components/inputs";
import { useModals, withModal } from "@/modules/modals";
import { GetItemId, useSelectorOptions } from "@/utilities";
import { Button, Divider, Group, LoadingOverlay, Stack } from "@mantine/core";
import { useForm } from "@mantine/form";
import { FunctionComponent, useMemo } from "react";
import { UseMutationResult } from "react-query";
interface Props {
mutation: UseMutationResult<void, unknown, FormType.ModifyItem, unknown>;
item: Item.Base | null;
onComplete?: () => void;
onCancel?: () => void;
}
const ItemEditForm: FunctionComponent<Props> = ({
mutation,
item,
onComplete,
onCancel,
}) => {
const { data, isFetching } = useLanguageProfiles();
const { isLoading, mutate } = mutation;
const modals = useModals();
const profileOptions = useSelectorOptions(
data ?? [],
(v) => v.name ?? "Unknown",
(v) => v.profileId.toString() ?? "-1",
);
const profile = useMemo(
() => data?.find((v) => v.profileId === item?.profileId) ?? null,
[data, item?.profileId],
);
const form = useForm({
initialValues: {
profile: profile ?? null,
},
});
const options = useSelectorOptions(
item?.audio_language ?? [],
(v) => v.name,
(v) => v.code2,
);
const isOverlayVisible = isLoading || isFetching || item === null;
return (
<form
onSubmit={form.onSubmit(({ profile }) => {
if (item) {
const itemId = GetItemId(item);
if (itemId) {
mutate({ id: [itemId], profileid: [profile?.profileId ?? null] });
onComplete?.();
modals.closeSelf();
return;
}
}
form.setErrors({ profile: "Invalid profile" });
})}
>
<LoadingOverlay visible={isOverlayVisible}></LoadingOverlay>
<Stack>
<MultiSelector
label="Audio Languages"
disabled
{...options}
value={item?.audio_language ?? []}
></MultiSelector>
<Selector
{...profileOptions}
{...form.getInputProps("profile")}
clearable
label="Languages Profile"
></Selector>
<Divider></Divider>
<Group position="right">
<Button
disabled={isOverlayVisible}
onClick={() => {
onCancel?.();
modals.closeSelf();
}}
color="gray"
variant="subtle"
>
Cancel
</Button>
<Button disabled={isOverlayVisible} type="submit">
Save
</Button>
</Group>
</Stack>
</form>
);
};
export const ItemEditModal = withModal(ItemEditForm, "item-editor", {
title: "Editor",
size: "md",
});
export default ItemEditForm;
|