From 980a5674e2d2aa55f55a2b7d29a7f0d37e531035 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Fri, 6 Feb 2026 09:45:07 +0700 Subject: [PATCH] feat: create ProjectFlockConfirmationModal component --- .../ProjectFlockConfirmationModal.tsx | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/components/pages/production/project-flock/ProjectFlockConfirmationModal.tsx diff --git a/src/components/pages/production/project-flock/ProjectFlockConfirmationModal.tsx b/src/components/pages/production/project-flock/ProjectFlockConfirmationModal.tsx new file mode 100644 index 00000000..45834ee5 --- /dev/null +++ b/src/components/pages/production/project-flock/ProjectFlockConfirmationModal.tsx @@ -0,0 +1,159 @@ +'use client'; + +import { ChangeEventHandler, RefObject, useId, useState } from 'react'; +import useSWR from 'swr'; + +import ConfirmationModal, { + ConfirmationModalProps, +} from '@/components/modal/ConfirmationModal'; +import TextArea from '@/components/input/TextArea'; +import { ProjectFlockFormConfirmationTable } from '@/components/pages/production/project-flock/form/ProjectFlockForm'; + +import { ProjectFlockFormValues } from '@/components/pages/production/project-flock/form/ProjectFlockForm.schema'; +import { Color } from '@/types/theme'; +import { isResponseSuccess } from '@/lib/api-helper'; +import { KandangApi } from '@/services/api/master-data'; + +interface ProjectFlockConfirmationModalProps + extends Omit { + ref: RefObject; + type?: 'info' | 'success' | 'error'; + projectFlockIds?: number[]; + projectFlockForm?: ProjectFlockFormValues; + onClose?: () => void; + + withNote?: boolean; + noteLabel?: string; + rows?: number; + placeholder?: string; + + primaryButton?: { + text?: string; + color?: Color; + isLoading?: boolean; + onClick?: (notes: string) => void; + }; +} + +const ProjectFlockConfirmationModal = ({ + ref, + type = 'success', + projectFlockForm, + projectFlockIds, + onClose, + withNote, + rows = 4, + noteLabel, + placeholder = 'Alasan', + primaryButton, + secondaryButton, + ...props +}: ProjectFlockConfirmationModalProps) => { + const randomId = useId(); + + const [notes, setNotes] = useState(''); + + const kandangUrl = `${KandangApi.basePath}?${new URLSearchParams({ + search: '', + location_id: projectFlockForm?.location_id + ? String(projectFlockForm?.location_id) + : '', + limit: '500', + }).toString()}`; + const { + data: kandang, + isLoading: isLoadingKandang, + mutate: refreshKandang, + } = useSWR(kandangUrl, KandangApi.getAllFetcher); + + const notesChangeHandler: ChangeEventHandler = (e) => { + setNotes(e.target.value); + }; + + const closeModalHandler = () => { + onClose?.(); + ref.current?.close(); + }; + + return ( + { + if (withNote) { + primaryButton?.onClick?.(notes); + } else if (primaryButton && primaryButton?.onClick) { + primaryButton?.onClick?.(''); + } else { + closeModalHandler(); + } + + setNotes(''); + }, + }} + secondaryButton={ + secondaryButton + ? { + text: secondaryButton?.text ?? 'Cancel', + color: secondaryButton?.color ?? 'none', + onClick: (e) => { + if (secondaryButton && secondaryButton?.onClick) { + secondaryButton.onClick?.(e); + } else { + closeModalHandler(); + } + + setNotes(''); + }, + } + : undefined + } + className={{ + modalBox: 'max-h-full', + }} + {...props} + > +
+ {!projectFlockIds && projectFlockForm && ( + + )} + + {/* {projectFlockIds && + !projectFlockForm && + projectFlockIds.map((projectFlockId, idx) => ( + + ))} */} + + {withNote && ( +