gitscope
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/services/dialogs/mutation/index.tsx · v0.3.0
Source
1 3072f60 Mrdn 2y ago import {
2 3072f60 Mrdn 2y ago DialogTitle,
3 3072f60 Mrdn 2y ago DialogContent,
4 3072f60 Mrdn 2y ago Dialog,
5 3072f60 Mrdn 2y ago DialogHeader,
6 3072f60 Mrdn 2y ago Form,
7 3072f60 Mrdn 2y ago Button,
8 3072f60 Mrdn 2y ago } from "@marzneshin/components";
9 3072f60 Mrdn 2y ago import type { FC } from "react";
10 3072f60 Mrdn 2y ago import {
11 3072f60 Mrdn 2y ago type ServiceType,
12 3072f60 Mrdn 2y ago useServicesCreationMutation,
13 3072f60 Mrdn 2y ago useServicesUpdateMutation,
14 3072f60 Mrdn 2y ago } from "@marzneshin/features/services";
15 3072f60 Mrdn 2y ago import { useTranslation } from "react-i18next";
16 3072f60 Mrdn 2y ago import { z } from "zod";
17 3072f60 Mrdn 2y ago import { useMutationDialog } from "@marzneshin/hooks";
18 3072f60 Mrdn 2y ago import { NameField, InboundsField } from "./fields";
19 3072f60 Mrdn 2y ago
20 3072f60 Mrdn 2y ago export const ServiceSchema = z.object({
21 3072f60 Mrdn 2y ago id: z.number().optional(),
22 3072f60 Mrdn 2y ago inbounds: z.array(z.number()).refine((value) => value.some((item) => item), {
23 3072f60 Mrdn 2y ago message: "You have to select at least one inbound.",
24 3072f60 Mrdn 2y ago }),
25 3072f60 Mrdn 2y ago name: z.string().trim().min(1),
26 3072f60 Mrdn 2y ago });
27 3072f60 Mrdn 2y ago type ServiceCreateType = z.infer<typeof ServiceSchema>;
28 3072f60 Mrdn 2y ago
29 3072f60 Mrdn 2y ago interface MutationDialogProps {
30 3072f60 Mrdn 2y ago entity: ServiceType | null;
31 3072f60 Mrdn 2y ago open: boolean;
32 3072f60 Mrdn 2y ago onOpenChange: (state: boolean) => void;
33 3072f60 Mrdn 2y ago }
34 3072f60 Mrdn 2y ago
35 3072f60 Mrdn 2y ago const getDefaultValues = (): ServiceCreateType => ({
36 3072f60 Mrdn 2y ago name: "",
37 3072f60 Mrdn 2y ago inbounds: [],
38 3072f60 Mrdn 2y ago });
39 3072f60 Mrdn 2y ago
40 3072f60 Mrdn 2y ago export const MutationDialog: FC<MutationDialogProps> = ({
41 3072f60 Mrdn 2y ago entity,
42 3072f60 Mrdn 2y ago open,
43 3072f60 Mrdn 2y ago onOpenChange,
44 3072f60 Mrdn 2y ago }) => {
45 3072f60 Mrdn 2y ago const isEditing = entity !== null;
46 3072f60 Mrdn 2y ago const updateMutation = useServicesUpdateMutation();
47 3072f60 Mrdn 2y ago const createMutation = useServicesCreationMutation();
48 3072f60 Mrdn 2y ago const { form, handleSubmit } = useMutationDialog({
49 3072f60 Mrdn 2y ago entity,
50 3072f60 Mrdn 2y ago updateMutation,
51 3072f60 Mrdn 2y ago createMutation,
52 3072f60 Mrdn 2y ago getDefaultValue: getDefaultValues,
53 3072f60 Mrdn 2y ago schema: ServiceSchema,
54 3072f60 Mrdn 2y ago onOpenChange,
55 3072f60 Mrdn 2y ago });
56 3072f60 Mrdn 2y ago const { t } = useTranslation();
57 3072f60 Mrdn 2y ago return (
58 3072f60 Mrdn 2y ago <Dialog open={open} onOpenChange={onOpenChange} defaultOpen={true}>
59 3072f60 Mrdn 2y ago <DialogContent>
60 3072f60 Mrdn 2y ago <DialogHeader>
61 3072f60 Mrdn 2y ago <DialogTitle className="text-primary">
62 3072f60 Mrdn 2y ago {isEditing
63 3072f60 Mrdn 2y ago ? t("page.services.dialogs.edition.title")
64 3072f60 Mrdn 2y ago : t("page.services.dialogs.creation.title")}
65 3072f60 Mrdn 2y ago </DialogTitle>
66 3072f60 Mrdn 2y ago </DialogHeader>
67 3072f60 Mrdn 2y ago <Form {...form}>
68 3072f60 Mrdn 2y ago <form onSubmit={handleSubmit} className="h-full">
69 3072f60 Mrdn 2y ago <NameField />
70 3072f60 Mrdn 2y ago <InboundsField />
71 3072f60 Mrdn 2y ago <Button
72 3072f60 Mrdn 2y ago className="mt-3 w-full font-semibold"
73 3072f60 Mrdn 2y ago type="submit"
74 3072f60 Mrdn 2y ago disabled={form.formState.isSubmitting}
75 3072f60 Mrdn 2y ago >
76 3072f60 Mrdn 2y ago {t("submit")}
77 3072f60 Mrdn 2y ago </Button>
78 3072f60 Mrdn 2y ago </form>
79 3072f60 Mrdn 2y ago </Form>
80 3072f60 Mrdn 2y ago </DialogContent>
81 3072f60 Mrdn 2y ago </Dialog>
82 3072f60 Mrdn 2y ago );
83 3072f60 Mrdn 2y ago };