"use client"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, useForm } from "react-hook-form"; import { z } from "zod"; import { useEffect, useState } from "react"; import { getUserLevelForAssignments } from "@/service/task"; import { FormControl, FormField, FormItem, FormMessage, } from "@/components/ui/form"; const FormSchema = z.object({ items: z.array(z.string()).refine((value) => value.some((item) => item), { message: "Required", }), }); interface UnitType { id: number; name: string; subDestination: { id: number; name: string }[] | null; } export function UnitMapping(props: { unit: "Polda" | "Satker" | "Polres"; sendDataToParent: (data: string[]) => void; isDetail: boolean; initData?: string[]; }) { const { unit, sendDataToParent, isDetail } = props; const [unitList, setUnitList] = useState([]); const [satkerList, setSatkerList] = useState<{ id: number; name: string }[]>( [] ); const [polresList, setPolresList] = useState<{ id: number; name: string }[]>( [] ); const [isOpen, setIsOpen] = useState(false); const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { items: props.initData ? props.initData : [], }, }); useEffect(() => { async function initState() { const response = await getUserLevelForAssignments(); setupUnit(response?.data?.data.list); console.log("list", response?.data?.data.list); } initState(); }, []); const unitType = form.watch("items"); const isAllUnitChecked = unitType && unitList.every((item) => unitType.includes(String(item.id))); const isAllSatkerChecked = unitType && satkerList.every((item) => unitType.includes(String(item.id))); const isAllPolresChecked = polresList.every((item) => unitType?.includes(String(item.id)) ); const setupUnit = (data: UnitType[]) => { const temp = data.filter((a) => a.name.includes("POLDA")); const temp2 = data.filter((a) => a.name.includes("SATKER")); const temp3 = temp.flatMap((item) => item.subDestination || []); setUnitList(temp); setSatkerList(temp2[0]?.subDestination || []); setPolresList(temp3); }; useEffect(() => { sendDataToParent(form.getValues("items")); }, [unitType]); return ( setIsOpen(true)} className="text-primary cursor-pointer text-xs mr-3" > Pilih {unit} {unit}
{ if (checked) { form.setValue( "items", unit === "Polda" ? unitList.map((item) => String(item.id)) : unit === "Satker" ? satkerList.map((item) => String(item.id)) : polresList.map((item) => String(item.id)) ); } else { form.setValue("items", []); } }} />
( {(unit === "Polda" ? unitList : unit === "Satker" ? satkerList : polresList )?.map((item: any) => ( { return ( { return checked ? field.onChange([ ...field.value, String(item.id), ]) : field.onChange( field.value?.filter( (value) => value !== String(item.id) ) ); }} />

{item.name}

); }} /> ))}
)} />
); }