Skip to content

Commit

Permalink
fix: multiple toast and account balance
Browse files Browse the repository at this point in the history
  • Loading branch information
xDeFc0nx committed Jan 25, 2025
1 parent 6e07b1e commit 99fe94a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 107 deletions.
152 changes: 62 additions & 90 deletions client/src/components/transaction/addTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ import {
const formSchema = z.object({
Type: z.enum(['Income', 'Expense']),
Description: z.string().min(1, 'Description is required'),

Amount: z.number().min(1, 'Amount must be greater than 0'),
IsRecurring: z.boolean(),
});

export const AddTransaction = () => {
const { socket, isReady } = useWebSocket();
const {
transactions,
setTransactions,
activeAccount,
setAccounts,
Expand All @@ -61,97 +59,71 @@ export const AddTransaction = () => {
},
});

const onSubmit = (values: z.infer<typeof formSchema>) => {
console.log('Form Values:', values); // Add this line
try {
if (socket && isReady) {
socket.send('createTransaction', {
AccountID: activeAccount?.AccountID,
Type: values.Type,
Description: values.Description,
Amount: values.Amount,
IsRecurring: values.IsRecurring,
});

socket.onMessage((msg) => {
const response = JSON.parse(msg);

if (response.transaction) {
setTransactions([
...transactions,
{
ID: response.transaction.ID,
UserID: response.transaction.UserID,
AccountID: response.transaction.AccountID,
Type: values.Type,
Description: values.Description,
Amount: values.Amount,
IsRecurring: values.IsRecurring,
CreatedAt: response.transaction.CreatedAt,
},
]);

setAccounts((prevAccounts) =>
prevAccounts.map((account) => {
if (account.AccountID === activeAccount?.AccountID) {
const updatedAccount = {
...account,
AccountBalance: account.AccountBalance + values.Amount,
const onSubmit = (values: z.infer<typeof formSchema>) => {
try {
if (socket && isReady && activeAccount) {
const currentAccountId = activeAccount.AccountID;

socket.send('createTransaction', {
AccountID: currentAccountId,
...values
});

const balanceHandler = (msg: string) => {
const response = JSON.parse(msg);

if (response.transaction) {
setTransactions(prev => [...prev, response.transaction]);
toast.success('Transaction added!',{
toastId: "success"
});
form.reset();
}


if (response.totalIncome !== undefined) {
setAccounts(prev => prev.map(acc =>
acc.AccountID === currentAccountId
? {...acc, Income: response.totalIncome}
: acc
));
}

if (response.totalExpense !== undefined) {
setAccounts(prev => prev.map(acc =>
acc.AccountID === currentAccountId
? {...acc, Expense: response.totalExpense}
: acc
));
}

if (response.accountBalance !== undefined) {
setAccounts(prev => prev.map(acc =>
acc.AccountID === currentAccountId
? {...acc, AccountBalance: response.accountBalance}
: acc
));
setActiveAccount(prev =>
prev?.AccountID === currentAccountId
? {...prev, AccountBalance: response.accountBalance}
: prev
);
}
};

// Update activeAccount if it matches the updated account
if (activeAccount?.AccountID === account.AccountID) {
setActiveAccount(updatedAccount);
}

return updatedAccount;
}
return account;
}),
);

setActiveAccount((prev) => {
if (!prev) return null;
return {
...prev,
AccountBalance: response.transaction.AccountBalance,

};
});
socket.send('getAccountIncome', { AccountID: activeAccount?.ID });

socket.send('getAccountExpense', { AccountID: activeAccount?.ID });
if (response.totalIncome) {
setActiveAccount((prev) =>
prev
? {
...prev,
Income: response.totalIncome,
}
: null,
);
}

if (response.totalExpense) {
setActiveAccount((prev) =>
prev
? {
...prev,
Expense: response.totalExpense,
}
: null,
);
}

toast.success('Transaction added successfully!');
}
});
}
} catch (error) {
console.error('Form submission error', error);
toast.error('Failed to submit the form. Please try again.');
socket.onMessage(balanceHandler);
setTimeout(() => {
socket.send('getAccountIncome', { AccountID: currentAccountId });
socket.send('getAccountExpense', { AccountID: currentAccountId });
socket.send('getAccountBalance', { AccountID: currentAccountId });
}, 100);
return
}
};
} catch (error) {
console.error('Submission error', error);
toast.error('Failed to add transaction');
}
};

return (
<Dialog>
Expand Down
26 changes: 9 additions & 17 deletions handlers/Transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,18 @@ func CreateTransaction(
return
}
}
totalBalance := account.Income - account.Expense
account.Balance = totalBalance

if err := db.DB.Save(account).Error; err != nil {
Send_Error(ws, "Failed to save", err)
return
}

response := map[string]interface{}{
"transaction": map[string]interface{}{
"ID": transaction.ID,
"UserID": transaction.UserID,
"AccountID": transaction.AccountID,
"Type": transaction.Type,
"Amount": transaction.Amount,
"Description": transaction.Description,
"IsRecurring": transaction.IsRecurring,
"Frequency": recurring.Frequency,
"CreatedAt": recurring.CreatedAt.Format(time.RFC3339),
"AccountBalance": account.Balance,
"ID": transaction.ID,
"UserID": transaction.UserID,
"AccountID": transaction.AccountID,
"Type": transaction.Type,
"Amount": transaction.Amount,
"Description": transaction.Description,
"IsRecurring": transaction.IsRecurring,
"Frequency": recurring.Frequency,
"CreatedAt": recurring.CreatedAt.Format(time.RFC3339),
},
}

Expand Down

0 comments on commit 99fe94a

Please sign in to comment.