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(DEV-12651): Add option to delete canceled payables #496

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/weak-knives-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@monite/sdk-react': minor
---

Adds option to delete canceled payables
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const PayableDetailsBase = ({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
}: PayablesDetailsProps) => {
Expand All @@ -73,6 +74,7 @@ const PayableDetailsBase = ({
approveInvoice,
cancelInvoice,
reopenInvoice,
deleteInvoice,
isPaymentLinkAvailable,
modalComponent,
},
Expand All @@ -89,6 +91,7 @@ const PayableDetailsBase = ({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
});
Expand Down Expand Up @@ -156,6 +159,7 @@ const PayableDetailsBase = ({
approveInvoice={approveInvoice}
reopenInvoice={reopenInvoice}
cancelInvoice={cancelInvoice}
deleteInvoice={deleteInvoice}
payInvoice={payInvoice}
payableDetailsFormId={payableDetailsFormId}
onClose={onClose}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface PayablesDetailsHeaderProps {
approveInvoice: () => void;
cancelInvoice: () => void;
reopenInvoice: () => void;
deleteInvoice: () => void;
payInvoice: () => void;
/** The "id" of the form used to edit the Payable */
payableDetailsFormId: string;
Expand All @@ -51,6 +52,7 @@ export const PayableDetailsHeader = ({
approveInvoice,
cancelInvoice,
reopenInvoice,
deleteInvoice,
payInvoice,
payableDetailsFormId,
isPaymentLinkAvailable,
Expand Down Expand Up @@ -111,6 +113,15 @@ export const PayableDetailsHeader = ({
onClick: cancelInvoice,
children: t(i18n)`Cancel bill`,
},
delete: {
variant: 'text',
color: 'error',
onClick: () => {
deleteInvoice();
onClose && onClose();
},
children: t(i18n)`Delete bill`,
},
pay: {
variant: 'contained',
onClick: payInvoice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type PayableDetailsPermissions =
| 'reject'
| 'approve'
| 'reopen'
| 'delete'
| 'pay';

export type UsePayableDetailsProps = {
Expand Down Expand Up @@ -142,6 +143,14 @@ export type UsePayableDetailsProps = {
*/
onReopened?: (id: string) => void;

/** Callback function that is called when the payable is deleted
*
* @param {string} id - The ID of the payable
*
* @returns {void}
*/
onDeleted?: (id: string) => void;

/**
* Callback function that is called when the user press the Pay button
*
Expand Down Expand Up @@ -174,6 +183,7 @@ export function usePayableDetails({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
}: UsePayableDetailsProps) {
Expand Down Expand Up @@ -307,6 +317,12 @@ export function usePayableDetails({
entityUserId: payable?.was_created_by_user_id,
});

const { data: isDeleteAvailable } = useIsActionAllowed({
method: 'payable',
action: 'delete',
entityUserId: payable?.was_created_by_user_id,
});

const createMutation = api.payables.postPayables.useMutation(
{},
{
Expand Down Expand Up @@ -465,6 +481,14 @@ export function usePayableDetails({
}
);

const deleteMutation = api.payables.deletePayablesId.useMutation(undefined, {
onSuccess: () =>
Promise.all([api.payables.getPayables.invalidateQueries(queryClient)]),
onError: (error) => {
toast.error(getAPIErrorMessage(i18n, error));
},
});

const status = payable?.status;

useEffect(() => {
Expand Down Expand Up @@ -560,11 +584,18 @@ export function usePayableDetails({
}

case 'rejected': {
if (isReopenAvailable) {
if (isDeleteAvailable) {
setPermissions(['reopen']);
}
break;
}

case 'canceled': {
if (isDeleteAvailable) {
setPermissions(['delete']);
}
break;
}
}

setIsPermissionsLoading(false);
Expand All @@ -577,6 +608,7 @@ export function usePayableDetails({
isSubmitAvailable,
isUpdatesAvailable,
isReopenAvailable,
isDeleteAvailable,
status,
payableId,
]);
Expand Down Expand Up @@ -858,6 +890,21 @@ export function usePayableDetails({
}
};

const deleteInvoice = async () => {
if (payableId) {
await deleteMutation.mutateAsync(
{
path: { payable_id: payableId },
},
{
onSuccess: () => {
onDeleted?.(payableId);
},
}
);
}
};

const payInvoice = useCallback(() => {
if (payable) {
// TODO: remove onPayUS prop
Expand Down Expand Up @@ -889,6 +936,7 @@ export function usePayableDetails({
approveInvoice,
reopenInvoice,
cancelInvoice,
deleteInvoice,
payInvoice,
handlePay,
modalComponent,
Expand Down
31 changes: 20 additions & 11 deletions packages/sdk-react/src/components/payables/Payables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type PayablesProps = Pick<
| 'onRejected'
| 'onApproved'
| 'onReopened'
| 'onDeleted'
| 'onPay'
| 'onPayUS'
>;
Expand All @@ -47,6 +48,7 @@ const PayablesBase = ({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
}: PayablesProps) => {
Expand All @@ -58,6 +60,14 @@ const PayablesBase = ({
open: boolean;
}>({ invoiceId: undefined, open: false });

const closeEditDialog = () => {
setInvoiceIdDialog((prev) => ({
...prev,
open: false,
invoiceId: undefined,
}));
};

const [isCreateInvoiceDialogOpen, setIsCreateInvoiceDialogOpen] =
useState(false);

Expand Down Expand Up @@ -166,27 +176,26 @@ const PayablesBase = ({
className={className + '-Dialog-PayableDetails'}
open={invoiceIdDialog.open}
container={root}
onClose={() => {
setInvoiceIdDialog((prev) => ({ ...prev, open: false }));
}}
onClosed={() => {
setInvoiceIdDialog((prev) =>
prev.open ? prev : { open: false, invoiceId: undefined }
);
}}
onClose={closeEditDialog}
onClosed={closeEditDialog}
fullScreen
>
<PayableDetails
id={invoiceIdDialog.invoiceId}
onClose={() => {
setInvoiceIdDialog((prev) => ({ ...prev, open: false }));
}}
onClose={closeEditDialog}
onSaved={onSaved}
onCanceled={onCanceled}
onSubmitted={onSubmitted}
onRejected={onRejected}
onApproved={onApproved}
onReopened={onReopened}
onDeleted={(payableId) => {
onDeleted?.(payableId);
closeEditDialog();
toast(t(i18n)`Bill #${payableId} has been deleted`, {
duration: 5000,
});
}}
onPay={onPay}
onPayUS={onPayUS}
/>
Expand Down
Loading