diff --git a/src/pages/payment-record.tsx b/src/pages/payment-record.tsx index 526ba7b4..28e90d25 100644 --- a/src/pages/payment-record.tsx +++ b/src/pages/payment-record.tsx @@ -8,7 +8,7 @@ import Layout from "@/components/High/Layout"; import {shouldRedirectHome} from "@/utils/navigation.disabled"; import usePayments from "@/hooks/usePayments"; import {Payment} from "@/interfaces/paypal"; -import {createColumnHelper, flexRender, getCoreRowModel, useReactTable} from "@tanstack/react-table"; +import {CellContext, createColumnHelper, flexRender, getCoreRowModel, HeaderGroup, useReactTable} from "@tanstack/react-table"; import {CURRENCIES} from "@/resources/paypal"; import {BsTrash} from "react-icons/bs"; import axios from "axios"; @@ -251,6 +251,24 @@ const IS_PAID_OPTIONS = [ label: 'Paid', }, ]; + +const CSV_WHITELISTED_KEYS = [ + 'corporateId', + 'corporate', + 'date', + 'amount', + 'agent', + 'agentCommission', + 'agentValue', + 'isPaid', +]; + +interface SimpleCSVColumn { + key: string, + label: string, + index: number, +}; + export default function PaymentRecord() { const [selectedCorporateUser, setSelectedCorporateUser] = useState(); const [selectedAgentUser, setSelectedAgentUser] = useState(); @@ -453,24 +471,75 @@ export default function PaymentRecord() { return []; }; + + const columHelperValue = (key: string, info: any) => { + switch(key) { + case 'agentCommission': { + const value = info.getValue(); + return { value: `${value}%`}; + } + case 'agent': { + const user = users.find((x) => x.id === info.row.original.agent) as AgentUser; + return { + value: user?.name, + user, + } + } + case 'agentValue': + case 'amount': { + const value = info.getValue(); + const numberValue = toFixedNumber(value, 2) + return { value: numberValue } + } + case 'date': { + const value = info.getValue(); + return { value: moment(value).format("DD/MM/YYYY") }; + } + case 'corporate': { + const specificValue = info.row.original.corporate; + const user = users.find((x) => x.id === specificValue) as CorporateUser; + return { + user, + value: user?.corporateInformation.companyInformation.name || user?.name, + }; + } + case 'currency': { + return { + value: info.row.original.currency, + }; + } + case 'isPaid': + case 'corporateId': + default: { + const value = info.getValue(); + return { value }; + } + } + + } + + const defaultColumns = [ - columnHelper.accessor("id", { - header: "ID", - id: "id", - cell: (info) => info.getValue(), + columnHelper.accessor("corporate", { + header: "Corporate ID", + id: 'corporateId', + cell: (info) => { + const { value } = columHelperValue(info.column.id, info); + return value; + }, }), columnHelper.accessor("corporate", { header: "Corporate", id: "corporate", cell: (info) => { - const user = users.find((x) => x.id === info.row.original.corporate) as CorporateUser; + const { user, value } = columHelperValue(info.column.id, info); return (
setSelectedCorporateUser(user)}> - {user?.corporateInformation.companyInformation.name || user?.name} + {value}
); }, @@ -478,52 +547,66 @@ export default function PaymentRecord() { columnHelper.accessor("date", { header: "Date", id: "date", - cell: (info) => {moment(info.getValue()).format("DD/MM/YYYY")}, + cell: (info) => { + const { value } = columHelperValue(info.column.id, info); + return {value}; + }, }), columnHelper.accessor("value", { header: "Amount", id: "amount", - cell: (info) => ( - - {toFixedNumber(info.getValue(), 2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label} - - ), + cell: (info) => { + const { value } = columHelperValue(info.column.id, info); + const currency = CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label + const finalValue = `${value} ${currency}`; + return {finalValue}; + } }), columnHelper.accessor("agent", { header: "Country Manager", id: "agent", - cell: (info) => ( -
setSelectedAgentUser(users.find((x) => x.id === info.row.original.agent))}> - {(users.find((x) => x.id === info.row.original.agent) as AgentUser)?.name} -
- ), + cell: (info) => { + const { user, value } = columHelperValue(info.column.id, info); + return ( +
setSelectedAgentUser(user)}> + {value} +
+ ); + }, }), columnHelper.accessor("agentCommission", { header: "Commission", id: "agentCommission", - cell: (info) => <>{info.getValue()}%, + cell: (info) => { + const { value } = columHelperValue(info.column.id, info); + return <>{value} + }, }), columnHelper.accessor("agentValue", { header: "Commission Value", id: "agentValue", - cell: (info) => ( - - {toFixedNumber(info.getValue(), 2)} {CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label} - - ), + cell: (info) => { + const { value } = columHelperValue(info.column.id, info); + const currency = CURRENCIES.find((x) => x.currency === info.row.original.currency)?.label + const finalValue = `${value} ${currency}`; + return {finalValue}; + }, }), columnHelper.accessor("isPaid", { header: "Paid", id: "isPaid", - cell: (info) => ( - (user?.type !== "agent" ? updatePayment(info.row.original, "isPaid", e) : null)}> - - - ), + cell: (info) => { + const { value } = columHelperValue(info.column.id, info); + return ( + (user?.type !== "agent" ? updatePayment(info.row.original, "isPaid", e) : null)}> + + + ); + }, }), ...getFileAssetsColumns(), { @@ -597,7 +680,51 @@ export default function PaymentRecord() { return null; } + + const getCSVData = () => { + const columns = table.getHeaderGroups().reduce((accm: SimpleCSVColumn[], group: HeaderGroup) => { + const whitelistedColumns = group.headers.filter((header) => CSV_WHITELISTED_KEYS.includes(header.id)); + + const data = whitelistedColumns.map((data) => ({ + key: data.column.columnDef.id, + label: data.column.columnDef.header, + })) as SimpleCSVColumn[]; + + return [ + ...accm, + ...data, + ]; + }, []); + + const { rows } = table.getRowModel(); + + const finalColumns = [ + ...columns, + { + key: 'currency', + label: 'Currency', + }, + ]; + + return { + columns: finalColumns, + rows: rows.map((row) => { + return finalColumns.reduce((accm, { key }) => { + const { value } = columHelperValue(key, { + row, + getValue: () => row.getValue(key), + }); + return { + ...accm, + [key]: value, + }; + }, {}); + }), + }; + } + + const { rows: csvRows, columns: csvColumns } = getCSVData(); return ( <> @@ -627,13 +754,8 @@ export default function PaymentRecord() {