Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preview): fix revoke url when Preview unmount #43

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/order/desktop/OrderListDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const OrderListDesktop: Component<{ initialTotalCost: MutableRefObject<nu
setIsFileUploadSuccess(false);
setIsOrderUpdate(false);
setDesktopOrderStep(0);
setMobileOrderStep({
current: 0,
prev: 3
});
if (printingRequestId?.id) {
await cancelPrintingRequest.mutateAsync(printingRequestId?.id);
}
Expand Down
8 changes: 7 additions & 1 deletion src/components/order/desktop/OrderSuccessDesktop.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { MutableRefObject, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { Button, Card, CardBody, Dialog, DialogBody, Typography } from '@material-tailwind/react';
import { CheckIcon, DocumentChartBarIcon } from '@heroicons/react/24/outline';
import coinImage from '@assets/coin.png';
import { useOrderPrintStore, useOrderWorkflowStore } from '@states';

export function useOrderSuccessDesktop() {
const queryClient = useQueryClient();
const [openDialog, setOpenDialog] = useState<boolean>(false);

const OrderSuccessDesktop: Component<{
initialTotalCost: MutableRefObject<number>;
serviceFee?: number;
}> = ({ initialTotalCost, serviceFee }) => {
const printingRequestId = queryClient.getQueryData<PrintingRequestId>(['printingRequestId']);
const { totalCost, setTotalCost, setIsFileUploadSuccess } = useOrderPrintStore();
const { setDesktopOrderStep } = useOrderWorkflowStore();

Expand Down Expand Up @@ -51,7 +54,10 @@ export function useOrderSuccessDesktop() {
<div className='flex flex-col gap-1'>
<div className='flex justify-between items-center'>
<p className='text-gray/4 text-base font-normal'>Order number:</p>
<p className='text-gray/4 text-base font-medium'>{`#1234-5678`}</p>
<p className='text-gray/4 text-base font-medium'>
{printingRequestId &&
`#${printingRequestId.id.slice(0, 4)}-${printingRequestId.id.slice(4, 8)}`}
</p>
</div>
<div className='flex justify-between items-center'>
<p className='text-gray/4 text-base font-normal'>Pick-up location:</p>
Expand Down
21 changes: 11 additions & 10 deletions src/components/order/desktop/UploadAndPreviewDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,20 @@ export const UploadAndPreviewDesktop: Component<{
if (coinPerPage !== undefined) {
const pdfDoc = await PDFDocument.load(fileEditedBuffer);
setFileCoins(pdfDoc.getPageCount() * coinPerPage);
setTotalCost(initialTotalCost.current + pdfDoc.getPageCount() * coinPerPage);
setTotalCost(
initialTotalCost.current + pdfDoc.getPageCount() * coinPerPage * fileConfig.numOfCopies
);
}
}
};
handleEditPdfPrinting();

return () => {
const revokeURL = queryClient.getQueryData<string>(['fileURL']);
if (revokeURL) {
URL.revokeObjectURL(revokeURL);
}
};
}, [
initialTotalCost,
coinPerPage,
Expand All @@ -88,6 +97,7 @@ export const UploadAndPreviewDesktop: Component<{
fileConfig.pageSide,
fileConfig.pages,
fileConfig.pagesPerSheet,
fileConfig.numOfCopies,
queryClient,
editPdfPrinting,
setFileCoins,
Expand Down Expand Up @@ -225,15 +235,6 @@ export const UploadAndPreviewDesktop: Component<{
queryFn: () => queryClient.getQueryData<string>(['fileURL'])
});

useEffect(() => {
return () => {
const revokeURL = queryClient.getQueryData<string>(['fileURL']);
if (revokeURL) {
URL.revokeObjectURL(revokeURL);
}
};
}, []);

const PreviewBody = () => {
return (
<DocViewer
Expand Down
7 changes: 7 additions & 0 deletions src/components/order/mobile/OrderListForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ export const OrderListForm: Component<{
setTotalCost(0);
setIsFileUploadSuccess(false);
setIsOrderUpdate(false);
setMobileOrderStep({
current: 0,
prev: 2
});
setDesktopOrderStep(0);
await handleExistOrderForm();
}, [
initialTotalCost,
setIsFileUploadSuccess,
setIsOrderUpdate,
setTotalCost,
setMobileOrderStep,
setDesktopOrderStep,
handleExistOrderForm
]);

Expand Down
8 changes: 7 additions & 1 deletion src/components/order/mobile/OrderSuccessForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MutableRefObject } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { Button, Card, CardBody, IconButton, Typography } from '@material-tailwind/react';
import { CheckIcon, DocumentChartBarIcon } from '@heroicons/react/24/outline';
import { XMarkIcon } from '@heroicons/react/24/solid';
Expand All @@ -10,6 +11,8 @@ export const OrderSuccessForm: Component<{
handleCloseOrderForm: () => void;
initialTotalCost: MutableRefObject<number>;
}> = ({ handleCloseOrderForm, initialTotalCost }) => {
const queryClient = useQueryClient();
const printingRequestId = queryClient.getQueryData<PrintingRequestId>(['printingRequestId']);
const {
serviceFee: { data: serviceFee }
} = usePrintingRequestQuery();
Expand Down Expand Up @@ -54,7 +57,10 @@ export const OrderSuccessForm: Component<{
<div className='flex flex-col gap-1'>
<div className='flex justify-between items-center'>
<p className='text-gray/4 text-base font-normal'>Order number:</p>
<p className='text-gray/4 text-base font-medium'>{`#1234-5678`}</p>
<p className='text-gray/4 text-base font-medium'>
{printingRequestId &&
`#${printingRequestId.id.slice(0, 4)}-${printingRequestId.id.slice(4, 8)}`}
</p>
</div>
<div className='flex justify-between items-center'>
<p className='text-gray/4 text-base font-normal'>Pick-up location:</p>
Expand Down
5 changes: 4 additions & 1 deletion src/components/order/mobile/UploadDocumentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export const UploadDocumentForm: Component<{
if (coinPerPage !== undefined) {
const pdfDoc = await PDFDocument.load(fileEditedBuffer);
setFileCoins(pdfDoc.getPageCount() * coinPerPage);
setTotalCost(initialTotalCost.current + pdfDoc.getPageCount() * coinPerPage);
setTotalCost(
initialTotalCost.current + pdfDoc.getPageCount() * coinPerPage * fileConfig.numOfCopies
);
}
}
};
Expand All @@ -90,6 +92,7 @@ export const UploadDocumentForm: Component<{
fileConfig.pageSide,
fileConfig.pages,
fileConfig.pagesPerSheet,
fileConfig.numOfCopies,
queryClient,
setFileCoins,
setTotalCost,
Expand Down