Skip to content

Commit

Permalink
Merge branch 'main' into dev-13777-backup-pre-rebase-2
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinafrombr authored Feb 17, 2025
2 parents 7c1c731 + 0c0892d commit 4667a03
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 73 deletions.
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 @@ -9,6 +9,7 @@ import {
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from 'recharts';

import { useMoniteContext } from '@monite/sdk-react';
Expand Down Expand Up @@ -180,6 +181,19 @@ export const CashFlowCard = () => {
}}
/>

<YAxis
tickLine={false}
axisLine={false}
tickFormatter={(value) => {
return new Intl.NumberFormat('en-US', {
notation: 'compact',
compactDisplay: 'short',
style: 'currency',
currency: 'USD',
}).format(Number(value) / 100);
}}
/>

<ReferenceLine y={0} stroke="#eee" />
<Bar dataKey="paid" fill="#ece2f4" />
<Bar dataKey="received" fill="#d2f2ec" />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"lint-staged": "~13.0.2",
"prettier": "^2.8.8",
"pretty-quick": "^3.1.3",
"turbo": "~2.4.0",
"turbo": "~2.4.2",
"typescript": "~5.5.4"
},
"resolutions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const PayableDetailsBase = ({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
}: PayablesDetailsProps) => {
Expand All @@ -69,6 +70,7 @@ const PayableDetailsBase = ({
approveInvoice,
cancelInvoice,
reopenInvoice,
deleteInvoice,
isPaymentLinkAvailable,
modalComponent,
},
Expand All @@ -80,6 +82,7 @@ const PayableDetailsBase = ({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
});
Expand Down Expand Up @@ -147,6 +150,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 @@ -34,6 +34,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 @@ -53,6 +54,7 @@ export const PayableDetailsHeader = ({
approveInvoice,
cancelInvoice,
reopenInvoice,
deleteInvoice,
payInvoice,
payableDetailsFormId,
isPaymentLinkAvailable,
Expand Down Expand Up @@ -115,6 +117,15 @@ export const PayableDetailsHeader = ({
onClick: () => setShowCancelationModal(true),
children: t(i18n)`Cancel bill`,
},
delete: {
variant: 'text',
color: 'error',
onClick: () => {
deleteInvoice();
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 @@ -87,6 +88,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 @@ -114,6 +123,7 @@ export function usePayableDetails({
onRejected,
onApproved,
onReopened,
onDeleted,
onPay,
onPayUS,
}: UsePayableDetailsProps) {
Expand Down Expand Up @@ -210,6 +220,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 @@ -368,6 +384,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 @@ -468,6 +492,13 @@ export function usePayableDetails({
}
break;
}

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

setIsPermissionsLoading(false);
Expand All @@ -480,6 +511,7 @@ export function usePayableDetails({
isSubmitAvailable,
isUpdatesAvailable,
isReopenAvailable,
isDeleteAvailable,
status,
payableId,
payable?.amount_to_pay,
Expand Down Expand Up @@ -756,6 +788,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 @@ -788,6 +835,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

0 comments on commit 4667a03

Please sign in to comment.