diff --git a/client/src/components/context/userData.tsx b/client/src/components/context/userData.tsx index 04a419e..2f9b775 100644 --- a/client/src/components/context/userData.tsx +++ b/client/src/components/context/userData.tsx @@ -16,6 +16,7 @@ export interface Account { Expense: number; AccountBalance: number; Type: string; + DateRange: string; } export interface Transaction { ID: string; diff --git a/client/src/components/sidebar/account-switcher.tsx b/client/src/components/sidebar/account-switcher.tsx index 5545233..96dc043 100644 --- a/client/src/components/sidebar/account-switcher.tsx +++ b/client/src/components/sidebar/account-switcher.tsx @@ -112,6 +112,7 @@ export function AccountSwitcher() { if (socket && isReady && activeAccount?.AccountID) { socket.send('getTransactions', { AccountID: activeAccount?.AccountID, + DateRange: activeAccount?.DateRange, }); socket.onMessage((msg) => { diff --git a/client/src/components/transaction/columns.tsx b/client/src/components/transaction/columns.tsx index c6b029f..605a3ce 100644 --- a/client/src/components/transaction/columns.tsx +++ b/client/src/components/transaction/columns.tsx @@ -67,25 +67,27 @@ export const columns: ColumnDef[] = [ cell: ({ row }) =>
{row.original.Type}
, }, - { - accessorKey: 'amount', - header: () =>
Amount
, - cell: ({ row }) => { - const amount = row.original.Amount; - const type = row.original.Type; - - // Add + for Income, - for Expense - const formattedAmount = type === 'Income' ? `+${amount}` : `-${amount}`; - - const backgroundColor = type === 'Income' ? 'bg-green-400/50' : 'bg-red-400/50'; - - return ( -
- {formattedAmount} -
- ); + { + accessorKey: 'amount', + header: () =>
Amount
, + cell: ({ row }) => { + const amount = row.original.Amount; + const type = row.original.Type; + + // Add + for Income, - for Expense + const formattedAmount = type === 'Income' ? `+${amount}` : `-${amount}`; + + const backgroundColor = + type === 'Income' ? 'bg-green-400/50' : 'bg-red-400/50'; + + return ( +
+ {formattedAmount} +
+ ); + }, }, -}, { + { id: 'actions', enableHiding: false, cell: ({ row }) => { diff --git a/client/src/pages/(secure)/columns.tsx b/client/src/pages/(secure)/columns.tsx index 75651ac..54c6ffc 100644 --- a/client/src/pages/(secure)/columns.tsx +++ b/client/src/pages/(secure)/columns.tsx @@ -27,24 +27,25 @@ export const columns: ColumnDef[] = [ }, { - accessorKey: 'amount', - header: () =>
Amount
, - cell: ({ row }) => { - const amount = row.original.Amount; - const type = row.original.Type; - - // Add + for Income, - for Expense - const formattedAmount = type === 'Income' ? `+${amount}` : `-${amount}`; - - const backgroundColor = type === 'Income' ? 'bg-green-400/50' : 'bg-red-400/50'; - - return ( -
- {formattedAmount} -
- ); + accessorKey: 'amount', + header: () =>
Amount
, + cell: ({ row }) => { + const amount = row.original.Amount; + const type = row.original.Type; + + // Add + for Income, - for Expense + const formattedAmount = type === 'Income' ? `+${amount}` : `-${amount}`; + + const backgroundColor = + type === 'Income' ? 'bg-green-400/50' : 'bg-red-400/50'; + + return ( +
+ {formattedAmount} +
+ ); + }, }, -}, { id: 'actions', enableHiding: false, diff --git a/client/src/pages/(secure)/index.tsx b/client/src/pages/(secure)/index.tsx index d9b0ad8..48be808 100644 --- a/client/src/pages/(secure)/index.tsx +++ b/client/src/pages/(secure)/index.tsx @@ -6,14 +6,111 @@ import { BreadcrumbPage, BreadcrumbSeparator, } from '@/components/ui/breadcrumb'; - +import { + Select, + SelectTrigger, + SelectValue, + SelectContent, + SelectItem, +} from '@/components/ui/select'; import { Separator } from '@/components/ui/separator'; import { SidebarInset, SidebarTrigger } from '@/components/ui/sidebar'; import { DataTable } from './dataTable'; import { columns } from './columns'; import { BalanceChart } from '@/components/charts/balanceChart'; +import { useWebSocket } from '@/components/WebSocketProvidor'; +import { useState } from 'react'; + export default function Index() { - const { activeAccount, transactions } = useUserData(); + const { setTransactions,transactions, activeAccount, setAccounts, setActiveAccount } = + useUserData(); + + const [dateRange, setDateRange] = useState('this_month'); + + const { socket } = useWebSocket(); + + const handleDateRangeChange = (value: string) => { + setDateRange(value); + + const currentAccountId = activeAccount?.AccountID; + if (socket && activeAccount) { + const updatedAccount = { ...activeAccount, DateRange: value }; + setActiveAccount(updatedAccount); + + socket.send('getTransactions', { + AccountID: activeAccount.AccountID, + DateRange: value, + }); + socket.send('getAccountIncome', { + AccountID: activeAccount.AccountID, + DateRange: value, + }); + socket.send('getAccountExpense', { + AccountID: activeAccount.AccountID, + DateRange: value, + }); + socket.send('getAccountBalance', { + AccountID: activeAccount.AccountID, + DateRange: value, + }); + + const messageHandler = (msg: string) => { + const response = JSON.parse(msg); + + if (response.transactions) { + setTransactions(response.transactions); + } + + if (response.totalIncome !== undefined) { + setAccounts((prev) => + prev.map((acc) => + acc.AccountID === currentAccountId + ? { ...acc, Income: response.totalIncome } + : acc, + ), + ); + setActiveAccount((prev) => + prev && prev.AccountID === currentAccountId + ? { ...prev, Income: response.totalIncome } + : prev, + ); + } + + if (response.totalExpense !== undefined) { + setAccounts((prev) => + prev.map((acc) => + acc.AccountID === currentAccountId + ? { ...acc, Expense: response.totalExpense } + : acc, + ), + ); + setActiveAccount((prev) => + prev && prev.AccountID === currentAccountId + ? { ...prev, Expense: response.totalExpense } + : prev, + ); + } + + if (response.accountBalance !== undefined) { + setAccounts((prev) => + prev.map((acc) => + acc.AccountID === currentAccountId + ? { ...acc, AccountBalance: response.accountBalance } + : acc, + ), + ); + setActiveAccount((prev) => + prev && prev.AccountID === currentAccountId + ? { ...prev, AccountBalance: response.accountBalance } + : prev, + ); + } + }; + + socket.onMessage(messageHandler); + } + }; + return (
@@ -29,6 +126,23 @@ export default function Index() { +
+ +
@@ -38,33 +152,32 @@ export default function Index() {
- {' '} {activeAccount?.AccountBalance}
- +
-

Total Income

+

+ Total Income +

-
- {' '} - {activeAccount?.Income} -
+
{activeAccount?.Income}

+20.1% from last month

-
+
-

Total Expenses

+

+ Total Expenses +

- {' '} {activeAccount?.Expense}

@@ -75,8 +188,7 @@ export default function Index() {

-
- +