From 30e1d90b36787abf1fe60d0cc19f518e21d40dd3 Mon Sep 17 00:00:00 2001 From: Steve Cassidy Date: Wed, 4 Dec 2024 18:02:11 +1100 Subject: [PATCH] rewrite the edit form to avoid need for a 'save' operation Signed-off-by: Steve Cassidy --- .../components/autoincrement/edit-form.tsx | 379 ++++++++---------- 1 file changed, 172 insertions(+), 207 deletions(-) diff --git a/app/src/gui/components/autoincrement/edit-form.tsx b/app/src/gui/components/autoincrement/edit-form.tsx index face61dfa..db83ef1c6 100644 --- a/app/src/gui/components/autoincrement/edit-form.tsx +++ b/app/src/gui/components/autoincrement/edit-form.tsx @@ -15,39 +15,36 @@ * * Filename: form.tsx * Description: - * TODO + * Defines a form for editing the auto-incrementer ranges for a field */ -import React, {useContext, useEffect, useState} from 'react'; -import {Formik, Form, Field, yupToFormErrors} from 'formik'; +import AddIcon from '@mui/icons-material/Add'; import { - ButtonGroup, - Box, - Alert, + Badge, Button, - LinearProgress, - Grid, - Divider, + ButtonGroup, Dialog, + DialogActions, + DialogContent, DialogTitle, - Typography, + Divider, IconButton, - DialogContent, - DialogActions, + Stack, + TextField, + Typography, } from '@mui/material'; -import AddIcon from '@mui/icons-material/Add'; -import {TextField} from 'formik-mui'; -import * as yup from 'yup'; +import {useContext, useState} from 'react'; +import {LocalAutoIncrementRange, ProjectID} from '@faims3/data-model'; +import CloseIcon from '@mui/icons-material/Close'; +import {useQuery, useQueryClient} from '@tanstack/react-query'; import {ActionType} from '../../../context/actions'; import {store} from '../../../context/store'; -import {ProjectID, LocalAutoIncrementRange} from '@faims3/data-model'; import { + createNewAutoincrementRange, getLocalAutoincrementRangesForField, setLocalAutoincrementRangesForField, - createNewAutoincrementRange, } from '../../../local-data/autoincrement'; -import CloseIcon from '@mui/icons-material/Close'; interface Props { project_id: ProjectID; @@ -58,11 +55,6 @@ interface Props { handleClose: () => void; } -const FORM_SCHEMA = yup.object().shape({ - start: yup.number().required().positive().integer(), - stop: yup.number().required().positive().integer(), -}); - export const AutoIncrementEditForm = ({ project_id, form_id, @@ -71,31 +63,31 @@ export const AutoIncrementEditForm = ({ open, handleClose, }: Props) => { - const [ranges, setRanges] = useState(null); - const [rangesInitialised, setRangesInitialised] = useState(false); const {dispatch} = useContext(store); - const ensureRanges = async () => { - if (ranges === null) { - const fetchedRanges = await getLocalAutoincrementRangesForField( + // useQuery to get the current ranges for the field, + // we will invalidate the query when we update the ranges + // so that they get re-fetched + const queryClient = useQueryClient(); + const queryKey = ['autoincrement', project_id, form_id, field_id]; + const {data: ranges} = useQuery({ + queryKey: queryKey, + queryFn: async () => { + const ranges = await getLocalAutoincrementRangesForField( project_id, form_id, field_id ); - setRanges(fetchedRanges); - } - }; + return ranges; + }, + initialData: [], + enabled: true, + }); const addNewRange = async () => { - await ensureRanges(); const updatedRanges = [...(ranges || [])]; updatedRanges.push(createNewAutoincrementRange(0, 0)); - setRanges(updatedRanges); - }; - - const clonedRanges = (): LocalAutoIncrementRange[] | null => { - if (ranges === null) return null; - return ranges.map(range => ({...range})); + updateRanges(updatedRanges); }; const updateRanges = async (newRanges: LocalAutoIncrementRange[]) => { @@ -106,19 +98,7 @@ export const AutoIncrementEditForm = ({ field_id, newRanges ); - dispatch({ - type: ActionType.ADD_ALERT, - payload: { - message: 'Range successfully updated', - severity: 'success', - }, - }); - - const isInitialised = newRanges.some( - range => range.using || range.fully_used - ); - setRanges(newRanges); - setRangesInitialised(isInitialised); + queryClient.invalidateQueries({queryKey: queryKey}); } catch (err: any) { dispatch({ type: ActionType.ADD_ALERT, @@ -130,163 +110,23 @@ export const AutoIncrementEditForm = ({ } }; - const renderRange = ( - range: LocalAutoIncrementRange, - rangeIndex: number, - ranges: LocalAutoIncrementRange[] - ) => { - const range_count = ranges.length; - const start_props = { - id: 'start', - label: 'start', - name: 'start', - required: true, - type: 'number', - readOnly: range.using || range.fully_used, - disabled: range.using || range.fully_used, - }; - const stop_props = { - id: 'stop', - label: 'stop', - name: 'stop', - required: true, - type: 'number', - readOnly: range.fully_used, - disabled: range.fully_used, + const updateRange = (index: number) => { + return (range: LocalAutoIncrementRange) => { + const rangesCopy = [...ranges]; + rangesCopy[index] = range; + updateRanges(rangesCopy); }; - - return ( - { - return FORM_SCHEMA.validate(values) - .then(v => { - if (!(v.stop > v.start)) { - return {stop: 'Must be greater than start'}; - } - return {}; - }) - .catch(err => { - return yupToFormErrors(err); - }); - }} - onSubmit={async (values, {setSubmitting}) => { - range.start = values.start; - range.stop = values.stop; - await updateRanges(ranges).then(() => { - setSubmitting(false); - }); - }} - > - {({submitForm, isSubmitting}) => ( - -
- - - - - - - - - - - - {range.using ? ( - - ) : ( - - )} - - - -
- {isSubmitting && } - -
- )} -
- ); }; - const renderRanges = () => { - const currentRanges = clonedRanges(); - - const all_used = currentRanges?.every( - (range: LocalAutoIncrementRange) => range.fully_used - ); - - if (currentRanges === null || currentRanges.length === 0) { - return ( - - No ranges allocated yet. - - ); + const handleRemoveRange = (index: number) => { + const newRanges = ranges?.filter((_, i) => i !== index); + if (newRanges !== undefined) { + updateRanges(newRanges); + } else { + updateRanges([]); } - - return ( -
- {all_used && ( - - All ranges are fully used. Please add a new range. - - )} - {currentRanges.map((range, index) => - renderRange(range, index, currentRanges) - )} -
- ); }; - useEffect(() => { - ensureRanges(); - }, []); - return ( Edit Settings for {label} @@ -314,11 +154,21 @@ export const AutoIncrementEditForm = ({ - {renderRanges()} - - + + {ranges?.map((range, index) => { + return ( + 1} + /> + ); + })} + + + + )} + + {!(props.range.using || props.range.fully_used) && ( + + )} + {props.range.fully_used && ( + + )} + + + + ); +};