-
Notifications
You must be signed in to change notification settings - Fork 2
/
EnvelopeMonth.tsx
199 lines (187 loc) · 6.07 KB
/
EnvelopeMonth.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Link } from 'react-router-dom'
import { PencilIcon } from '@heroicons/react/24/solid'
import { useTranslation } from 'react-i18next'
import { useState } from 'react'
import { api } from '../lib/api/base'
import { formatMoney } from '../lib/format'
import {
Budget,
EnvelopeMonth as EnvelopeMonthType,
UUID,
Translation,
} from '../types'
import Modal from './Modal'
import { translatedMonthFormat } from '../lib/dates'
import AllocationInputs from './AllocationInputs'
type props = {
envelope: EnvelopeMonthType
i: number
budget: Budget
editingEnvelope?: UUID
editEnvelope: (id?: UUID) => void
reloadBudgetMonth: () => void
setError: (message: string) => void
}
const allocationApi = api('allocation')
const EnvelopeMonth = ({
envelope,
i,
budget,
editingEnvelope,
editEnvelope,
reloadBudgetMonth,
setError,
}: props) => {
const { t }: Translation = useTranslation()
const [allocatedAmount, setAllocatedAmount] = useState(envelope.allocation)
const closeInput = () => {
editEnvelope(undefined)
}
const updateAllocation = async () => {
if (Number(allocatedAmount) === 0) {
return allocationApi.delete(undefined, { url: envelope.links.allocation })
} else {
return allocationApi.update(
{ amount: allocatedAmount },
envelope.links.allocation
)
}
}
const createAllocation = async () => {
return allocationApi.create(
{
amount: allocatedAmount,
envelopeId: envelope.id,
month: envelope.month,
},
budget,
envelope.links.allocation
)
}
const month = translatedMonthFormat.format(new Date(envelope.month))
return (
<tr
className={`hover:bg-gray-50 dark:hover:bg-slate-700 border-t ${
i === 0
? 'border-gray-300 dark:border-black'
: 'border-gray-200 dark:border-gray-900'
}`}
>
<td>
<Link
to={`/envelopes/${envelope.id}`}
className="h-full block whitespace-nowrap py-4 pl-4 pr-1 text-sm font-medium text-gray-900 dark:text-gray-300 sm:pl-6 overflow-hidden text-ellipsis"
>
{envelope.name}
</Link>
</td>
<td
className={`whitespace-nowrap px-1 py-4 text-sm text-right ${
Number(allocatedAmount) < 0
? 'negative'
: 'text-gray-500 dark:text-gray-400'
}`}
>
<Modal
open={editingEnvelope === envelope.id}
setOpen={open => (open ? editEnvelope(envelope.id) : closeInput())}
>
<form
onSubmit={e => {
e.preventDefault()
const response =
Number(envelope.allocation) === 0
? createAllocation()
: updateAllocation()
response.then(closeInput).then(reloadBudgetMonth).catch(setError)
}}
onReset={e => {
e.preventDefault()
setAllocatedAmount(envelope.allocation)
closeInput()
}}
>
<div>
<div className="mt-3 text-center sm:mt-5">
<h3 className="text-lg font-medium leading-6">
{t('editObject', {
object: t('dashboard.allocationForEnvelope', {
envelope: envelope.name,
}),
})}
</h3>
<div className="mt-2">
<AllocationInputs
budget={budget}
savedAllocation={envelope.allocation}
newAllocation={allocatedAmount}
updateNewAllocation={setAllocatedAmount}
/>
</div>
</div>
</div>
<div className="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2 sm:gap-3">
<button
type="submit"
className="inline-flex w-full justify-center rounded-md border border-transparent bg-sky-500 dark:bg-sky-600 px-4 py-2 text-base font-medium text-white shadow-sm hover:bg-sky-600 dark:hover:bg-sky-500 sm:col-start-2 sm:text-sm"
>
{t('save')}
</button>
<button
type="reset"
className="mt-3 inline-flex w-full justify-center rounded-md border border-gray-300 dark:border-gray-700 bg-white dark:bg-slate-800 px-4 py-2 text-base font-medium text-gray-700 dark:text-gray-400 shadow-sm hover:bg-gray-50 dark:hover:bg-slate-700 sm:col-start-1 sm:mt-0 sm:text-sm"
>
{t('cancel')}
</button>
</div>
</form>
</Modal>
<div onClick={() => editEnvelope(envelope.id)}>
<span className="pr-1">
{formatMoney(envelope.allocation, budget.currency, {
signDisplay: 'auto',
})}
</span>
<button
aria-label={t('editObject', {
object: t('dashboard.allocationForEnvelopeMonth', {
envelope: envelope.name,
month: month,
}),
})}
>
<PencilIcon className="inline icon-xs text-gray-400" />
</button>
</div>
</td>
<td
className={`hidden md:table-cell whitespace-nowrap px-1 py-4 text-sm text-right ${
Number(envelope.spent) > 0
? 'positive'
: 'text-gray-500 dark:text-gray-400'
}`}
>
<Link to={`/transactions?envelope=${envelope.id}`}>
{formatMoney(envelope.spent, budget.currency, {
hideZero: true,
})}
</Link>
</td>
<td
className={`whitespace-nowrap pl-1 pr-4 sm:pr-6 py-4 text-sm text-right ${
Number(envelope.balance) < 0
? 'negative'
: 'text-gray-500 dark:text-gray-400'
}`}
>
<Link to={`/transactions?envelope=${envelope.id}`}>
{formatMoney(envelope.balance, budget.currency, {
hideZero: true,
signDisplay: 'auto',
})}
</Link>
</td>
</tr>
)
}
export default EnvelopeMonth