-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReportingPeriods.tsx
148 lines (131 loc) · 3.92 KB
/
ReportingPeriods.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
import { useState, useMemo, useCallback } from 'react'
import Button from 'react-bootstrap/Button'
import Modal from 'react-bootstrap/Modal'
import { useAuth } from 'web/src/auth'
import { useMutation } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'
import { QUERY } from 'src/components/ReportingPeriod/ReportingPeriodsCell/ReportingPeriodsCell'
import TableBuilder from 'src/components/TableBuilder/TableBuilder'
import { formatDateString } from 'src/utils'
import { columnDefs } from './columns'
const CERTIFY_REPORTING_PERIOD_MUTATION = gql`
mutation CertifyReportingPeriodAndOpenNextPeriod($id: Int!) {
certifyReportingPeriodAndOpenNextPeriod(reportingPeriodId: $id) {
id
}
}
`
const ReportingPeriodsList = ({
reportingPeriods,
organizationOfCurrentUser,
}) => {
const { hasRole } = useAuth()
const isUSDRAdmin = hasRole('USDR_ADMIN')
const [showCertificationModal, setShowCertificationModal] = useState(false)
const currentReportingPeriod = useMemo(
() =>
reportingPeriods.find(
(period) =>
period.id ===
organizationOfCurrentUser.preferences.current_reporting_period_id
),
[reportingPeriods, organizationOfCurrentUser]
)
const [certifyReportingPeriod] = useMutation(
CERTIFY_REPORTING_PERIOD_MUTATION,
{
onCompleted: () => {
toast.success('Reporting period certified')
setShowCertificationModal(false)
},
onError: (error) => {
console.error(error)
toast.error('Unable to certify the current reporting period.')
},
refetchQueries: [{ query: QUERY }],
}
)
const handleCertify = useCallback(() => {
if (currentReportingPeriod?.id) {
certifyReportingPeriod({ variables: { id: currentReportingPeriod.id } })
}
}, [currentReportingPeriod, certifyReportingPeriod])
const canEdit = useCallback(
(reportingPeriod) => {
return (
isUSDRAdmin &&
(reportingPeriod.id === currentReportingPeriod?.id ||
new Date(reportingPeriod.startDate) >
new Date(currentReportingPeriod?.endDate))
)
},
[isUSDRAdmin, currentReportingPeriod]
)
const certificationDisplay = useCallback(
(reportingPeriod) => {
if (reportingPeriod.id === currentReportingPeriod?.id) {
return (
<Button
onClick={() => setShowCertificationModal(true)}
variant="primary"
size="sm"
>
Certify Reporting Period
</Button>
)
}
const certification = reportingPeriod.certificationForOrganization
if (certification) {
return `${formatDateString(certification.createdAt)} by ${
certification.certifiedBy.email
}`
}
return ''
},
[currentReportingPeriod, setShowCertificationModal]
)
const columns = useMemo(
() =>
columnDefs({
certificationDisplay,
canEdit,
isUSDRAdmin,
}),
[certificationDisplay, canEdit, isUSDRAdmin]
)
return (
<>
<Modal
show={showCertificationModal}
onHide={() => setShowCertificationModal(false)}
centered
>
<Modal.Header closeButton>
<Modal.Title>Certify Reporting Period</Modal.Title>
</Modal.Header>
<Modal.Body>
Certify the{' '}
<span className="fw-bold">{currentReportingPeriod?.name}</span>{' '}
period?
</Modal.Body>
<Modal.Footer>
<Button variant="primary" onClick={handleCertify}>
Certify
</Button>
<Button
variant="outline-secondary"
onClick={() => setShowCertificationModal(false)}
>
Cancel
</Button>
</Modal.Footer>
</Modal>
<TableBuilder
data={reportingPeriods}
columns={columns}
filterableInputs={['name']}
/>
</>
)
}
export default ReportingPeriodsList