Skip to content

Commit

Permalink
fix: show notification when pay transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
mnindrazaka committed Oct 10, 2024
1 parent 0315b69 commit 1e15199
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
11 changes: 9 additions & 2 deletions libs/ui/src/app/ExpenseListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ import { dehydrate, QueryClient, useQueryClient } from '@tanstack/react-query';
export async function getExpenseListScreenDehydratedState() {
const client = new QueryClient();
await client.prefetchQuery({
queryKey: expenseListQueryKey(),
queryFn: () => expenseList(),
queryKey: expenseListQueryKey({
sortBy: 'created_at',
order: 'desc',
}),
queryFn: () =>
expenseList({
sortBy: 'created_at',
order: 'desc',
}),
});
return dehydrate(client);
}
Expand Down
11 changes: 9 additions & 2 deletions libs/ui/src/app/WalletTransferListScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,15 @@ export async function getWalletTransferListScreenDehydratedState(
queryFn: () => walletFindById(walletId),
}),
client.prefetchQuery({
queryKey: walletTransferListQueryKey(walletId),
queryFn: () => walletTransferList(walletId),
queryKey: walletTransferListQueryKey(walletId, {
sortBy: 'created_at',
order: 'desc',
}),
queryFn: () =>
walletTransferList(walletId, {
sortBy: 'created_at',
order: 'desc',
}),
}),
]);

Expand Down
21 changes: 17 additions & 4 deletions libs/ui/src/data/openApi/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ export class OpenAPIWalletRepository implements WalletRepository {
walletId
) => {
const res = this.client.getQueryState<WalletTransferList200>(
walletTransferListQueryKey(walletId)
walletTransferListQueryKey(walletId, {
sortBy: 'created_at',
order: 'desc',
})
)?.data;

this.client.removeQueries({
queryKey: walletTransferListQueryKey(walletId),
queryKey: walletTransferListQueryKey(walletId, {
sortBy: 'created_at',
order: 'desc',
}),
});

return res?.data.map(transformers.walletTransfer) ?? [];
Expand All @@ -46,8 +52,15 @@ export class OpenAPIWalletRepository implements WalletRepository {
) => {
return this.client
.fetchQuery({
queryKey: walletTransferListQueryKey(walletId),
queryFn: () => walletTransferList(walletId),
queryKey: walletTransferListQueryKey(walletId, {
sortBy: 'created_at',
order: 'desc',
}),
queryFn: () =>
walletTransferList(walletId, {
sortBy: 'created_at',
order: 'desc',
}),
})
.then((data) => data.data.map(transformers.walletTransfer));
};
Expand Down
11 changes: 10 additions & 1 deletion libs/ui/src/domain/usecases/transactionPay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type TransactionPayState = (
| { type: 'shown' }
| { type: 'paying' }
| { type: 'payingSuccess' }
| { type: 'payingError' }
) &
Context;

Expand All @@ -23,7 +24,8 @@ export type TransactionPayAction =
| { type: 'HIDE_CONFIRMATION' }
| { type: 'PAY'; walletId: number }
| { type: 'PAY_SUCCESS' }
| { type: 'PAY_ERROR' };
| { type: 'PAY_ERROR' }
| { type: 'PAY_CANCEL' };

export class TransactionPayUsecase extends Usecase<
TransactionPayState,
Expand Down Expand Up @@ -86,6 +88,10 @@ export class TransactionPayUsecase extends Usecase<
walletId,
}))
.with([{ type: 'paying' }, { type: 'PAY_ERROR' }], ([state]) => ({
...state,
type: 'payingError',
}))
.with([{ type: 'payingError' }, { type: 'PAY_CANCEL' }], ([state]) => ({
...state,
type: 'shown',
}))
Expand Down Expand Up @@ -128,6 +134,9 @@ export class TransactionPayUsecase extends Usecase<
.with({ type: 'payingSuccess' }, () => {
dispatch({ type: 'HIDE_CONFIRMATION' });
})
.with({ type: 'payingError' }, () => {
dispatch({ type: 'PAY_CANCEL' });
})
.otherwise(() => {
// TODO: IMPLEMENT SOMETHING
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import { toFormikValidationSchema } from 'zod-formik-adapter';
import { TransactionPaymentAlertView } from './TransactionPaymentAlert.view';
import { useTransactionPayController } from '../../../../controllers';
import { z } from 'zod';
import { useEffect } from 'react';
import { useToastController } from '@tamagui/toast';

export const TransactionPaymentAlert = () => {
const { state, dispatch } = useTransactionPayController();

const toast = useToastController();
useEffect(() => {
if (state.type === 'payingSuccess') {
toast.show('Payment Success');
} else if (state.type === 'payingError') {
toast.show('Payment Error');
}
}, [state.type, toast]);

const formik = useFormik<{ walletId: number }>({
initialValues: { walletId: NaN },
onSubmit: ({ walletId }) => dispatch({ type: 'PAY', walletId }),
Expand All @@ -16,12 +27,15 @@ export const TransactionPaymentAlert = () => {
});

const isButtonDisabled =
state.type === 'paying' || state.type === 'payingSuccess';
state.type === 'paying' ||
state.type === 'payingSuccess' ||
state.type === 'payingError';

const isOpen =
state.type === 'shown' ||
state.type === 'paying' ||
state.type === 'payingSuccess';
state.type === 'payingSuccess' ||
state.type === 'payingError';

const onCancel = () => dispatch({ type: 'HIDE_CONFIRMATION' });

Expand Down

0 comments on commit 1e15199

Please sign in to comment.