2025-01-13 09:50:23 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogTrigger,
|
|
|
|
|
} from "@/components/ui/dialog";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
|
import { useRouter } from "@/i18n/routing";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
getUserRoles,
|
|
|
|
|
postCategory,
|
|
|
|
|
postDataFAQ,
|
|
|
|
|
} from "@/service/settings/settings";
|
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
|
|
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
|
|
|
import { close, error, loading } from "@/config/swal";
|
|
|
|
|
import { useToast } from "@/components/ui/use-toast";
|
|
|
|
|
import { stringify } from "querystring";
|
|
|
|
|
|
|
|
|
|
const FormSchema = z.object({
|
|
|
|
|
answer: z.string({
|
|
|
|
|
required_error: "Required",
|
|
|
|
|
}),
|
|
|
|
|
question: z.string({
|
|
|
|
|
required_error: "Required",
|
|
|
|
|
}),
|
|
|
|
|
publishTo: z.string({
|
|
|
|
|
required_error: "Required",
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
// publishTo: z.array(z.string()).refine((value) => value.some((item) => item), {
|
|
|
|
|
// message: "Required",
|
|
|
|
|
// }),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const publishToList = [
|
|
|
|
|
{
|
|
|
|
|
id: "mabes",
|
|
|
|
|
name: "Nasional",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "polda",
|
|
|
|
|
name: "Polda",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "satker",
|
|
|
|
|
name: "Satker",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "internasional",
|
|
|
|
|
name: "Internasional",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export default function CreateSpvFAQModal() {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const { toast } = useToast();
|
|
|
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof FormSchema>>({
|
|
|
|
|
resolver: zodResolver(FormSchema),
|
|
|
|
|
defaultValues: { publishTo: "wilayah" },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const target = form.watch("publishTo");
|
2025-07-24 07:37:12 +00:00
|
|
|
const isAllTargetChecked = target && publishToList.every((item) =>
|
|
|
|
|
target.includes(item.id)
|
2025-01-13 09:50:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (data: z.infer<typeof FormSchema>) => {
|
|
|
|
|
const request = {
|
|
|
|
|
question: data.question,
|
|
|
|
|
answer: data.answer,
|
|
|
|
|
isInternational: data.publishTo === "wilayah" ? false : true,
|
|
|
|
|
isActive: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const response = await postDataFAQ(request);
|
|
|
|
|
close();
|
|
|
|
|
if (response?.error) {
|
|
|
|
|
toast({ title: stringify(response.message), variant: "destructive" });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
toast({
|
|
|
|
|
title: "Succes",
|
|
|
|
|
description: "FAQ berhasil dibuat",
|
|
|
|
|
});
|
|
|
|
|
router.push("/supervisor/faq?dataChange=true");
|
|
|
|
|
setIsOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
<Button color="primary" size="md">
|
|
|
|
|
Tambah FAQ
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
<DialogContent size="md">
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Tambah FAQ</DialogTitle>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
|
|
|
className="space-y-3 bg-white rounded-sm"
|
|
|
|
|
>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="publishTo"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="space-y-3">
|
|
|
|
|
<FormLabel>Wilayah Publish</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<RadioGroup
|
|
|
|
|
onValueChange={field.onChange}
|
|
|
|
|
value={field.value}
|
|
|
|
|
className="flex flex-row space-x-1"
|
|
|
|
|
>
|
|
|
|
|
<FormItem className="flex items-center space-x-3 space-y-0">
|
|
|
|
|
<FormControl>
|
|
|
|
|
<RadioGroupItem value="wilayah" />
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormLabel className="font-normal">Wilayah</FormLabel>
|
|
|
|
|
</FormItem>
|
|
|
|
|
<FormItem className="flex items-center space-x-3 space-y-0">
|
|
|
|
|
<FormControl>
|
|
|
|
|
<RadioGroupItem value="international" />
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormLabel className="font-normal">
|
|
|
|
|
Internasional
|
|
|
|
|
</FormLabel>
|
|
|
|
|
</FormItem>
|
|
|
|
|
</RadioGroup>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="question"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Pertanyaan</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Textarea placeholder="Masukkan pertanyaan" {...field} />
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="answer"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Jawaban</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Textarea placeholder="Masukkan jawaban" {...field} />
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
color="primary"
|
|
|
|
|
size="md"
|
|
|
|
|
className="text-xs"
|
|
|
|
|
>
|
|
|
|
|
Tambah FAQ
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|