@@ -2,7 +2,9 @@ import { useDataQuery } from '@dhis2/app-runtime'
22import i18n from '@dhis2/d2-i18n'
33import { CenteredContent , CircularLoader } from '@dhis2/ui'
44import 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'
68import styles from './PeriodTypes.module.css'
79
810const query = {
@@ -132,7 +134,58 @@ const groupByFrequency = (periodTypes) => {
132134}
133135
134136const 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