-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EES-5541 simplified form layout and updated reducer
- Loading branch information
1 parent
1427d1a
commit 1b744fc
Showing
6 changed files
with
288 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 38 additions & 111 deletions
149
...src/pages/release/datablocks/components/chart/ChartBoundaryLevelsDataSetConfiguration.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,70 @@ | ||
import Button from '@common/components/Button'; | ||
import ButtonGroup from '@common/components/ButtonGroup'; | ||
import ButtonText from '@common/components/ButtonText'; | ||
import FormFieldSelect from '@common/components/form/FormFieldSelect'; | ||
import Modal from '@common/components/Modal'; | ||
import { FormFieldSelect } from '@common/components/form'; | ||
import { MapDataSetConfig } from '@common/modules/charts/types/chart'; | ||
import expandDataSet from '@common/modules/charts/util/expandDataSet'; | ||
import generateDataSetKey from '@common/modules/charts/util/generateDataSetKey'; | ||
import { FullTableMeta } from '@common/modules/table-tool/types/fullTable'; | ||
import React, { useCallback, useState } from 'react'; | ||
import { useFieldArray, UseFormReturn } from 'react-hook-form'; | ||
import type { ChartBoundaryLevelsFormValues } from './ChartBoundaryLevelsConfiguration'; | ||
import React, { useMemo } from 'react'; | ||
import generateDataSetLabel from './utils/generateDataSetLabel'; | ||
|
||
interface Props { | ||
control: UseFormReturn<ChartBoundaryLevelsFormValues>['control']; | ||
meta: FullTableMeta; | ||
} | ||
|
||
export default function ChartBoundaryLevelsDataSetConfiguration({ | ||
control, | ||
dataSetConfigs, | ||
meta, | ||
}: Props) { | ||
const [updateIndex, setUpdateIndex] = useState<number>(); | ||
const [selectValue, setSelectValue] = useState<number>(); | ||
const { fields: dataSetConfigs, update } = useFieldArray< | ||
ChartBoundaryLevelsFormValues, | ||
'dataSetConfigs' | ||
>({ | ||
control, | ||
name: 'dataSetConfigs', | ||
}); | ||
|
||
const openFormModal = useCallback( | ||
(i: number) => { | ||
setUpdateIndex(i); | ||
setSelectValue(dataSetConfigs[i].boundaryLevel); | ||
}, | ||
[setUpdateIndex, dataSetConfigs], | ||
); | ||
|
||
const closeFormModal = useCallback(() => { | ||
setUpdateIndex(undefined); | ||
setSelectValue(undefined); | ||
}, [setUpdateIndex]); | ||
}: { | ||
dataSetConfigs: Omit<MapDataSetConfig, 'dataGrouping'>[]; | ||
meta: FullTableMeta; | ||
}) { | ||
const mappedDataSetConfigs = useMemo(() => { | ||
return dataSetConfigs.map(dataSetConfig => { | ||
const expandedDataSet = expandDataSet(dataSetConfig.dataSet, meta); | ||
const dataSetLabel = generateDataSetLabel(expandedDataSet); | ||
const key = generateDataSetKey(dataSetConfig.dataSet); | ||
|
||
return { | ||
dataSetLabel, | ||
key, | ||
}; | ||
}); | ||
}, [meta, dataSetConfigs]); | ||
return ( | ||
<> | ||
<h4>Set boundary levels per data set</h4> | ||
{/* {error && <ErrorMessage id={`${id}-error`}>{error}</ErrorMessage>} */} | ||
|
||
{!!dataSetConfigs && dataSetConfigs.length > 1 && ( | ||
<table data-testid="chart-boundary-level-selections"> | ||
<thead> | ||
<tr> | ||
<th>Data set</th> | ||
<th>Boundary</th> | ||
<th className="govuk-!-text-align-right">Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{dataSetConfigs.map((dataSetConfig, index) => { | ||
const expandedDataSet = expandDataSet( | ||
dataSetConfig.dataSet, | ||
meta, | ||
); | ||
const label = generateDataSetLabel(expandedDataSet); | ||
const key = generateDataSetKey(dataSetConfig.dataSet); | ||
|
||
const boundaryLabel = dataSetConfig.boundaryLevel | ||
? meta.boundaryLevels.find( | ||
({ id }) => | ||
String(id) === String(dataSetConfig.boundaryLevel), | ||
)?.label | ||
: 'Default'; | ||
|
||
{mappedDataSetConfigs.map(({ dataSetLabel, key }, index) => { | ||
return ( | ||
<tr key={key}> | ||
<td>{label}</td> | ||
<td>{boundaryLabel}</td> | ||
<td className="govuk-!-text-align-right"> | ||
<ButtonText | ||
onClick={() => { | ||
openFormModal(index); | ||
}} | ||
> | ||
Edit | ||
</ButtonText> | ||
<td>{dataSetLabel}</td> | ||
<td> | ||
<FormFieldSelect | ||
label={`Boundary level for dataset: ${dataSetLabel}`} | ||
hideLabel | ||
name={`dataSetConfigs.${index}.boundaryLevel`} | ||
order={[]} | ||
options={[ | ||
{ | ||
label: 'Use default', | ||
value: '', | ||
}, | ||
...meta.boundaryLevels.map(({ id, label }) => ({ | ||
value: Number(id), | ||
label, | ||
})), | ||
]} | ||
/> | ||
</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
)} | ||
|
||
{updateIndex !== undefined && ( | ||
<Modal open title="Edit boundary" onExit={closeFormModal}> | ||
<FormFieldSelect | ||
label="Boundary level" | ||
hint="Select a version of geographical data to use for this dataset" | ||
name="dataSetBoundaryLevel" | ||
order={[]} | ||
options={[ | ||
{ | ||
label: 'Default', | ||
value: '', | ||
}, | ||
...meta.boundaryLevels.map(({ id, label }) => ({ | ||
value: id, | ||
label, | ||
})), | ||
]} | ||
onChange={({ target }) => { | ||
// works but dodgy type? | ||
const { value } = target; | ||
setSelectValue( | ||
Number.isNaN(Number(value)) ? undefined : Number(value), | ||
); | ||
}} | ||
/> | ||
<ButtonGroup> | ||
<Button | ||
onClick={() => { | ||
const { dataSet, dataGrouping } = dataSetConfigs[updateIndex]; | ||
update(updateIndex, { | ||
dataSet, | ||
dataGrouping, | ||
boundaryLevel: selectValue, | ||
}); | ||
closeFormModal(); | ||
}} | ||
> | ||
Done | ||
</Button> | ||
<Button onClick={closeFormModal} variant="secondary"> | ||
Cancel | ||
</Button> | ||
</ButtonGroup> | ||
</Modal> | ||
)} | ||
</> | ||
); | ||
} |
Oops, something went wrong.