-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement post message in expense domain
- Loading branch information
1 parent
3f849a9
commit db72c21
Showing
13 changed files
with
177 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 23 additions & 17 deletions
40
libs/ui/src/expenses/components/ExpenseDeleteAlert/ExpenseDeleteAlert.state.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
// eslint-disable-next-line @nx/enforce-module-boundaries | ||
import { | ||
useExpenseDeleteById, | ||
useExpenseFindById, | ||
} from '../../../../../api-contract/src'; | ||
import { useExpenseDeleteById } from '../../../../../api-contract/src'; | ||
import { useCallback, useState } from 'react'; | ||
import { useToastController } from '@tamagui/toast'; | ||
import { PostMessageEvent, usePostMessage } from '../../../base'; | ||
|
||
export type UseExpenseDeleteAlertStateProps = { | ||
expenseId: number; | ||
onSuccess: () => void; | ||
}; | ||
export const useExpenseDeleteAlertState = () => { | ||
const [expenseId, setExpenseId] = useState<number>(); | ||
const { status, mutateAsync } = useExpenseDeleteById(expenseId ?? NaN); | ||
|
||
const onReceiveMessage = useCallback((event: PostMessageEvent) => { | ||
if (event.type === 'ExpenseDeleteConfirmation') { | ||
setExpenseId(event.expenseId); | ||
} | ||
}, []); | ||
|
||
export const useExpenseDeleteAlertState = ({ | ||
expenseId, | ||
onSuccess, | ||
}: UseExpenseDeleteAlertStateProps) => { | ||
const { status, mutateAsync } = useExpenseDeleteById(expenseId); | ||
const { data } = useExpenseFindById(expenseId); | ||
const { postMessage } = usePostMessage(onReceiveMessage); | ||
|
||
const toast = useToastController(); | ||
|
||
const onButtonConfirmPress = () => { | ||
mutateAsync({}) | ||
.then(() => toast.show('Expense deleted successfully')) | ||
.then(onSuccess) | ||
.then(() => { | ||
toast.show('Expense deleted successfully'); | ||
postMessage({ type: 'ExpenseDeleteSuccess' }); | ||
setExpenseId(undefined); | ||
}) | ||
.catch(() => toast.show('Failed to delete expense')); | ||
}; | ||
|
||
return { status, onButtonConfirmPress }; | ||
const isOpen = typeof expenseId === 'number'; | ||
|
||
const onCancel = () => setExpenseId(undefined); | ||
|
||
return { status, onButtonConfirmPress, isOpen, onCancel }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 31 additions & 1 deletion
32
libs/ui/src/expenses/components/ExpenseList/ExpenseList.state.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
import { useRouter } from 'solito/router'; | ||
// eslint-disable-next-line @nx/enforce-module-boundaries | ||
import { useExpenseList } from '../../../../../api-contract/src'; | ||
import { Expense, useExpenseList } from '../../../../../api-contract/src'; | ||
import { PostMessageEvent, usePostMessage } from '../../../base'; | ||
import { useCallback } from 'react'; | ||
|
||
export const useExpenseListState = () => { | ||
const router = useRouter(); | ||
|
||
const { data, status, error, refetch } = useExpenseList({ | ||
sortBy: 'created_at', | ||
order: 'desc', | ||
}); | ||
|
||
const onReceiveMessage = useCallback( | ||
(event: PostMessageEvent) => { | ||
if (event.type === 'ExpenseDeleteSuccess') { | ||
refetch(); | ||
} | ||
}, | ||
[refetch] | ||
); | ||
|
||
const { postMessage } = usePostMessage(onReceiveMessage); | ||
|
||
const onDeleteMenuPress = (expense: Expense) => { | ||
postMessage({ | ||
type: 'ExpenseDeleteConfirmation', | ||
expenseId: expense.id, | ||
}); | ||
}; | ||
|
||
const onEditMenuPress = (expense: Expense) => { | ||
router.push(`/expenses/${expense.id}`); | ||
}; | ||
|
||
return { | ||
expenses: data?.data ?? [], | ||
status, | ||
error, | ||
refetch, | ||
onDeleteMenuPress, | ||
onEditMenuPress, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
libs/ui/src/expenses/components/ExpenseListItem/ExpenseListItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Calendar, Clock, Pencil, Trash, Wallet } from '@tamagui/lucide-icons'; | ||
import { ListItem } from '../../../base'; | ||
import dayjs from 'dayjs'; | ||
import { XStackProps } from 'tamagui'; | ||
|
||
export type ExpenseListItemProps = { | ||
budgetName: string; | ||
total: number; | ||
createdAt: string; | ||
walletName: string; | ||
onEditMenuPress?: () => void; | ||
onDeleteMenuPress?: () => void; | ||
} & XStackProps; | ||
|
||
export const ExpenseListItem = ({ | ||
budgetName, | ||
createdAt, | ||
total, | ||
walletName, | ||
onEditMenuPress, | ||
onDeleteMenuPress, | ||
...xStackProps | ||
}: ExpenseListItemProps) => { | ||
return ( | ||
<ListItem | ||
title={budgetName} | ||
subtitle={`Rp. ${total.toLocaleString('id')}`} | ||
menus={[ | ||
{ | ||
title: 'Edit', | ||
icon: Pencil, | ||
onPress: onEditMenuPress, | ||
isShown: typeof onEditMenuPress === 'function', | ||
}, | ||
{ | ||
title: 'Delete', | ||
icon: Trash, | ||
onPress: onDeleteMenuPress, | ||
isShown: typeof onDeleteMenuPress === 'function', | ||
}, | ||
]} | ||
footerItems={[ | ||
{ | ||
icon: Calendar, | ||
value: dayjs(createdAt).format('DD/MM/YYYY'), | ||
}, | ||
{ | ||
icon: Clock, | ||
value: dayjs(createdAt).format('HH:mm'), | ||
}, | ||
{ | ||
icon: Wallet, | ||
value: walletName, | ||
}, | ||
]} | ||
{...xStackProps} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ExpenseListItem' |
57 changes: 0 additions & 57 deletions
57
libs/ui/src/expenses/screens/ExpenseListScreen/ExpenseListScreen.state.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.