From de2a5c2413c6e0a7890342045ce517dc15599f51 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 1 Sep 2024 16:08:15 +0200 Subject: [PATCH] [DEV] test 2 --- .../src/components/popup/TrackEditPopUp.tsx | 236 ++++++++++++------ 1 file changed, 157 insertions(+), 79 deletions(-) diff --git a/front2/src/components/popup/TrackEditPopUp.tsx b/front2/src/components/popup/TrackEditPopUp.tsx index 939ab20..6be79e2 100644 --- a/front2/src/components/popup/TrackEditPopUp.tsx +++ b/front2/src/components/popup/TrackEditPopUp.tsx @@ -20,7 +20,6 @@ import { useNavigate, useParams } from 'react-router-dom'; import { Album, Track } from '@/back-api'; import { FormGroup } from '@/components/form/FormGroup'; -import { useFormidable } from '@/components/form/Formidable'; import { SelectMultiple } from '@/components/form/SelectMultiple'; import { SelectSingle } from '@/components/form/SelectSingle'; import { useOrderedAlbums } from '@/service/Album'; @@ -31,6 +30,25 @@ import { isNullOrUndefined } from '@/utils/validator'; export type TrackEditPopUpProps = {}; +const getDifferences = (obj1: any, obj2: any): { [key: string]: boolean } => { + const result: { [key: string]: boolean } = {}; + for (const key in obj1) { + if (obj1.hasOwnProperty(key)) { + result[key] = obj1[key] !== obj2[key]; + } + } + return result; +}; + +const hasAnyTrue = (obj: { [key: string]: boolean }): boolean => { + for (const key in obj) { + if (obj.hasOwnProperty(key) && obj[key] === true) { + return true; + } + } + return false; +}; + export const TrackEditPopUp = ({}: TrackEditPopUpProps) => { const { trackId } = useParams(); const { dataGenders } = useOrderedGenders(undefined); @@ -51,88 +69,148 @@ export const TrackEditPopUp = ({}: TrackEditPopUpProps) => { }; const initialRef = useRef(null); const finalRef = useRef(null); - const form = useFormidable({ - //onSubmit, - //onValuesChange, - initialValues: dataTrack, - //onValid: () => console.log('onValid'), - //onInvalid: () => console.log('onInvalid'), + const form = useForm({ + onSubmit, + onValuesChange, + initialValues: { ...dataTrack }, + onValid: () => console.log('onValid'), + onInvalid: () => console.log('onInvalid'), }); + const formValues = useFormFields({ + connect: form, + selector: (field) => field.value, + }); + /* + const formValues = useFormFields({ + connect: form, + fields: ['genderId'] as const, + selector: 'value', + }); + */ + console.log(`dataTrack : ${JSON.stringify(dataTrack, null, 2)}`); + console.log(`Redraw : ${JSON.stringify(formValues, null, 2)}`); + const [newData, setNewData] = useState(); + const [changeData, setChangeData] = useState<{ [key: string]: boolean }>(); + const [changeOneData, setChangeOneData] = useState(true); + // initialize value when ready + useEffect(() => { + setNewData(dataTrack); + }, [dataTrack, setNewData, setChangeData]); + + useEffect(() => { + if (!newData) { + return; + } + const ret = getDifferences(dataTrack, newData); + setChangeData(ret); + ///////////////////////setChangeOneData(hasAnyTrue(ret)); + console.log( + `ChangeData=${JSON.stringify(ret, null, 2)} ChangeOneData=${hasAnyTrue(ret)}` + ); + }, [dataTrack, newData, setChangeData, setChangeOneData]); + + // const onChangeValue = (key: string, data) => { + // console.log(`[${key}] data: ${data}`); + // setNewData((previous) => { + // if (previous === undefined) { + // return previous; + // } + // if (previous[key] === data) { + // return previous; + // } + // const tmp = { ...previous }; + // tmp[key] = data; + // console.log(`data = ${JSON.stringify(tmp, null, 2)}`); + // return tmp; + // }); + // }; return ( - - - - Edit Track - + +
+ + + + Edit Track + - - - Title - form.setValues({ name: e.target.value })} - /> - + + + Title + form.setValues({ name: e.target.value })} + /> + - - -