mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE): Refactor sales data fetching and component structure
This commit is contained in:
@@ -34,18 +34,6 @@ const ClosingDetailPage = () => {
|
|||||||
() => ProjectFlockKandangApi.getSingle(Number(kandangId))
|
() => ProjectFlockKandangApi.getSingle(Number(kandangId))
|
||||||
);
|
);
|
||||||
|
|
||||||
const { data: salesData, isLoading: isLoadingSales } = useSWR(
|
|
||||||
kandangId
|
|
||||||
? `sales-${closingId}-${kandangId}`
|
|
||||||
: closingId
|
|
||||||
? `sales-${closingId}`
|
|
||||||
: null,
|
|
||||||
() =>
|
|
||||||
kandangId
|
|
||||||
? ClosingApi.getPenjualanByKandang(Number(closingId), Number(kandangId))
|
|
||||||
: ClosingApi.getPenjualan(Number(closingId))
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!closingId) {
|
if (!closingId) {
|
||||||
router.back();
|
router.back();
|
||||||
|
|
||||||
@@ -62,10 +50,7 @@ const ClosingDetailPage = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isLoading =
|
const isLoading =
|
||||||
isLoadingClosing ||
|
isLoadingClosing || isLoadingProject || isLoadingKandang;
|
||||||
isLoadingSales ||
|
|
||||||
isLoadingProject ||
|
|
||||||
isLoadingKandang;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='w-full p-4 flex flex-row justify-center'>
|
<div className='w-full p-4 flex flex-row justify-center'>
|
||||||
@@ -75,7 +60,6 @@ const ClosingDetailPage = () => {
|
|||||||
<ClosingDetail
|
<ClosingDetail
|
||||||
id={Number(closingId)}
|
id={Number(closingId)}
|
||||||
initialValue={closing.data}
|
initialValue={closing.data}
|
||||||
salesData={isResponseSuccess(salesData) ? salesData.data : undefined}
|
|
||||||
projectData={
|
projectData={
|
||||||
isResponseSuccess(projectData) ? projectData.data : undefined
|
isResponseSuccess(projectData) ? projectData.data : undefined
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,11 @@ import ProductionDataClosingTab from '@/components/pages/closing/tab/ProductionD
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
ClosingGeneralInformation,
|
ClosingGeneralInformation,
|
||||||
BaseClosingSales,
|
|
||||||
} from '@/types/api/closing';
|
} from '@/types/api/closing';
|
||||||
import SapronakCalculationClosingTab from '@/components/pages/closing/tab/SapronakCalculationClosingTab';
|
import SapronakCalculationClosingTab from '@/components/pages/closing/tab/SapronakCalculationClosingTab';
|
||||||
import OverheadClosingTab from '@/components/pages/closing/tab/OverheadClosingTab';
|
import OverheadClosingTab from '@/components/pages/closing/tab/OverheadClosingTab';
|
||||||
import FinanceClosingTab from '@/components/pages/closing/tab/FinanceClosingTab';
|
import FinanceClosingTab from '@/components/pages/closing/tab/FinanceClosingTab';
|
||||||
import SalesClosingTable from '@/components/pages/closing/table/SalesClosingTable';
|
import SalesClosingTab from '@/components/pages/closing/tab/SalesClosingTab';
|
||||||
import HppExpeditionClosingTab from '@/components/pages/closing/tab/HppExpeditionClosingTab';
|
import HppExpeditionClosingTab from '@/components/pages/closing/tab/HppExpeditionClosingTab';
|
||||||
import ClosingKandangList from '@/components/pages/closing/ClosingKandangList';
|
import ClosingKandangList from '@/components/pages/closing/ClosingKandangList';
|
||||||
import { ProjectFlock } from '@/types/api/production/project-flock';
|
import { ProjectFlock } from '@/types/api/production/project-flock';
|
||||||
@@ -25,7 +24,6 @@ import { useClosingTabStore } from '@/stores/closing/closing-tab.store';
|
|||||||
interface ClosingDetailProps {
|
interface ClosingDetailProps {
|
||||||
id: number;
|
id: number;
|
||||||
initialValue?: ClosingGeneralInformation;
|
initialValue?: ClosingGeneralInformation;
|
||||||
salesData?: BaseClosingSales;
|
|
||||||
projectData?: ProjectFlock;
|
projectData?: ProjectFlock;
|
||||||
kandangData?: ProjectFlockKandang;
|
kandangData?: ProjectFlockKandang;
|
||||||
}
|
}
|
||||||
@@ -33,7 +31,6 @@ interface ClosingDetailProps {
|
|||||||
const ClosingDetail: React.FC<ClosingDetailProps> = ({
|
const ClosingDetail: React.FC<ClosingDetailProps> = ({
|
||||||
id,
|
id,
|
||||||
initialValue,
|
initialValue,
|
||||||
salesData,
|
|
||||||
projectData,
|
projectData,
|
||||||
kandangData,
|
kandangData,
|
||||||
}) => {
|
}) => {
|
||||||
@@ -60,7 +57,7 @@ const ClosingDetail: React.FC<ClosingDetailProps> = ({
|
|||||||
{
|
{
|
||||||
id: 'penjualan',
|
id: 'penjualan',
|
||||||
label: 'Penjualan',
|
label: 'Penjualan',
|
||||||
content: <SalesClosingTable initialValues={salesData} />,
|
content: <SalesClosingTab projectFlockId={id} />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'overhead',
|
id: 'overhead',
|
||||||
@@ -91,7 +88,7 @@ const ClosingDetail: React.FC<ClosingDetailProps> = ({
|
|||||||
];
|
];
|
||||||
|
|
||||||
return validTabs;
|
return validTabs;
|
||||||
}, [initialValue, salesData, kandangData, id]);
|
}, [initialValue, kandangData, id]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import SalesClosingTable from '@/components/pages/closing/table/SalesClosingTable';
|
||||||
|
|
||||||
|
interface SalesClosingTabProps {
|
||||||
|
projectFlockId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SalesClosingTab = ({
|
||||||
|
projectFlockId,
|
||||||
|
}: SalesClosingTabProps) => {
|
||||||
|
return (
|
||||||
|
<div className='flex flex-col gap-4'>
|
||||||
|
{projectFlockId && (
|
||||||
|
<SalesClosingTable projectFlockId={projectFlockId} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SalesClosingTab;
|
||||||
@@ -5,6 +5,7 @@ import { ColumnDef } from '@tanstack/react-table';
|
|||||||
import Table from '@/components/Table';
|
import Table from '@/components/Table';
|
||||||
import Card from '@/components/Card';
|
import Card from '@/components/Card';
|
||||||
import { formatCurrency, formatNumber, formatDate } from '@/lib/helper';
|
import { formatCurrency, formatNumber, formatDate } from '@/lib/helper';
|
||||||
|
import { isResponseSuccess } from '@/lib/api-helper';
|
||||||
import {
|
import {
|
||||||
BaseClosingSales,
|
BaseClosingSales,
|
||||||
BaseSales,
|
BaseSales,
|
||||||
@@ -13,20 +14,46 @@ import {
|
|||||||
import { Product } from '@/types/api/master-data/product';
|
import { Product } from '@/types/api/master-data/product';
|
||||||
import { Customer } from '@/types/api/master-data/customer';
|
import { Customer } from '@/types/api/master-data/customer';
|
||||||
import { Kandang } from '@/types/api/master-data/kandang';
|
import { Kandang } from '@/types/api/master-data/kandang';
|
||||||
|
import { ClosingApi } from '@/services/api/closing';
|
||||||
|
import { useSearchParams } from 'next/navigation';
|
||||||
|
import useSWR from 'swr';
|
||||||
|
|
||||||
interface SalesClosingTableProps {
|
interface SalesClosingTableProps {
|
||||||
type?: 'detail';
|
projectFlockId: number;
|
||||||
initialValues?: BaseClosingSales;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const SalesClosingTable = ({ initialValues }: SalesClosingTableProps) => {
|
const SalesClosingTable = ({
|
||||||
|
projectFlockId,
|
||||||
|
}: SalesClosingTableProps) => {
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const kandangId = searchParams.get('kandangId');
|
||||||
|
|
||||||
|
const { data: sales, isLoading } = useSWR(
|
||||||
|
kandangId
|
||||||
|
? `/closing/sales/${projectFlockId}/${kandangId}`
|
||||||
|
: `/closing/sales/${projectFlockId}`,
|
||||||
|
() =>
|
||||||
|
kandangId
|
||||||
|
? ClosingApi.getPenjualanByKandang(
|
||||||
|
projectFlockId,
|
||||||
|
Number(kandangId)
|
||||||
|
)
|
||||||
|
: ClosingApi.getPenjualan(projectFlockId)
|
||||||
|
);
|
||||||
|
|
||||||
const salesData: BaseSales[] = useMemo(() => {
|
const salesData: BaseSales[] = useMemo(() => {
|
||||||
return initialValues?.sales || [];
|
if (isResponseSuccess(sales)) {
|
||||||
}, [initialValues]);
|
return sales.data.sales || [];
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}, [sales]);
|
||||||
|
|
||||||
const summary: ClosingSalesSummary | undefined = useMemo(() => {
|
const summary: ClosingSalesSummary | undefined = useMemo(() => {
|
||||||
return initialValues?.summary;
|
if (isResponseSuccess(sales)) {
|
||||||
}, [initialValues]);
|
return sales.data.summary;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}, [sales]);
|
||||||
|
|
||||||
const totals = useMemo(() => {
|
const totals = useMemo(() => {
|
||||||
if (salesData.length === 0) {
|
if (salesData.length === 0) {
|
||||||
@@ -306,6 +333,7 @@ const SalesClosingTable = ({ initialValues }: SalesClosingTableProps) => {
|
|||||||
<Table
|
<Table
|
||||||
data={salesData}
|
data={salesData}
|
||||||
columns={salesColumns}
|
columns={salesColumns}
|
||||||
|
isLoading={isLoading}
|
||||||
renderFooter={salesData.length > 0}
|
renderFooter={salesData.length > 0}
|
||||||
className={{
|
className={{
|
||||||
tableWrapperClassName: 'overflow-x-auto',
|
tableWrapperClassName: 'overflow-x-auto',
|
||||||
|
|||||||
Reference in New Issue
Block a user