Skip to content

Commit a3ee0a5

Browse files
committed
feat: implement period type toggling in PeriodTypes component
1 parent 14ecb47 commit a3ee0a5

File tree

1 file changed

+86
-19
lines changed

1 file changed

+86
-19
lines changed

src/period-types/PeriodTypes.component.jsx

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { useDataQuery } from '@dhis2/app-runtime'
22
import i18n from '@dhis2/d2-i18n'
33
import { CenteredContent, CircularLoader } from '@dhis2/ui'
44
import CheckboxMaterial from 'material-ui/Checkbox'
5-
import React from 'react'
5+
import React, { useState, useCallback } from 'react'
6+
import { getInstance as getD2 } from 'd2'
7+
import settingsActions from '../settingsActions.js'
68
import styles from './PeriodTypes.module.css'
79

810
const query = {
@@ -132,7 +134,58 @@ const groupByFrequency = (periodTypes) => {
132134
}
133135

134136
const PeriodTypes = () => {
135-
const { loading, error, data } = useDataQuery(query)
137+
const { loading, error, data, refetch } = useDataQuery(query)
138+
const [updating, setUpdating] = useState(false)
139+
140+
const handlePeriodTypeToggle = useCallback(
141+
async (periodTypeName, isCurrentlyEnabled) => {
142+
setUpdating(true)
143+
try {
144+
const d2 = await getD2()
145+
const api = d2.Api.getApi()
146+
const allPeriodTypes = data?.periodTypes?.periodTypes || []
147+
const allowedPeriodTypes = data?.dataOutputPeriodTypes || []
148+
// Handle both array of strings and array of objects with name property
149+
const currentAllowedSet = new Set(
150+
allowedPeriodTypes.map((pt) =>
151+
typeof pt === 'string' ? pt : pt.name
152+
)
153+
)
154+
155+
// Update the set based on the toggle
156+
if (isCurrentlyEnabled) {
157+
currentAllowedSet.delete(periodTypeName)
158+
} else {
159+
currentAllowedSet.add(periodTypeName)
160+
}
161+
162+
// Convert set to array of objects with name property
163+
// The API expects: [{name: "Monthly"}, {name: "Quarterly"}, ...]
164+
const updatedPeriodTypes = Array.from(currentAllowedSet).map((name) => ({
165+
name,
166+
}))
167+
168+
// POST to the configuration endpoint
169+
await api.post('configuration/dataOutputPeriodTypes', updatedPeriodTypes)
170+
171+
// Refetch the data to get the updated state
172+
await refetch()
173+
settingsActions.showSnackbarMessage(
174+
i18n.t('Settings updated')
175+
)
176+
} catch (err) {
177+
console.error('Failed to update period types:', err)
178+
settingsActions.showSnackbarMessage(
179+
i18n.t(
180+
'There was a problem updating settings. Changes have not been saved.'
181+
)
182+
)
183+
} finally {
184+
setUpdating(false)
185+
}
186+
},
187+
[data, refetch]
188+
)
136189

137190
if (loading) {
138191
return (
@@ -157,7 +210,12 @@ const PeriodTypes = () => {
157210
const allowedPeriodTypes = data?.dataOutputPeriodTypes || []
158211

159212
// Create a Set of allowed period type names for quick lookup
160-
const allowedSet = new Set(allowedPeriodTypes.map((pt) => pt.name))
213+
// Handle both array of strings and array of objects with name property
214+
const allowedSet = new Set(
215+
allowedPeriodTypes.map((pt) =>
216+
typeof pt === 'string' ? pt : pt.name
217+
)
218+
)
161219

162220
// Group period types by frequency
163221
const groupedPeriodTypes = groupByFrequency(allPeriodTypes)
@@ -172,22 +230,31 @@ const PeriodTypes = () => {
172230
<div key={group.frequencyOrder} className={styles.group}>
173231
<p className={styles.groupLabel}>{group.label}</p>
174232
<div className={styles.checkboxList}>
175-
{group.periodTypes.map((periodType) => (
176-
<div
177-
key={periodType.name}
178-
className={styles.checkboxItem}
179-
>
180-
<CheckboxMaterial
181-
checked={allowedSet.has(
182-
periodType.name
183-
)}
184-
label={formatPeriodTypeName(
185-
periodType.name
186-
)}
187-
onCheck={() => {}}
188-
/>
189-
</div>
190-
))}
233+
{group.periodTypes.map((periodType) => {
234+
const isEnabled = allowedSet.has(
235+
periodType.name
236+
)
237+
return (
238+
<div
239+
key={periodType.name}
240+
className={styles.checkboxItem}
241+
>
242+
<CheckboxMaterial
243+
checked={isEnabled}
244+
disabled={updating}
245+
label={formatPeriodTypeName(
246+
periodType.name
247+
)}
248+
onCheck={() =>
249+
handlePeriodTypeToggle(
250+
periodType.name,
251+
isEnabled
252+
)
253+
}
254+
/>
255+
</div>
256+
)
257+
})}
191258
</div>
192259
</div>
193260
))}

0 commit comments

Comments
 (0)