'use client'; import ActionPopMenu from '@/components/common/ActionPopMenu'; import { MyTable, ColumnData } from '@/components/common/MyTable'; import StatusChangePopup from '@/components/common/StatusChangePopup'; import BaseButton from '@/components/ui-kit/BaseButton'; import BaseIconButton from '@/components/ui-kit/BaseIconButton'; import BaseInput from '@/components/ui-kit/BaseInput'; import BasePagination from '@/components/ui-kit/BasePagination'; import { useAuthContext } from '@/context/auth-context'; import { BoxStatus, BoxStatusList, IBox } from '@/data/box/box.model'; import { box_requests } from '@/data/box/box.requests'; import { DEFAULT_PAGE_SIZE, pageLinks } from '@/helpers/constants'; import useDebouncedInput from '@/hooks/useDebouncedInput'; import useInput from '@/hooks/useInput'; import { useMyNavigation } from '@/hooks/useMyNavigation'; import { useMyTranslation } from '@/hooks/useMyTranslation'; import useRequest from '@/hooks/useRequest'; import { file_service } from '@/services/file-service'; import { notifyUnknownError } from '@/services/notification'; import { getBoxStatusStyles, getStatusColor } from '@/theme/getStatusBoxStyles'; import { Add, AddCircleOutline, Circle, Delete, Download, Edit, FilterList, FilterListOff, Search, PlusOne } from '@mui/icons-material'; import { Box, Button, Stack, Tooltip, Typography } from '@mui/material'; import { useRouter } from 'next/navigation'; import React, { useEffect, useMemo, useState } from 'react'; type Props = {}; const DashboardBoxesPage = (props: Props) => { const t = useMyTranslation(); const navigation = useMyNavigation(); const { isAdmin } = useAuthContext(); const [page, setPage] = useState(1); const [pageSize] = useState(DEFAULT_PAGE_SIZE); const { value: keyword, onChange: handleKeyword, setValue: setKeyword } = useInput(''); const [boxStatusFilter, setBoxStatusFilter] = useState(undefined); const [deleteIds, setDeleteIds] = useState([]); const [downloadIds, setDownloadIds] = useState([]); const [changeStatusIds, setChangeStatusIds] = useState([]); const boxStatusOptions = useMemo(() => { const p = ['READY_TO_INVOICE'] as BoxStatus[]; if (isAdmin) { p.push('READY'); } return p; }, [isAdmin]); const getBoxesQuery = useRequest( () => box_requests.getAll({ page: page, cargoId: keyword, status: boxStatusFilter, direction: 'desc', sort: 'id', }), { dependencies: [page, boxStatusFilter], selectData(data) { return data.data.data; }, } ); const { data: list, totalElements, totalPages, } = useMemo(() => { if (getBoxesQuery.data?.data) { return { data: getBoxesQuery.data.data, totalElements: getBoxesQuery.data.totalElements, totalPages: getBoxesQuery.data.totalPages, }; } else { return { data: [], totalElements: 0, totalPages: 0, }; } }, [getBoxesQuery]); const loading = getBoxesQuery.loading; const handleChange = (newPage: number) => { setTimeout(() => { setPage(newPage); }, 100); }; const resetFilter = () => { setPage(1); setKeyword(''); setBoxStatusFilter(undefined); }; const onDelete = async (id: number) => { if (deleteIds.includes(id)) return; try { setDeleteIds(p => [...p, id]); await box_requests.delete({ packetId: id }); getBoxesQuery.refetch(); } catch (error) { notifyUnknownError(error); } finally { setDeleteIds(prev => prev.filter(i => i !== id)); } }; const onDownloadExcel = async (id: number) => { if (downloadIds.includes(id)) return; try { setDownloadIds(p => [...p, id]); const response = await box_requests.downloadExcel({ packetId: id }); const file = new File([response.data], 'Box-excel.xlsx', { type: response.data.type }); file_service.download(file); } catch (error) { notifyUnknownError(error); } finally { setDownloadIds(prev => prev.filter(i => i !== id)); } }; const onChangeStatus = async (id: number, newStatus: BoxStatus) => { if (changeStatusIds.includes(id)) return; try { setChangeStatusIds(p => [...p, id]); await box_requests.changeStatus({ packetId: id, status: newStatus }); getBoxesQuery.refetch(); } catch (error) { notifyUnknownError(error); } finally { setChangeStatusIds(prev => prev.filter(i => i !== id)); } }; useEffect(() => { const timeoutId = setTimeout(() => { setPage(1); getBoxesQuery.refetch(); }, 350); return () => clearTimeout(timeoutId); }, [keyword]); // No, PartyName, PacketName, PartyTozaOg'irlik, CountOfItems, WeightOfItems, CargoID, PassportNameFamily - PacketStatusForInvoice const columns: ColumnData[] = [ { label: t('No'), width: 100, renderCell(data, rowIndex) { return (page - 1) * pageSize + rowIndex + 1; }, }, { dataKey: 'id', label: "Qo'shish", width: 120, renderCell: data => { return ; }, }, { dataKey: 'partyName', label: t('party_name'), width: 120, }, { dataKey: 'name', label: t('name'), width: 120, }, { dataKey: 'packetNetWeight', label: t("weight"), width: 120, }, { dataKey: 'totalItems', label: t('count_of_items'), width: 120, }, { dataKey: 'totalNetWeight', label: t("party_weight"), width: 120, }, { dataKey: 'cargoId', label: t('cargo_id'), width: 120, }, { dataKey: 'passportName', label: t('client'), width: 120, }, { dataKey: 'status', label: t('status'), width: 240, renderHeaderCell(rowIndex) { return ( {t('box_status')} { return { icon: , label: t(stat), onClick() { setBoxStatusFilter(stat); setPage(1); }, }; })} mainIcon={} placement={{ anchorOrigin: { vertical: 'bottom', horizontal: 'center', }, transformOrigin: { horizontal: 'center', vertical: 'top', }, }} /> ); }, renderCell(data) { return ( { return { icon: ( ), label: t(stat), onClick: () => { onChangeStatus(data.id, stat); }, }; })} /> ); }, }, { label: '', width: 100, numeric: true, renderCell(data, rowIndex) { return ( , label: t('edit'), onClick: () => { navigation.push(pageLinks.dashboard.boxes.edit(data.id)); }, }, { icon: , label: t('delete'), onClick: () => { onDelete(data.id); }, dontCloseOnClick: true, loading: deleteIds.includes(data.id), }, ...(data.status === 'READY' ? [ { icon: , label: t('download_excel'), onClick: () => { onDownloadExcel(data.id); }, loading: downloadIds.includes(data.id), dontCloseOnClick: true, }, ] : []), ]} /> ); }, }, ]; return ( } href={pageLinks.dashboard.boxes.create}> {t('create_packet')} {t('packet')} , }} placeholder={t('filter_packet_name')} value={keyword} onChange={e => { setKeyword(e.target.value); }} /> } size='small' onClick={resetFilter}> {t('reset_filter')} ); }; export default DashboardBoxesPage;