feat: implement purchase export to excel

This commit is contained in:
ValdiANS
2026-04-22 23:20:31 +07:00
parent 60df577cc6
commit 5d6aaace86
2 changed files with 54 additions and 0 deletions
@@ -192,6 +192,8 @@ const PurchaseTable = () => {
// ===== STATE MANAGEMENT ===== // ===== STATE MANAGEMENT =====
const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [isDeleteLoading, setIsDeleteLoading] = useState(false);
const [isLoadingExportingToExcel, setIsLoadingExportingToExcel] =
useState(false);
const [isExportProgressLoading, setIsExportProgressLoading] = useState(false); const [isExportProgressLoading, setIsExportProgressLoading] = useState(false);
const [selectedPurchase, setSelectedPurchase] = useState<Purchase | null>( const [selectedPurchase, setSelectedPurchase] = useState<Purchase | null>(
null null
@@ -475,6 +477,20 @@ const PurchaseTable = () => {
updateFilter('approval_status', ''); updateFilter('approval_status', '');
}; };
const exportToExcel = useCallback(async () => {
setIsLoadingExportingToExcel(true);
try {
await PurchaseApi.exportToExcel(getTableFilterQueryString());
} catch (error) {
toast.error(
await getExportErrorMessage(error, 'Gagal mengekspor data pembelian')
);
} finally {
setIsLoadingExportingToExcel(false);
}
}, [getTableFilterQueryString]);
const resetExportProgressForm = useCallback(() => { const resetExportProgressForm = useCallback(() => {
setExportProgressStartDate(''); setExportProgressStartDate('');
setExportProgressEndDate(''); setExportProgressEndDate('');
@@ -610,6 +626,17 @@ const PurchaseTable = () => {
</Button> </Button>
} }
> >
<Button
variant='ghost'
color='none'
onClick={exportToExcel}
isLoading={isLoadingExportingToExcel}
className='w-full p-3 justify-start text-sm text-base-content/50 font-semibold text-nowrap'
>
<Icon icon='heroicons:table-cells' width={20} height={20} />
Ekspor ke Excel
</Button>
<Button <Button
variant='ghost' variant='ghost'
color='none' color='none'
+27
View File
@@ -115,6 +115,33 @@ export const PurchaseApi = {
}, },
}, },
async exportToExcel(initialQueryString: string) {
const params = new URLSearchParams(initialQueryString);
params.set('export', 'excel');
params.set('type', 'all');
params.set('page', '1');
params.set('limit', '99999999999');
const queryString = `?${params.toString()}`;
const res = await httpClient<Blob>(`${this.basePath}${queryString}`, {
method: 'GET',
responseType: 'blob',
});
const url = window.URL.createObjectURL(new Blob([res]));
const link = document.createElement('a');
link.href = url;
const fileName = `pembelian-${formatDate(Date.now(), 'DD-MM-YYYY')}.xlsx`;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
link.remove();
},
async exportInputProgressToExcel(startDate: string, endDate: string) { async exportInputProgressToExcel(startDate: string, endDate: string) {
const params = new URLSearchParams(); const params = new URLSearchParams();