diff --git a/src/components/pages/uniformity/UniformityTable.tsx b/src/components/pages/uniformity/UniformityTable.tsx index 91816751..8c997d7c 100644 --- a/src/components/pages/uniformity/UniformityTable.tsx +++ b/src/components/pages/uniformity/UniformityTable.tsx @@ -3,7 +3,7 @@ import React, { useCallback, useState, useEffect } from 'react'; import useSWR from 'swr'; import { Icon } from '@iconify/react'; -import { SortingState, CellContext } from '@tanstack/react-table'; +import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table'; import { cn } from '@/lib/helper'; import Button from '@/components/Button'; import UniformityChart from '@/components/pages/uniformity/UniformityChart'; @@ -168,6 +168,142 @@ const UniformityTable = ({ refresh }: { refresh?: () => void }) => { } }; + // ===== TABLE COLUMNS DEFINITION ===== + const uniformityColumns: ColumnDef[] = [ + { + id: 'select', + header: ({ table }) => { + const allRows = table.getRowModel().rows; + const selectableRows = allRows.filter((row) => { + const uniformity = row.original; + return !isUniformityLocked(uniformity); + }); + + const hasNoSelectableRows = selectableRows.length === 0; + + const handleSelectAll = () => { + const isAllSelected = selectableRows.every((row) => + row.getIsSelected() + ); + + selectableRows.forEach((row) => { + row.toggleSelected(!isAllSelected); + }); + }; + + const isAllSelected = + selectableRows.length > 0 && + selectableRows.every((row) => row.getIsSelected()); + + const isSomeSelected = selectableRows.some((row) => + row.getIsSelected() + ); + + return ( +
+ +
+ ); + }, + cell: ({ row }) => { + const uniformity = row.original; + const isDisabled = isUniformityLocked(uniformity); + + return ( +
+ +
+ ); + }, + }, + { + header: 'Lokasi', + cell: (props) => props.row.original.location.name || '-', + }, + { + header: 'Flock', + cell: (props) => `Flock ${props.row.original.project_flock_kandang_id}`, + }, + { + header: 'Kandang', + cell: (props) => props.row.original.kandang.name || '-', + }, + { + header: 'Tanggal (Week)', + cell: (props) => `Week ${props.row.original.week}`, + }, + { + header: 'Status', + cell: (props) => { + const status = props.row.original.status; + return ( + + {getStatusText(status)} + + ); + }, + }, + { + header: 'Uniformity', + cell: (props) => { + const uniformity = props.row.original.uniformity; + return {uniformity}%; + }, + }, + { + header: 'Aksi', + cell: (props: CellContext) => { + const currentPageSize = props.table.getPaginationRowModel().rows.length; + const currentPageRows = props.table.getPaginationRowModel().flatRows; + const currentRowRelativeIndex = + currentPageRows.findIndex((r) => r.id === props.row.id) + 1; + + const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2; + + const deleteClickHandler = () => { + setSelectedUniformity(props.row.original); + singleDeleteModal.openModal(); + }; + + return ( + <> + {currentPageSize > 2 && ( + + + + )} + + {currentPageSize <= 2 && ( + + + + )} + + ); + }, + }, + ]; + return ( <>
@@ -211,151 +347,7 @@ const UniformityTable = ({ refresh }: { refresh?: () => void }) => { > data={isResponseSuccess(uniformities) ? uniformities?.data : []} - columns={[ - { - id: 'select', - header: ({ table }) => { - const allRows = table.getRowModel().rows; - const selectableRows = allRows.filter((row) => { - const uniformity = row.original; - return !isUniformityLocked(uniformity); - }); - - const hasNoSelectableRows = selectableRows.length === 0; - - const handleSelectAll = () => { - const isAllSelected = selectableRows.every((row) => - row.getIsSelected() - ); - - selectableRows.forEach((row) => { - row.toggleSelected(!isAllSelected); - }); - }; - - const isAllSelected = - selectableRows.length > 0 && - selectableRows.every((row) => row.getIsSelected()); - - const isSomeSelected = selectableRows.some((row) => - row.getIsSelected() - ); - - return ( -
- -
- ); - }, - cell: ({ row }) => { - const uniformity = row.original; - const isDisabled = isUniformityLocked(uniformity); - - return ( -
- -
- ); - }, - }, - { - header: '#', - cell: (props) => - tableFilterState.pageSize * (tableFilterState.page - 1) + - props.row.index + - 1, - }, - { - header: 'Lokasi', - cell: (props) => props.row.original.location.name || '-', - }, - { - header: 'Flock', - cell: (props) => - `Flock ${props.row.original.project_flock_kandang_id}`, - }, - { - header: 'Kandang', - cell: (props) => props.row.original.kandang.name || '-', - }, - { - header: 'Tanggal (Week)', - cell: (props) => `Week ${props.row.original.week}`, - }, - { - header: 'Status', - cell: (props) => { - const status = props.row.original.status; - return ( - - {getStatusText(status)} - - ); - }, - }, - { - header: 'Uniformity', - cell: (props) => { - const uniformity = props.row.original.uniformity; - return {uniformity}%; - }, - }, - { - header: 'Aksi', - cell: (props: CellContext) => { - const currentPageSize = - props.table.getPaginationRowModel().rows.length; - const currentPageRows = - props.table.getPaginationRowModel().flatRows; - const currentRowRelativeIndex = - currentPageRows.findIndex((r) => r.id === props.row.id) + 1; - - const isLast2Rows = - currentRowRelativeIndex > currentPageSize - 2; - - const deleteClickHandler = () => { - setSelectedUniformity(props.row.original); - singleDeleteModal.openModal(); - }; - - return ( - <> - {currentPageSize > 2 && ( - - - - )} - - {currentPageSize <= 2 && ( - - - - )} - - ); - }, - }, - ]} + columns={uniformityColumns} pageSize={tableFilterState.pageSize} page={isResponseSuccess(uniformities) ? uniformities?.meta?.page : 0} totalItems={