Skip to content

Commit

Permalink
feat(edit transaction): not submit without changes
Browse files Browse the repository at this point in the history
  • Loading branch information
uigywnkiub committed Jul 9, 2024
1 parent 58229cb commit ae879ed
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/ui/transaction-form-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ function TransactionFormEdit({ transaction }: TProps) {
Array.from(category)[0]?.toString(),
)
const currency = transaction.currency
const isEdited = transaction.isEdited
const transactionId = transaction.id

const onChangeDescription = (e: React.ChangeEvent<HTMLInputElement>) => {
setDescription(e.target.value)
Expand All @@ -64,19 +66,46 @@ function TransactionFormEdit({ transaction }: TProps) {
}
}

const hasChanges = (
newData: Partial<TTransaction>,
oldData: TTransaction,
) => {
const { amount, ...restOldData } = oldData
const modifiedOldData: TTransaction = {
...restOldData,
amount: getFormattedCurrency(oldData.amount),
}
return Object.keys(newData).some((key) => {
const newKey = key as keyof typeof newData
return (
newData[newKey] !== undefined &&
newData[newKey] !== modifiedOldData[newKey]
)
})
}

const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
setIsLoading(true)

const newTransactionData = {
isIncome: isSwitchedOn,
isEdited: true,
isEdited,
description,
amount,
category: categoryWithEmoji,
currency,
}

if (!hasChanges(newTransactionData, transaction)) {
toast.error('No changes detected.')
setIsLoading(false)
return
}
newTransactionData.isEdited = true

try {
await editTransactionById(transaction.id, newTransactionData)
await editTransactionById(transactionId, newTransactionData)
router.push(ROUTE.HOME)
toast.success('Transaction edited.')
} catch (err) {
Expand Down

0 comments on commit ae879ed

Please sign in to comment.