Skip to content

Commit

Permalink
chore: formating
Browse files Browse the repository at this point in the history
  • Loading branch information
xDeFc0nx committed Jan 25, 2025
1 parent f87068f commit 9b54dee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 77 deletions.
133 changes: 67 additions & 66 deletions client/src/components/transaction/addTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ const formSchema = z.object({

export const AddTransaction = () => {
const { socket, isReady } = useWebSocket();
const {
setTransactions,
activeAccount,
setAccounts,
setActiveAccount,
} = useUserData();
const { setTransactions, activeAccount, setAccounts, setActiveAccount } =
useUserData();

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
Expand All @@ -59,71 +55,76 @@ export const AddTransaction = () => {
},
});

const onSubmit = (values: z.infer<typeof formSchema>) => {
try {
if (socket && isReady && activeAccount) {
const currentAccountId = activeAccount.AccountID;
const onSubmit = (values: z.infer<typeof formSchema>) => {
try {
if (socket && isReady && activeAccount) {
const currentAccountId = activeAccount.AccountID;

socket.send('createTransaction', {
AccountID: currentAccountId,
...values
});
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();
}
const balanceHandler = (msg: string) => {
const response = JSON.parse(msg);


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
);
}
};
if (response.transaction) {
setTransactions((prev) => [...prev, response.transaction]);
toast.success('Transaction added!', {
toastId: 'success',
});
form.reset();
}

socket.onMessage(balanceHandler);
setTimeout(() => {
socket.send('getAccountIncome', { AccountID: currentAccountId });
socket.send('getAccountExpense', { AccountID: currentAccountId });
socket.send('getAccountBalance', { AccountID: currentAccountId });
}, 100);
return
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,
);
}
};

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');
}
} catch (error) {
console.error('Submission error', error);
toast.error('Failed to add transaction');
}
};
};

return (
<Dialog>
Expand Down
9 changes: 1 addition & 8 deletions client/src/components/transaction/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,11 @@ export const columns: ColumnDef<Transaction>[] = [
accessorKey: 'Description',
header: 'Description',
},
{
{
accessorKey: 'type',
header: () => <div className="text-right">Type</div>,
cell: ({ row }) => <div className="text-right">{row.original.Type}</div>,
},

{
accessorKey: 'amount',
header: () => <div className="text-right">Amount</div>,
cell: ({ row }) => <div className="text-right">{row.original.Amount}</div>,
},

{
id: 'actions',
enableHiding: false,
Expand Down
12 changes: 9 additions & 3 deletions client/src/pages/(secure)/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import {
import { useUserData, type Transaction } from '@/components/context/userData';

export const columns: ColumnDef<Transaction>[] = [

{
accessorKey: 'Description',
header: 'Description',
},
{
{
accessorKey: 'type',
header: () => <div className="text-right">Type</div>,
cell: ({ row }) => <div className="text-right">{row.original.Type}</div>,
Expand All @@ -30,7 +29,14 @@ export const columns: ColumnDef<Transaction>[] = [
{
accessorKey: 'amount',
header: () => <div className="text-right">Amount</div>,
cell: ({ row }) => <div className="text-right">{row.original.Amount}</div>,
cell: ({ row }) => {
const amount = row.original.Amount;
const type = row.original.Type;

const formattedAmount = type === 'Income' ? `+${amount}` : `-${amount}`;

return <div className="text-right">{formattedAmount}</div>;
},
},
{
id: 'actions',
Expand Down

0 comments on commit 9b54dee

Please sign in to comment.