import moment from 'moment'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { IMAGE_ROOT } from '@/shared/constants/common'; import { PATHS } from '@/shared/constants/paths'; import { useNavigate } from '@/shared/lib/hooks/use-navigate'; import { CashReceiptList } from '@/entities/transaction/ui/cash-receipt-list'; import { TransactionCategory, CashReceiptPurposeType, CashReceiptProcessResult, ListItemProps, CashReceiptListParams, CashReceiptTransactionType, CashReceiptSearchNumberType, CashReceiptSummaryParams, CashReceiptListResponse, CashReceiptSummaryResponse, DetailData } from '@/entities/transaction/model/types'; import { useCashReceiptListMutation } from '@/entities/transaction/api/use-cash-receipt-list-mutation'; import { useDownloadExcelMutation } from '@/entities/transaction/api/use-download-excel-mutation'; import { DEFAULT_PAGE_PARAM } from '@/entities/common/model/constant'; import { CashReceiptFilter } from '@/entities/transaction/ui/filter/cash-receipt-filter'; import { SortTypeBox } from '@/entities/common/ui/sort-type-box'; import { SortTypeKeys, HeaderType, DefaultRequestPagination } from '@/entities/common/model/types'; import { useSetOnBack, useSetHeaderTitle, useSetHeaderType, useSetFooterMode } from '@/widgets/sub-layout/use-sub-layout'; import { getCashReceiptTransactionTypeBtnGroup } from '@/entities/transaction/model/contant'; import { useStore } from '@/shared/model/store'; import { useCashReceiptSummaryMutation } from '@/entities/transaction/api/use-cash-receipt-summary-mutation'; import { EmailBottomSheet } from '@/entities/common/ui/email-bottom-sheet'; import useIntersectionObserver from '@/widgets/intersection-observer'; import { CashReceiptDetail } from '@/entities/transaction/ui/detail/cash-receit-detail'; export const CashReceiptListPage = () => { const { navigate } = useNavigate(); const { t, i18n } = useTranslation(); const userMid = useStore.getState().UserStore.mid; const [onActionIntersect, setOnActionIntersect] = useState(false); const [sortType, setSortType] = useState(SortTypeKeys.LATEST); const [listItems, setListItems] = useState>([]); const [filterOn, setFilterOn] = useState(false); const [pageParam, setPageParam] = useState(DEFAULT_PAGE_PARAM); const [mid, setMid] = useState(userMid); const [startDate, setStartDate] = useState(moment().subtract(1, 'month').format('YYYYMMDD')); const [endDate, setEndDate] = useState(moment().format('YYYYMMDD')); const [purposeType, setPurposeType] = useState(CashReceiptPurposeType.ALL); const [transactionType, setTransactionType] = useState(CashReceiptTransactionType.ALL); const [processResult, setProcessResult] = useState(CashReceiptProcessResult.ALL); const [searchNumberType, setSearchNumberType] = useState(CashReceiptSearchNumberType.APPROVAL_NUMBER); const [searchNumber, setSearchNumber] = useState(''); const [approvalCount, setApprovalCount] = useState(0); const [approvalAmount, setApprovalAmount] = useState(0); const [cancelCount, setCancelCount] = useState(0); const [cancelAmount, setCancelAmount] = useState(0); const [totalCount, setTotalCount] = useState(0); const [emailBottomSheetOn, setEmailBottomSheetOn] = useState(false); const [detailOn, setDetailOn] = useState(false); const [detailTid, setDetailTid] = useState(''); const [detailServiceCode, setDetailServiceCode] = useState(''); useSetHeaderTitle(t('cashReceipt.title')); useSetHeaderType(HeaderType.LeftArrow); useSetOnBack(() => { navigate(PATHS.home); }); useSetFooterMode(false); const { mutateAsync: cashReceiptList } = useCashReceiptListMutation(); const { mutateAsync: cashReceiptSummary } = useCashReceiptSummaryMutation(); const { mutateAsync: downloadExcel } = useDownloadExcelMutation(); const onIntersect: IntersectionObserverCallback = (entries: Array) => { entries.forEach((entry: IntersectionObserverEntry) => { if(entry.isIntersecting){ if(onActionIntersect && !!pageParam.cursor){ setOnActionIntersect(false); callList('page'); } } }); }; const { setTarget } = useIntersectionObserver({ threshold: 1, onIntersect }); const callList = (type?: string) => { setOnActionIntersect(false); let listSummaryParams: CashReceiptSummaryParams = { mid: mid, startDate: startDate, endDate: endDate, purposeType: purposeType, transactionType: transactionType, processResult: processResult, searchNumberType: searchNumberType, searchNumber: searchNumber, page: pageParam }; let listParams: CashReceiptListParams = { ...listSummaryParams, page: { ...pageParam, ...{ sortType: sortType } } }; if(type !== 'page' && listParams.page){ listParams.page.cursor = null; } cashReceiptList(listParams).then((rs: CashReceiptListResponse) => { if(type === 'page'){ setListItems([ ...listItems, ...rs.content ]); } else{ setListItems(rs.content); } if(rs.hasNext && rs.nextCursor !== pageParam.cursor && rs.content.length === DEFAULT_PAGE_PARAM.size ){ setPageParam({ ...pageParam, ...{ cursor: rs.nextCursor } }); } else{ setPageParam({ ...pageParam, ...{ cursor: null } }); } setOnActionIntersect( !!rs.hasNext && rs.nextCursor !== pageParam.cursor && rs.content.length === DEFAULT_PAGE_PARAM.size ); }); cashReceiptSummary(listSummaryParams).then((rs: CashReceiptSummaryResponse) => { setApprovalCount(rs.approvalCount); setApprovalAmount(rs.approvalAmount); setCancelCount(rs.cancelCount); setCancelAmount(rs.cancelAmount); setTotalCount(rs.totalCount); }); } const onRequestDownloadExcel = (userEmail?: string) => { setEmailBottomSheetOn(true); }; const setDetailData = (detailData: DetailData) => { setDetailOn(detailData.detailOn); setDetailTid(detailData.tid); if(detailData?.serviceCode){ setDetailServiceCode(detailData?.serviceCode); } }; const onClickToOpenFilter = () => { setFilterOn(!filterOn); }; const onClickToDownloadExcel = () => { // tid??? 확인 필요 downloadExcel({ // tid: tid }).then((rs) => { }); }; const onClickToSort = (sort: SortTypeKeys) => { setSortType(sort); }; const onClickToTransactionType = (val: CashReceiptTransactionType) => { setTransactionType(val); }; const onClickToNavigate = () => { navigate(PATHS.transaction.cashReceipt.handWrittenIssuance); }; useEffect(() => { callList(); }, [ mid, startDate, endDate, sortType, purposeType, transactionType, processResult, searchNumberType, searchNumber ]); return ( <>
{ t('cashReceipt.approval') } {t('home.money', { value: new Intl.NumberFormat('en-US').format(approvalAmount || 0) })} ({t('home.count', { value: new Intl.NumberFormat('en-US').format(approvalCount || 0) })})
{ t('common.cancel') } {t('home.money', { value: new Intl.NumberFormat('en-US').format(cancelAmount || 0) })} ({t('home.count', { value: new Intl.NumberFormat('en-US').format(cancelCount || 0) })})
{ getCashReceiptTransactionTypeBtnGroup(t).map((value: any, index: number) => ( onClickToTransactionType(value.value)} >{value.name} )) }
{ !!emailBottomSheetOn && } ); };