-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountDetailContainer.tsx
157 lines (145 loc) · 5.04 KB
/
AccountDetailContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { type FC, useCallback, useMemo, useState } from 'react';
import { Stack } from '@chakra-ui/react/stack';
import { useLoaderData, useRevalidator } from '@remix-run/react';
import { useTranslation } from 'react-i18next';
import AccountDetails from '~/components/accounts/AccountDetails';
import FLPButton from '~/components/core/buttons/FLPButton';
import FLPHeading from '~/components/core/typography/FLPHeading';
import type { loader } from '~/routes/app.accounts.$account';
import supabase from '~/utils/supabase';
import { currentYear, emptyObject } from '~/utils/utils';
const AccountDetailContainer: FC = () => {
const { t } = useTranslation();
const { revalidate } = useRevalidator();
const { account, accountDetails } = useLoaderData<typeof loader>();
const [isLoading, setIsLoading] = useState(false);
const [isEditMode, setIsEditMode] = useState(false);
const [editedValues, setEditedValues] = useState<{ [key: string]: { [key: string]: string } }>(emptyObject);
const availableYears = useMemo(() => accountDetails?.map(({ year }: { year?: number }) => year), [accountDetails]);
const handleToggleEditMode = useCallback(() => setIsEditMode((prev) => !prev), []);
const handleInputChange = useCallback(
(event: {
target: {
dataset: DOMStringMap;
value: string;
};
}) => {
setEditedValues((prev) => ({
...prev,
[event.target.dataset.year]: {
...(prev?.[event.target.dataset.year] ?? emptyObject),
[event.target.dataset.month]: event.target.value
}
}));
},
[]
);
const handleAddNewYear = useCallback(
async (selectedYear: number, yearToAdd: 'current' | 'prev' | 'next') => {
setIsLoading(true);
const nextYear = selectedYear + (yearToAdd === 'prev' ? -1 : 1);
const yearWithMonths = Array.from({ length: 12 }, (_, i) => i + 1).map((value) => ({
account_id: account.id,
month: value,
year: yearToAdd === 'current' ? currentYear : nextYear,
value: 0
}));
await supabase.from('account_details').insert(yearWithMonths);
revalidate();
setIsLoading(false);
},
[account.id, revalidate]
);
const handleSaveValues = useCallback(
(event: { preventDefault: () => void }) => {
setIsLoading(true);
event.preventDefault();
const updatedValues = Object.entries(editedValues)
.map(([year, values]) =>
Object.entries(values).map(([month, value]) => ({
account_id: account.id,
month: parseInt(month),
year: parseInt(year),
value: parseInt(value)
}))
)
.flat();
updatedValues.forEach(async (value) => {
await supabase
.from('account_details')
.update(value)
.eq('account_id', account.id)
.eq('month', value.month)
.eq('year', value.year);
});
handleToggleEditMode();
setEditedValues(emptyObject);
revalidate();
setIsLoading(false);
},
[account.id, editedValues, handleToggleEditMode, revalidate]
);
return (
<Stack flexDirection="column" gap={10}>
<Stack flexDirection="row" alignItems="center" justifyContent="space-between">
<Stack flexDirection="column">
<FLPHeading as="h2" size="sm">
{account.type}
</FLPHeading>
<FLPHeading as="h1" size="xl">
{account.name}
</FLPHeading>
</Stack>
<Stack>
<FLPButton
disabled={isLoading}
isLoading={isLoading}
variant="outline"
onClick={isEditMode ? handleSaveValues : handleToggleEditMode}
>
{isEditMode ? t('save') : t('edit')}
</FLPButton>
</Stack>
</Stack>
<AccountDetails onInputChange={handleInputChange} editedValues={editedValues} isEditMode={isEditMode} />
<Stack flexDirection="row">
{availableYears?.length ? (
<>
<FLPButton
colorScheme="green"
disabled={isLoading}
isLoading={isLoading}
size="sm"
variant="outline"
onClick={() => handleAddNewYear(availableYears?.[availableYears.length - 1], 'prev')}
>
{t('addPrevYear')}
</FLPButton>
<FLPButton
colorScheme="green"
disabled={isLoading}
isLoading={isLoading}
size="sm"
variant="outline"
onClick={() => handleAddNewYear(availableYears?.[0], 'next')}
>
{t('addNextYear')}
</FLPButton>
</>
) : (
<FLPButton
colorScheme="green"
disabled={isLoading}
isLoading={isLoading}
size="sm"
variant="outline"
onClick={() => handleAddNewYear(new Date().getFullYear(), 'current')}
>
{t('addCurrentYear')}
</FLPButton>
)}
</Stack>
</Stack>
);
};
export default AccountDetailContainer;