Skip to content

Commit

Permalink
feat: update recurred payment
Browse files Browse the repository at this point in the history
  • Loading branch information
paolojulian committed Jul 28, 2024
1 parent fdc7b90 commit 2e80e3a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Button from '@/components/common/Button';
import Stack from '@/components/common/Stack';
import { Expense } from '@/hooks/services/expense/expense.types';
import { useUpdateExpense } from '@/hooks/services/expense/useUpdateExpense';
import { Formik } from 'formik';
import { FC } from 'react';
import { Alert } from 'react-native';
import {
RECCURED_PAYMENT_MODAL_SCHEMA,
RecurredPaymentModalValues,
Expand All @@ -13,20 +15,36 @@ import {
} from './ReccuredPaymentModalForm.style';

type ReccuredPaymentModalFormProps = {
onDismiss: () => void;
expense: Expense;
};

const ReccuredPaymentModalForm: FC<ReccuredPaymentModalFormProps> = ({ expense }) => {
const ReccuredPaymentModalForm: FC<ReccuredPaymentModalFormProps> = ({ onDismiss, expense }) => {
const { mutateAsync: updateExpenseMutate } = useUpdateExpense(expense.id);

const handleSubmit = async (values: RecurredPaymentModalValues) => {
try {
await updateExpenseMutate({
amount: values.amount,
description: values.description,
});
onDismiss();
Alert.alert('Success', 'Expense updated successfully');
} catch {
Alert.alert('Error', 'Failed to update expense');
}
};

return (
<Formik<RecurredPaymentModalValues>
onSubmit={() => {}}
onSubmit={handleSubmit}
initialValues={{
amount: expense?.amount,
description: expense?.description,
}}
validationSchema={RECCURED_PAYMENT_MODAL_SCHEMA}
>
{({ handleBlur, handleChange, values }) => (
{({ handleBlur, handleChange, handleSubmit, values }) => (
<Stack style={{ flexGrow: 1, gap: 24, paddingVertical: 24 }}>
<Stack style={{ gap: 8, flexGrow: 1 }}>
<RecurredPaymentAmountField
Expand All @@ -40,7 +58,7 @@ const ReccuredPaymentModalForm: FC<ReccuredPaymentModalFormProps> = ({ expense }
value={values.description}
/>
</Stack>
<Button text="Update" onPress={() => {}} />
<Button text="Update" onPress={() => handleSubmit()} />
</Stack>
)}
</Formik>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const RecurredPaymenModal: FC<RecurredPaymentModalProps> = ({ onClose, isOpen, e
{expense ? (
<RecurredPaymentModalContainer>
<RecurredPaymentModalHeader onDeleteDone={onClose} expense={expense} />
<ReccuredPaymentModalForm expense={expense} />
<ReccuredPaymentModalForm onDismiss={onClose} expense={expense} />
</RecurredPaymentModalContainer>
) : (
<></>
Expand Down
7 changes: 6 additions & 1 deletion hooks/services/expense/useExpenseRecurredPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export function useExpenseRecurredPayments(id: number, filterDate: Date) {
};

return useQuery({
queryKey: [ExpenseQueryKeys.recurrenceItems, id, dayjs(filterDate).format('YYYY-MM-DD')],
queryKey: [
ExpenseQueryKeys.expense,
ExpenseQueryKeys.recurrenceItems,
id,
dayjs(filterDate).format('YYYY-MM-DD'),
],
queryFn,
});
}
Expand Down
2 changes: 2 additions & 0 deletions hooks/services/expense/useUpdateExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export function useUpdateExpense(id: Expense['id']) {
}

const queriesToCancel: string[] = [
ExpenseQueryKeys.expense,
ExpenseQueryKeys.item,
ExpenseQueryKeys.list,
ExpenseQueryKeys.recurringExpenses,
BudgetQueryKeys.budget,
Expand Down

0 comments on commit 2e80e3a

Please sign in to comment.