diff --git a/src/components/pages/uniformity/UniformityTable.tsx b/src/components/pages/uniformity/UniformityTable.tsx index efa8dd66..97194e98 100644 --- a/src/components/pages/uniformity/UniformityTable.tsx +++ b/src/components/pages/uniformity/UniformityTable.tsx @@ -1,7 +1,7 @@ 'use client'; import React, { useCallback, useState, useEffect, useMemo } from 'react'; -import { useRouter } from 'next/navigation'; +import { useRouter, useSearchParams } from 'next/navigation'; import useSWR from 'swr'; import { Icon } from '@iconify/react'; import { ColumnDef, SortingState } from '@tanstack/react-table'; @@ -61,6 +61,7 @@ const isUniformityLocked = (uniformity: Uniformity): boolean => { const UniformityTable = ({ refresh }: { refresh?: () => void }) => { const router = useRouter(); + const searchParams = useSearchParams(); const isSuccess = useUniformityStore((s) => s.isSuccess); const setIsSuccess = useUniformityStore((s) => s.setIsSuccess); @@ -81,7 +82,9 @@ const UniformityTable = ({ refresh }: { refresh?: () => void }) => { const [sorting, setSorting] = useState([]); const [rowSelection, setRowSelection] = useState>({}); - const [selectedUniformity] = useState(undefined); + const [selectedUniformity, setSelectedUniformity] = useState< + Uniformity | undefined + >(undefined); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [isBulkActionLoading, setIsBulkActionLoading] = useState(false); @@ -120,6 +123,28 @@ const UniformityTable = ({ refresh }: { refresh?: () => void }) => { ); }, [selectedUniformities]); + useEffect(() => { + const action = searchParams.get('action'); + const id = searchParams.get('id'); + + if (action && id) { + if (isResponseSuccess(uniformities)) { + const uniformity = uniformities.data.find((u) => u.id === parseInt(id)); + if (uniformity) { + setSelectedUniformity(uniformity); + + if (action === 'approve') { + singleApproveModal.openModal(); + } else if (action === 'reject') { + singleRejectModal.openModal(); + } + + router.replace('/uniformity', { scroll: false }); + } + } + } + }, [searchParams, uniformities]); + useEffect(() => { if (isSuccess) { successModal.openModal(); diff --git a/src/components/pages/uniformity/detail/UniformityDetail.tsx b/src/components/pages/uniformity/detail/UniformityDetail.tsx index 57edd666..ef43969e 100644 --- a/src/components/pages/uniformity/detail/UniformityDetail.tsx +++ b/src/components/pages/uniformity/detail/UniformityDetail.tsx @@ -10,6 +10,7 @@ import DrawerHeader from '@/components/helper/drawer/DrawerHeader'; import Table from '@/components/Table'; import { formatNumber } from '@/lib/helper'; import { type OptionType } from '@/components/input/SelectInput'; +import RequirePermission from '@/components/helper/RequirePermission'; import { UniformityDetail as UniformityDetailType } from '@/types/api/uniformity/uniformity'; type DetailOptionType = OptionType & { @@ -29,9 +30,19 @@ const UniformityDetail: React.FC = ({ router.back(); }; + const handleApprove = () => { + router.push(`/uniformity?action=approve&id=${initialValues.id}`); + }; + + const handleReject = () => { + router.push(`/uniformity?action=reject&id=${initialValues.id}`); + }; + const infoUmumTableData: DetailOptionType[] = useMemo(() => { if (!initialValues) return []; + const { info_umum } = initialValues; + return [ { id: 'tanggal', @@ -90,116 +101,6 @@ const UniformityDetail: React.FC = ({ [initialValues] ); - const samplingTableData: DetailOptionType[] = useMemo(() => { - if (!initialValues) return []; - - return [ - { - id: 'sampling-size', - value: 'sampling-size', - label: 'Sampling size', - }, - { - id: 'mean-weight', - value: 'mean-weight', - label: 'Mean Weight', - }, - { - id: 'min-limit', - value: 'min-limit', - label: 'Min Limit (-10%)', - }, - { - id: 'max-limit', - value: 'max-limit', - label: 'Max Limit (+10%)', - }, - ]; - }, [initialValues]); - - const columnsSampling: ColumnDef[] = useMemo( - () => [ - { - accessorKey: 'label', - header: 'Label', - cell: (props) => props.row.original.label, - }, - { - accessorKey: 'value', - header: 'Value', - cell: (props) => { - const id = props.row.original.id; - const { sampling } = initialValues!; - - const valueMap: Record = { - 'sampling-size': `${formatNumber(sampling.chick_qty_of_weight)} of Birds`, - 'mean-weight': `${formatNumber(sampling.mean_weight)} g`, - 'min-limit': `${formatNumber(sampling.mean_down)} g`, - 'max-limit': `${formatNumber(sampling.mean_up)} g`, - }; - - return {valueMap[id] || '-'}; - }, - }, - ], - [initialValues] - ); - - const resultTableData: DetailOptionType[] = useMemo(() => { - if (!initialValues) return []; - - return [ - { - id: 'ideal-birds', - value: 'ideal-birds', - label: 'Ideal Birds', - }, - { - id: 'outside-range', - value: 'outside-range', - label: 'Outside Range', - }, - { - id: 'uniformity', - value: 'uniformity', - label: 'Uniformity', - }, - { - id: 'cv', - value: 'cv', - label: 'CV', - }, - ]; - }, [initialValues]); - - const columnsResult: ColumnDef[] = useMemo( - () => [ - { - accessorKey: 'label', - header: 'Label', - cell: (props) => props.row.original.label, - }, - { - accessorKey: 'value', - header: 'Value', - cell: (props) => { - const id = props.row.original.id; - const { result } = initialValues!; - - const valueMap: Record = { - 'ideal-birds': `${formatNumber(result.uniform_qty)} of Birds`, - 'outside-range': `${formatNumber(result.outside_qty)} of Birds`, - uniformity: `${result.uniformity} %`, - cv: `${result.cv}`, - }; - - return {valueMap[id] || '-'}; - }, - }, - ], - [initialValues] - ); - return (
{/* Header */} @@ -233,34 +134,19 @@ const UniformityDetail: React.FC = ({ paginationClassName: 'hidden', }} /> - - {/* Sampling */} -
-

Sampling and Range

- - data={samplingTableData} - columns={columnsSampling} - pageSize={4} - className={{ - containerClassName: 'mb-0', - paginationClassName: 'hidden', - }} - /> -
- - {/* Result */} -
-

Result

- - data={resultTableData} - columns={columnsResult} - pageSize={4} - className={{ - containerClassName: 'mb-0', - paginationClassName: 'hidden', - }} - /> +
+ {/* Approve/Reject Buttons */} + {initialValues.result && ( + +
+ + +
+
+ )}
) : (