From ebcf9cc1341863e2cb13a7ba971f641f1e4ca17e Mon Sep 17 00:00:00 2001 From: Mahdi Triki <133099132+trikimahdi@users.noreply.github.com> Date: Wed, 7 Jun 2023 10:20:48 +0200 Subject: [PATCH] Remove usimportexportparams (#266) Signed-off-by: Mahdi Triki --- demo/src/FlatParametersTab.js | 23 ++-- src/hooks/useImportExportParams.js | 173 ----------------------------- src/index.js | 5 - 3 files changed, 14 insertions(+), 187 deletions(-) delete mode 100644 src/hooks/useImportExportParams.js diff --git a/demo/src/FlatParametersTab.js b/demo/src/FlatParametersTab.js index f95f5974..377c642c 100644 --- a/demo/src/FlatParametersTab.js +++ b/demo/src/FlatParametersTab.js @@ -5,8 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { useImportExportParams } from '../../src'; -import React from 'react'; +import React, { useCallback, useState } from 'react'; import RightResizableBox from './right-resizable-box'; import FlatParameters from '../../src/components/FlatParameters/FlatParameters'; @@ -105,18 +104,24 @@ const EXAMPLE_PARAMETERS = [ ]; export const FlatParametersTab = () => { - const [currentParameters1, paramsComponent1] = useImportExportParams( - EXAMPLE_PARAMETERS, - null, - false - ); + const [currentParameters, setCurrentParameters] = useState({}); + const onChange = useCallback((paramName, value, isEdit) => { + if (!isEdit) { + setCurrentParameters((prevCurrentParameters) => { + return { + ...prevCurrentParameters, + ...{ [paramName]: value }, + }; + }); + } + }, []); return (
- {paramsComponent1} diff --git a/src/hooks/useImportExportParams.js b/src/hooks/useImportExportParams.js deleted file mode 100644 index 64fc9973..00000000 --- a/src/hooks/useImportExportParams.js +++ /dev/null @@ -1,173 +0,0 @@ -/** - * Copyright (c) 2022, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -// Hook taking an array of parameters with this format -// [{"name":"nameOfParam","type":"typeOfParam","description":"descriptionOfParam","defaultValue":"defaultValue","possibleValues":[arrayOfPossibleValue]}] -// Returns : -// - an object containing those modified values to be able to send them to a backend -// - a render of a form allowing to modify those values -// - a function allowing to reset the fields - -import React, { useCallback, useMemo, useRef, useState } from 'react'; -import FlatParameters, { - extractDefault, -} from '../components/FlatParameters/FlatParameters'; - -function areEquivDeeply(a, b) { - if (a === b) { - return true; - } - - const aIsArray = Array.isArray(a); - const bIsArray = Array.isArray(b); - if (aIsArray || bIsArray) { - if (aIsArray && bIsArray && a.length === b.length) { - let i = 0; - while (i < a.length && areEquivDeeply(a[i], b[i])) { - ++i; - } - if (i >= a.length) { - return true; - } - } - return false; - } - - if (typeof a !== 'object' || typeof b !== 'object') { - return false; - } - - return areEquivDeeply(Object.entries(a), Object.entries(b)); -} - -export function extractDefaultMap(paramsAsArray) { - return Object.fromEntries( - paramsAsArray.map((paramDescription) => { - return [paramDescription.name, extractDefault(paramDescription)]; - }) - ); -} - -export function makeDeltaMap(defaultMap, changingMap) { - if (!changingMap) { - return null; - } - - const delta = {}; - - Object.entries(defaultMap).forEach(([k, v]) => { - const m = changingMap[k]; - if (!areEquivDeeply(v, m)) { - delta[k] = m; - } - }); - - return Object.keys(delta).length ? delta : null; -} - -function makeFullMap(defaultMap, changingMap) { - if (!changingMap) { - return { ...defaultMap }; - } - - const full = {}; - - Object.entries(defaultMap).forEach(([k, v]) => { - if (!changingMap.hasOwnProperty(k)) { - full[k] = v; - } else { - const m = changingMap[k]; - if (!areEquivDeeply(v, m)) { - full[k] = m; - } else { - full[k] = v; - } - } - }); - - return full; -} - -export const useImportExportParams = ( - paramsAsArray, - initValues, - returnsDelta = true, - variant = 'outlined' -) => { - const defaultValues = useMemo(() => { - return extractDefaultMap(paramsAsArray); - }, [paramsAsArray]); - const baseValues = useMemo(() => { - return makeFullMap(defaultValues, initValues); - }, [defaultValues, initValues]); - - const [currentValues, setCurrentValues] = useState(baseValues); - const prevRef = useRef(); - - const onChange = useCallback((paramName, value, isEdit) => { - if (!isEdit) { - setCurrentValues((prevCurrentValues) => { - return { - ...prevCurrentValues, - ...{ [paramName]: value }, - }; - }); - } - }, []); - - const resetValuesToDefault = useCallback( - (isToInit = true) => { - setCurrentValues(isToInit ? baseValues : defaultValues); - }, - [defaultValues, baseValues] - ); - - const jsx = useMemo(() => { - return ( - - ); - }, [paramsAsArray, currentValues, onChange, variant]); - - let ret; - if ( - prevRef.current && - areEquivDeeply(prevRef.current.currentValues, currentValues) - ) { - if (!returnsDelta) { - ret = [prevRef.current.currentValues, jsx, resetValuesToDefault]; - } else if (!prevRef.current.deltaValues) { - ret = [ - makeDeltaMap(defaultValues, currentValues), - jsx, - resetValuesToDefault, - ]; - } else { - ret = [prevRef.current.deltaValues, jsx, resetValuesToDefault]; - } - } else { - ret = [ - returnsDelta - ? makeDeltaMap(defaultValues, currentValues) - : currentValues, - jsx, - resetValuesToDefault, - ]; - } - - prevRef.current = { - currentValues, - jsx: ret[1], - deltaValues: returnsDelta ? ret[0] : null, - }; - - return ret; -}; diff --git a/src/index.js b/src/index.js index 6dbac285..f04b12c1 100644 --- a/src/index.js +++ b/src/index.js @@ -79,11 +79,6 @@ export card_error_boundary_fr from './components/translations/card-error-boundar export { TagRenderer } from './components/ElementSearchDialog'; export { EquipmentItem } from './components/ElementSearchDialog/equipment-item'; export CardErrorBoundary from './components/CardErrorBoundary'; -export { - useImportExportParams, - makeDeltaMap, - extractDefaultMap, -} from './hooks/useImportExportParams'; export { useIntlRef } from './hooks/useIntlRef'; export { useSnackMessage } from './hooks/useSnackMessage'; export { useDebounce } from './hooks/useDebounce';