From 66baa16dd8524e6f8fdfae1e20088170e45e6de7 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Sat, 7 Sep 2024 09:13:26 +0100 Subject: [PATCH 1/4] feat: remove locales from redux store --- .../item/compare/home-container.js | 5 +-- .../item/home-container.js | 5 +-- .../series/visualization/home-container.js | 5 +-- .../visualization/home-container.tsx | 6 +-- .../edition-creation/creation-container.js | 4 +- .../edition-creation/edition-container.js | 5 +-- .../visualization/home-container.js | 5 ++- .../compare/home-container.js | 6 +-- .../edition-creation/creation-container.js | 6 +-- .../edition-creation/edition-container.js | 4 +- .../visualization/home-container.js | 5 +-- .../visualization/home-container.spec.js | 2 - .../datasets/edit/editing.spec.js | 2 +- .../modules-datasets/datasets/view/view.js | 17 ++++---- .../distributions/view/view.js | 9 ++--- .../document/edition/index.js | 4 +- .../document/visualization/index.js | 6 +-- .../families/edition/index.js | 5 +-- .../families/visualization/index.tsx | 9 ++--- .../indicators/edition/index.js | 4 +- .../indicators/visualization/index.js | 11 ++--- src/packages/modules-operations/msd/index.js | 15 +++---- .../modules-operations/msd/index.spec.js | 40 +------------------ .../msd/pages/help/index.js | 3 +- .../msd/pages/sims-creation/index.js | 17 +++++--- .../operations/edition/index.js | 4 +- .../operations/visualization/index.js | 11 +++-- .../series/edition/index.js | 4 +- .../series/visualization/index.js | 10 ++--- .../components/component-detail/index.js | 7 +--- .../component-detail/view-container.js | 9 ++--- .../component-detail/view-container.spec.js | 7 +--- src/packages/redux/index.spec.ts | 16 -------- src/packages/redux/model.ts | 2 - src/packages/redux/selectors.ts | 4 -- src/packages/utils/hooks/useLocales.spec.tsx | 19 +++++++++ src/packages/utils/hooks/useLocales.ts | 6 +++ 37 files changed, 123 insertions(+), 176 deletions(-) create mode 100644 src/packages/utils/hooks/useLocales.spec.tsx create mode 100644 src/packages/utils/hooks/useLocales.ts diff --git a/src/packages/modules-classifications/item/compare/home-container.js b/src/packages/modules-classifications/item/compare/home-container.js index 22e3fe750..79fbb93a4 100644 --- a/src/packages/modules-classifications/item/compare/home-container.js +++ b/src/packages/modules-classifications/item/compare/home-container.js @@ -1,16 +1,15 @@ -import { useSelector } from 'react-redux'; import { useParams } from 'react-router-dom'; import { Loading } from '../../../components'; import Compare from './home'; import useClassificationItem from '../hook'; -import { getLocales } from '../../../redux/selectors'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const CompareContainer = () => { const { classificationId, itemId } = useParams(); const [secondLang] = useSecondLang(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const { isLoading, item } = useClassificationItem(classificationId, itemId); diff --git a/src/packages/modules-classifications/item/home-container.js b/src/packages/modules-classifications/item/home-container.js index 82dfc24e7..0f0ff2b90 100644 --- a/src/packages/modules-classifications/item/home-container.js +++ b/src/packages/modules-classifications/item/home-container.js @@ -1,18 +1,17 @@ -import { useSelector } from 'react-redux'; import ItemVisualization from './home'; import { Loading } from '../../components'; import { useParams } from 'react-router-dom'; import useClassificationItem from './hook'; import { useQueryClient } from '@tanstack/react-query'; import { fetchingPreviousLevels } from './client'; -import { getLocales } from '../../redux/selectors'; import { useSecondLang } from '../../utils/hooks/second-lang'; +import { useLocales } from '../../utils/hooks/useLocales'; const ItemVisualizationContainer = () => { const queryClient = useQueryClient(); const { classificationId, itemId } = useParams(); const [secondLang] = useSecondLang(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const { isLoading, item } = useClassificationItem( classificationId, diff --git a/src/packages/modules-classifications/series/visualization/home-container.js b/src/packages/modules-classifications/series/visualization/home-container.js index 9237151e3..80156f7e0 100644 --- a/src/packages/modules-classifications/series/visualization/home-container.js +++ b/src/packages/modules-classifications/series/visualization/home-container.js @@ -1,18 +1,17 @@ import { useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import SeriesVisualization from './home'; import { Loading } from '../../../components'; import { useParams } from 'react-router-dom'; import { ClassificationsApi } from '../../../sdk/classification'; -import { getLocales } from '../../../redux/selectors'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const SeriesVisualizationContainer = () => { const { id } = useParams(); const [series, setSeries] = useState(); const [secondLang] = useSecondLang(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); useEffect(() => { Promise.all([ ClassificationsApi.getSeriesGeneral(id), diff --git a/src/packages/modules-classifications/visualization/home-container.tsx b/src/packages/modules-classifications/visualization/home-container.tsx index 1e414d948..0d4661361 100644 --- a/src/packages/modules-classifications/visualization/home-container.tsx +++ b/src/packages/modules-classifications/visualization/home-container.tsx @@ -1,15 +1,13 @@ import { useParams } from 'react-router-dom'; -import { useSelector } from 'react-redux'; import ClassificationVisualization from './home'; import { Loading } from '../../components'; import { useClassification, usePublishClassification } from '../hooks'; -import { getLocales } from '../../redux/selectors'; -import { ReduxModel } from '../../redux/model'; import { useSecondLang } from '../../utils/hooks/second-lang'; +import { useLocales } from '../../utils/hooks/useLocales'; const ClassificationVisualizationContainer = () => { const { id } = useParams<{ id: string }>(); - const langs = useSelector((state: ReduxModel) => getLocales(state)); + const langs = useLocales(); const [secondLang] = useSecondLang(); const { isLoading, classification } = useClassification(id); diff --git a/src/packages/modules-concepts/collections/edition-creation/creation-container.js b/src/packages/modules-concepts/collections/edition-creation/creation-container.js index 247cfd384..afb1c17c7 100644 --- a/src/packages/modules-concepts/collections/edition-creation/creation-container.js +++ b/src/packages/modules-concepts/collections/edition-creation/creation-container.js @@ -10,12 +10,12 @@ import { Loading } from '../../../components'; import { ConceptsApi } from '../../../sdk'; import { CollectionApi } from '../../../sdk/collection-api'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; +import { useLocales } from '../../../utils/hooks/useLocales'; const CreationContainer = () => { const history = useHistory(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const collection = useSelector((state) => emptyCollection(state.app.properties.defaultContributor) ); diff --git a/src/packages/modules-concepts/collections/edition-creation/edition-container.js b/src/packages/modules-concepts/collections/edition-creation/edition-container.js index c940eb7bd..ae6c8f6ff 100644 --- a/src/packages/modules-concepts/collections/edition-creation/edition-container.js +++ b/src/packages/modules-concepts/collections/edition-creation/edition-container.js @@ -1,5 +1,4 @@ import { useCallback, useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import { useHistory, useParams } from 'react-router-dom'; import CollectionEditionCreation from './home'; import buildPayload from '../../../modules-concepts/collections/utils/build-payload/build-payload'; @@ -9,13 +8,13 @@ import { Loading } from '../../../components'; import { ConceptsApi } from '../../../sdk'; import { CollectionApi } from '../../../sdk/collection-api'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; +import { useLocales } from '../../../utils/hooks/useLocales'; const EditionContainer = () => { const { id } = useParams(); const history = useHistory(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const [loadingCollection, setLoadingCollection] = useState(true); const [loadingExtraData, setLoadingExtraData] = useState(true); diff --git a/src/packages/modules-concepts/collections/visualization/home-container.js b/src/packages/modules-concepts/collections/visualization/home-container.js index 58390dad5..0954f2ecf 100644 --- a/src/packages/modules-concepts/collections/visualization/home-container.js +++ b/src/packages/modules-concepts/collections/visualization/home-container.js @@ -4,8 +4,9 @@ import { Loading } from '../../../components'; import CollectionVisualization from './home'; import { useParams } from 'react-router-dom'; import { ConceptsApi } from '../../../sdk'; -import { getLocales, getPermission } from '../../../redux/selectors'; +import { getPermission } from '../../../redux/selectors'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const CollectionVisualizationContainer = () => { const { id } = useParams(); @@ -15,7 +16,7 @@ const CollectionVisualizationContainer = () => { const permission = useSelector((state) => getPermission(state)); const [secondLang] = useSecondLang(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const fetchData = useCallback(() => { Promise.all([ diff --git a/src/packages/modules-concepts/compare/home-container.js b/src/packages/modules-concepts/compare/home-container.js index 4e711fe1e..efd6b6c86 100644 --- a/src/packages/modules-concepts/compare/home-container.js +++ b/src/packages/modules-concepts/compare/home-container.js @@ -1,17 +1,17 @@ import { useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import { useParams } from 'react-router-dom'; import { Loading } from '../../components'; import ConceptCompare from './home'; import { ConceptsApi } from '../../sdk'; import { rmesHtmlToRawHtml } from '../../utils/html-utils'; -import { getLocales } from '../../redux/selectors'; import { emptyNotes } from '../utils/notes'; import { range } from '../../utils/array-utils'; import { useSecondLang } from '../../utils/hooks/second-lang'; +import { useLocales } from '../../utils/hooks/useLocales'; + const ConceptCompareContainer = () => { const { id } = useParams(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const [secondLang] = useSecondLang(); const [loading, setLoading] = useState(true); diff --git a/src/packages/modules-concepts/edition-creation/creation-container.js b/src/packages/modules-concepts/edition-creation/creation-container.js index 88f02e284..0ac37f4c0 100644 --- a/src/packages/modules-concepts/edition-creation/creation-container.js +++ b/src/packages/modules-concepts/edition-creation/creation-container.js @@ -1,4 +1,4 @@ -import { useCallback, useState, useEffect } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import ConceptEditionCreation from './home'; @@ -6,15 +6,15 @@ import D from '../../deprecated-locales'; import { Loading } from '../../components'; import { ConceptsApi } from '../../sdk'; import { useStamps } from '../../utils/hooks/stamps'; -import { getLocales } from '../../redux/selectors'; import { useTitle } from '../../utils/hooks/useTitle'; import buildPayloadCreation from '../../modules-concepts/utils/build-payload-creation-update/build-payload-creation'; import emptyConcept from '../../modules-concepts/utils/empty-concept'; import { mergeWithAllConcepts } from '../utils/links'; import { sortArrayByLabel } from '../../utils/array-utils'; +import { useLocales } from '../../utils/hooks/useLocales'; const CreationContainer = () => { - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const maxLengthScopeNote = useSelector((state) => Number(state.app.properties.maxLengthScopeNote) ); diff --git a/src/packages/modules-concepts/edition-creation/edition-container.js b/src/packages/modules-concepts/edition-creation/edition-container.js index 0a820a4f4..08558200d 100644 --- a/src/packages/modules-concepts/edition-creation/edition-container.js +++ b/src/packages/modules-concepts/edition-creation/edition-container.js @@ -9,11 +9,11 @@ import { rmesHtmlToRawHtml } from '../../utils/html-utils'; import { ConceptsApi } from '../../sdk'; import * as generalUtils from '../../modules-concepts/utils/general'; import { useStamps } from '../../utils/hooks/stamps'; -import { getLocales } from '../../redux/selectors'; import { useTitle } from '../../utils/hooks/useTitle'; import buildPayloadUpdate from '../../modules-concepts/utils/build-payload-creation-update/build-payload-update'; import { mergeWithAllConcepts } from '../utils/links'; import { emptyNotes } from '../utils/notes'; +import { useLocales } from '../../utils/hooks/useLocales'; const formatNotes = (notes) => { return Object.assign( @@ -29,7 +29,7 @@ const EditionContainer = () => { const { id } = useParams(); const history = useHistory(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const maxLengthScopeNote = useSelector((state) => Number(state.app.properties.maxLengthScopeNote) ); diff --git a/src/packages/modules-concepts/visualization/home-container.js b/src/packages/modules-concepts/visualization/home-container.js index d6ac32ee9..b236188e5 100644 --- a/src/packages/modules-concepts/visualization/home-container.js +++ b/src/packages/modules-concepts/visualization/home-container.js @@ -1,15 +1,14 @@ import { useCallback, useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import { useHistory, useParams } from 'react-router-dom'; import { Loading } from '../../components'; import ConceptVisualization from './home'; import { LoadingProvider } from './loading'; import { ConceptsApi } from '../../sdk'; import { rmesHtmlToRawHtml } from '../../utils/html-utils'; -import { getLocales } from '../../redux/selectors'; import { emptyNotes } from '../utils/notes'; import { usePermission } from '../../redux/hooks/usePermission'; import { useSecondLang } from '../../utils/hooks/second-lang'; +import { useLocales } from '../../utils/hooks/useLocales'; const formatNotes = (notes) => { return Object.assign( @@ -25,7 +24,7 @@ const ConceptVisualizationContainer = () => { const { id } = useParams(); const history = useHistory(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const permission = usePermission(); const [secondLang] = useSecondLang(); diff --git a/src/packages/modules-concepts/visualization/home-container.spec.js b/src/packages/modules-concepts/visualization/home-container.spec.js index ee868e392..5b4b71602 100644 --- a/src/packages/modules-concepts/visualization/home-container.spec.js +++ b/src/packages/modules-concepts/visualization/home-container.spec.js @@ -5,7 +5,6 @@ import { ConceptsApi } from '../../sdk'; import * as R from '../../auth/roles'; import configureStore from '../../redux/configure-store'; import { renderWithAppContext } from '../../tests-utils/render'; -import { locales } from '../../tests-utils/default-values'; jest.mock('../../sdk'); @@ -18,7 +17,6 @@ const store = configureStore({ stamp: 'stamp', }, }, - ...locales, }, }); diff --git a/src/packages/modules-datasets/datasets/edit/editing.spec.js b/src/packages/modules-datasets/datasets/edit/editing.spec.js index fa2089f33..0077ec734 100644 --- a/src/packages/modules-datasets/datasets/edit/editing.spec.js +++ b/src/packages/modules-datasets/datasets/edit/editing.spec.js @@ -14,7 +14,7 @@ jest.mock('../../datasets', () => ({ })); const store = configureStore({ - app: { secondLang: true, auth: { type: '', user: { roles: [] } } }, + app: { auth: { type: '', user: { roles: [] } } }, }); describe('DatasetEdit Component', () => { diff --git a/src/packages/modules-datasets/datasets/view/view.js b/src/packages/modules-datasets/datasets/view/view.js index 938163a0e..d5d7ef023 100644 --- a/src/packages/modules-datasets/datasets/view/view.js +++ b/src/packages/modules-datasets/datasets/view/view.js @@ -1,5 +1,4 @@ import { useHistory, useParams } from 'react-router-dom'; -import { useSelector } from 'react-redux'; import { renderMarkdownElement } from '../../../utils/html-utils'; import { useEffect, useState } from 'react'; import { Note } from '@inseefr/wilco'; @@ -9,18 +8,17 @@ import { withCodesLists } from '../../../utils/hoc/withCodesLists'; import { useDataset } from '../../datasets'; import { ViewMenu } from './menu'; import { - Deleting, - Publishing, - Loading, - Row, + CheckSecondLang, ContributorsVisualisation, + Deleting, DisseminationStatusVisualisation, - PageTitleBlock, - CheckSecondLang, ErrorBloc, + Loading, + PageTitleBlock, + Publishing, + Row, } from '../../../components'; import { CL_PROCESS_STEP } from '../../../redux/actions/constants/codeList'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; import { DatasetsApi } from '../../../sdk'; @@ -30,6 +28,7 @@ import { List } from '../../../components/list'; import { CodeDisplay } from '../../../components/code-display'; import { StatisticalInformations } from './StatisticalInformations'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const Dataset = (props) => { const [serverSideError, setServerSideError] = useState(); @@ -44,7 +43,7 @@ const Dataset = (props) => { const { data: dataset, isLoading } = useDataset(id); - const { lg1, lg2 } = useSelector((state) => getLocales(state)); + const { lg1, lg2 } = useLocales(); const [secondLang] = useSecondLang(); const queryClient = useQueryClient(); diff --git a/src/packages/modules-datasets/distributions/view/view.js b/src/packages/modules-datasets/distributions/view/view.js index ac479d71e..fb18fa0d5 100644 --- a/src/packages/modules-datasets/distributions/view/view.js +++ b/src/packages/modules-datasets/distributions/view/view.js @@ -1,10 +1,9 @@ import { useHistory, useParams } from 'react-router-dom'; -import { useSelector } from 'react-redux'; import { + CheckSecondLang, Loading, - Row, PageTitleBlock, - CheckSecondLang, + Row, } from '../../../components'; import { renderMarkdownElement } from '../../../utils/html-utils'; import { Note } from '@inseefr/wilco'; @@ -12,11 +11,11 @@ import D, { D1, D2 } from '../../../deprecated-locales/build-dictionary'; import { useDataset, useDistribution } from '../../datasets'; import { ViewMenu } from './menu'; import { useMutation, useQueryClient } from '@tanstack/react-query'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; import { stringToDate } from '../../../utils/date-utils'; import { DistributionApi } from '../../../sdk'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; export const DistributionView = (props) => { const { id } = useParams(); @@ -28,7 +27,7 @@ export const DistributionView = (props) => { distribution?.idDataset ); - const { lg1, lg2 } = useSelector((state) => getLocales(state)); + const { lg1, lg2 } = useLocales(); const [secondLang] = useSecondLang(); const queryClient = useQueryClient(); diff --git a/src/packages/modules-operations/document/edition/index.js b/src/packages/modules-operations/document/edition/index.js index 4eb1567e4..5bdd30d8d 100644 --- a/src/packages/modules-operations/document/edition/index.js +++ b/src/packages/modules-operations/document/edition/index.js @@ -4,15 +4,15 @@ import { useDispatch, useSelector } from 'react-redux'; import { Loading } from '../../../components'; import DocumentationEdition from '../../../modules-operations/document/edition/edition'; import { loadCodesList } from '../../../redux/actions/operations/utils/setup'; -import { getLocales } from '../../../redux/selectors'; import { GeneralApi } from '../../../sdk/general-api'; +import { useLocales } from '../../../utils/hooks/useLocales'; const OperationsDocumentationEditionContainer = (props) => { const { id } = useParams(); const { pathname } = useLocation(); const type = /(link|document)/.exec(pathname)[1]; - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const langOptions = useSelector( (state) => state.operationsCodesList.results['ISO-639'] || {} ); diff --git a/src/packages/modules-operations/document/visualization/index.js b/src/packages/modules-operations/document/visualization/index.js index 9d4ca130b..1e64c02ca 100644 --- a/src/packages/modules-operations/document/visualization/index.js +++ b/src/packages/modules-operations/document/visualization/index.js @@ -1,4 +1,4 @@ -import { Loading, PageTitleBlock, CheckSecondLang } from '../../../components'; +import { CheckSecondLang, Loading, PageTitleBlock } from '../../../components'; import { loadCodesList } from '../../../redux/actions/operations/utils/setup'; @@ -7,10 +7,10 @@ import { useDispatch, useSelector } from 'react-redux'; import { useParams, useRouteMatch } from 'react-router-dom'; import OperationsDocumentVisualization from './home'; -import { getLocales } from '../../../redux/selectors'; import { GeneralApi } from '../../../sdk/general-api'; import { Menu } from './Menu'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; function getPath(path) { return path.includes('document') ? 'document' : 'link'; @@ -20,7 +20,7 @@ const DocumentationVisualizationContainer = () => { const { id } = useParams(); const { path } = useRouteMatch(); const type = getPath(path); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const [secondLang] = useSecondLang(); const langOptions = useSelector( (state) => state.operationsCodesList.results['ISO-639'] diff --git a/src/packages/modules-operations/families/edition/index.js b/src/packages/modules-operations/families/edition/index.js index 7d9d51e86..5e85e1b13 100644 --- a/src/packages/modules-operations/families/edition/index.js +++ b/src/packages/modules-operations/families/edition/index.js @@ -1,17 +1,16 @@ import { useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; -import { useSelector } from 'react-redux'; import { Loading } from '../../../components'; import OperationsFamilyEdition from '../../../modules-operations/families/edition/edition'; import { useGoBack } from '../../../utils/hooks/useGoBack'; import D from '../../../deprecated-locales/build-dictionary'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; import { OperationsApi } from '../../../sdk/operations-api'; +import { useLocales } from '../../../utils/hooks/useLocales'; const OperationsFamilyEditionContainer = () => { const { id } = useParams(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const goBack = useGoBack(); const [family, setFamily] = useState({}); diff --git a/src/packages/modules-operations/families/visualization/index.tsx b/src/packages/modules-operations/families/visualization/index.tsx index 2f07cb5db..e3a948ab2 100644 --- a/src/packages/modules-operations/families/visualization/index.tsx +++ b/src/packages/modules-operations/families/visualization/index.tsx @@ -1,24 +1,23 @@ import D from '../../../deprecated-locales'; -import { useSelector } from 'react-redux'; import { useParams } from 'react-router-dom'; import { - Loading, + CheckSecondLang, ErrorBloc, + Loading, PageTitleBlock, - CheckSecondLang, } from '../../../components'; import { useCallback, useEffect, useState } from 'react'; import OperationsFamilyVisualization from '../../../modules-operations/families/visualization/visualization'; -import { getLocales } from '../../../redux/selectors'; import { OperationsApi } from '../../../sdk/operations-api'; import { Menu } from './menu'; import { Family } from '../../../model/operations/family'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const FamilyView = () => { const { id } = useParams<{ id: string }>(); - const langs = useSelector(getLocales); + const langs = useLocales(); const [secondLang] = useSecondLang(); const [family, setFamily] = useState(); diff --git a/src/packages/modules-operations/indicators/edition/index.js b/src/packages/modules-operations/indicators/edition/index.js index f47501c21..35593b6ca 100644 --- a/src/packages/modules-operations/indicators/edition/index.js +++ b/src/packages/modules-operations/indicators/edition/index.js @@ -7,14 +7,14 @@ import { CL_FREQ } from '../../../redux/actions/constants/codeList'; import { useGoBack } from '../../../utils/hooks/useGoBack'; import { useCodesList } from '../../../utils/hooks/codeslist'; import D from '../../../deprecated-locales'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; import { OperationsApi } from '../../../sdk/operations-api'; +import { useLocales } from '../../../utils/hooks/useLocales'; const OperationsIndicatorsEditionContainer = (props) => { const { id } = useParams(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const frequencies = useCodesList(CL_FREQ); const organisations = useSelector( (state) => state.operationsOrganisations.results || [] diff --git a/src/packages/modules-operations/indicators/visualization/index.js b/src/packages/modules-operations/indicators/visualization/index.js index 345dd699e..c8e1a43a2 100644 --- a/src/packages/modules-operations/indicators/visualization/index.js +++ b/src/packages/modules-operations/indicators/visualization/index.js @@ -1,25 +1,26 @@ -import { useEffect, useState, useCallback } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; import D from '../../../deprecated-locales'; import { useSelector } from 'react-redux'; import OperationsIndicatorVisualization from '../../../modules-operations/indicators/visualization/general'; import { - Loading, + CheckSecondLang, ErrorBloc, + Loading, PageTitleBlock, - CheckSecondLang, } from '../../../components'; import { CL_FREQ } from '../../../redux/actions/constants/codeList'; import { useCodesList } from '../../../utils/hooks/codeslist'; -import { getLocales } from '../../../redux/selectors'; import { OperationsApi } from '../../../sdk/operations-api'; import { Menu } from './menu'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; + const IndicatorVisualizationContainer = () => { const { id } = useParams(); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const [secondLang] = useSecondLang(); const frequency = useCodesList(CL_FREQ); const organisations = useSelector( diff --git a/src/packages/modules-operations/msd/index.js b/src/packages/modules-operations/msd/index.js index 218be7fd9..a8f376f4c 100644 --- a/src/packages/modules-operations/msd/index.js +++ b/src/packages/modules-operations/msd/index.js @@ -2,30 +2,30 @@ import { Component, useEffect, useState } from 'react'; import MSDLayout from '../../modules-operations/msd/layout/'; import { connect } from 'react-redux'; import { Loading, PageTitleBlock } from '../../components'; -import { NOT_LOADED, LOADED } from '../../sdk/constants'; +import { LOADED, NOT_LOADED } from '../../sdk/constants'; import loadMetadataStructure from '../../redux/operations/metadatastructure/list'; import { D1, D2 } from '../../deprecated-locales'; import { buildExtract } from '@inseefr/wilco'; import { - getOperationsOrganisations, getOperationsCodesList, + getOperationsOrganisations, } from '../../redux/operations/selector'; import loadSIMS, { - saveSims, publishSims, + saveSims, } from '../../redux/actions/operations/sims/item'; import MSDHelp from '../../modules-operations/msd/pages/help'; import SimsVisualisation from '../../modules-operations/msd/pages/sims-visualisation/'; import SimsCreation from '../../modules-operations/msd/pages/sims-creation/'; import { useRouteMatch } from 'react-router-dom'; -import { getParentType, getParentId } from './utils'; +import { getParentId, getParentType } from './utils'; import './msd.scss'; import { isEssentialRubricKo } from './sims-field-title'; import { SimsContextProvider } from './context'; import { useGoBack } from '../../utils/hooks/useGoBack'; -import { getLocales, getOperationsSimsCurrent } from '../../redux/selectors'; +import { getOperationsSimsCurrent } from '../../redux/selectors'; import { isLoaded, loadGeographies } from '../../redux/geographies.action'; import { GeneralApi } from '../../sdk/general-api'; import { OperationsApi } from '../../sdk/operations-api'; @@ -111,7 +111,6 @@ class MSDContainer extends Component { saveSims, idParent, disableSectionAnchor, - langs, currentSims, organisations, parentType, @@ -185,7 +184,6 @@ class MSDContainer extends Component { metadataStructure={metadataStructure} codesLists={codesLists} currentSection={this.props.match.params.idSection} - langs={langs} organisations={organisations} /> )} @@ -198,7 +196,6 @@ class MSDContainer extends Component { codesLists={codesLists} organisations={organisations} currentSection={this.props.match.params.idSection} - langs={langs} publishSims={this.props.publishSims} exportCallback={this.exportCallback} missingDocuments={this.state.missingDocuments} @@ -216,7 +213,6 @@ class MSDContainer extends Component { codesLists={codesLists} onSubmit={saveSims} idParent={idParent} - langs={langs} goBack={goBack} mode={mode} organisations={organisations} @@ -264,7 +260,6 @@ export const mapStateToProps = (state, ownProps) => { return { geographiesLoaded: isLoaded(state), - langs: getLocales(state), metadataStructure, metadataStructureStatus, currentSims: !id || currentSims.id === id ? currentSims : {}, diff --git a/src/packages/modules-operations/msd/index.spec.js b/src/packages/modules-operations/msd/index.spec.js index 5b65c23e6..9ad2d03a3 100644 --- a/src/packages/modules-operations/msd/index.spec.js +++ b/src/packages/modules-operations/msd/index.spec.js @@ -1,5 +1,5 @@ -import { mapStateToProps, HELP, VIEW } from './index'; -import { NOT_LOADED, LOADED } from '../../sdk/constants'; +import { HELP, mapStateToProps, VIEW } from './index'; +import { LOADED, NOT_LOADED } from '../../sdk/constants'; describe('', () => { it('should return NOT_LOADED for the metadataStructureStatus', () => { @@ -17,10 +17,6 @@ describe('', () => { results: 'operationsMetadataStructureList', status: LOADED, }, - app: { - lg1: 'lg1', - lg2: 'lg2', - }, geographies: {}, operationsDocuments: {}, }; @@ -43,10 +39,6 @@ describe('', () => { results: 'operationsMetadataStructureList', status: LOADED, }, - app: { - lg1: 'lg1', - lg2: 'lg2', - }, operationsDocuments: {}, operationsSimsCurrent: { id: 1, @@ -69,32 +61,4 @@ describe('', () => { idOperation: 2, }); }); - - it('should return the langes', () => { - const input = { - operationsMetadataStructureList: { - results: 'operationsMetadataStructureList', - status: LOADED, - }, - operationsDocuments: {}, - app: { - lg1: 'lg1', - lg2: 'lg2', - }, - geographies: {}, - }; - - const props = { - mode: HELP, - match: { - params: { - id: 1, - }, - }, - }; - expect(mapStateToProps(input, props).langs).toEqual({ - lg1: 'lg1', - lg2: 'lg2', - }); - }); }); diff --git a/src/packages/modules-operations/msd/pages/help/index.js b/src/packages/modules-operations/msd/pages/help/index.js index 4d575bceb..33bd341fd 100644 --- a/src/packages/modules-operations/msd/pages/help/index.js +++ b/src/packages/modules-operations/msd/pages/help/index.js @@ -1,13 +1,14 @@ import HelpInformation from '../../../../modules-operations/msd/help-information'; import { Note } from '@inseefr/wilco'; +import { useLocales } from '../../../../utils/hooks/useLocales'; export default function MSDHelp({ metadataStructure, currentSection, codesLists, - langs: { lg1 }, organisations, }) { + const { lg1 } = useLocales(); function MSDInformations({ msd }) { return ( <> diff --git a/src/packages/modules-operations/msd/pages/sims-creation/index.js b/src/packages/modules-operations/msd/pages/sims-creation/index.js index 199fbd4b1..8b188adb0 100644 --- a/src/packages/modules-operations/msd/pages/sims-creation/index.js +++ b/src/packages/modules-operations/msd/pages/sims-creation/index.js @@ -4,16 +4,16 @@ import Field from '../../../../modules-operations/msd/pages/sims-creation/sims-f import SimsDocumentField from '../../../../modules-operations/msd/pages/sims-creation/sims-document-field'; import { Loading, Select } from '../../../../components'; import { - CancelButton, ActionToolbar, - SaveButton, + CancelButton, CheckSecondLang, + SaveButton, } from '@inseefr/wilco'; import { DUPLICATE } from '../../index'; import { - hasLabelLg2, getParentId, getParentIdName, + hasLabelLg2, removeRubricsWhenDuplicate, shouldDisplayTitleForPrimaryItem, } from '../../utils'; @@ -23,8 +23,9 @@ import './sims-creation.scss'; import { RubricEssentialMsg } from '../../rubric-essantial-msg'; import { OperationsApi } from '../../../../sdk/operations-api'; import { sortArrayByLabel } from '../../../../utils/array-utils'; -import { rangeType, flattenTree } from '../../../utils/msd'; +import { flattenTree, rangeType } from '../../../utils/msd'; import { useHistory } from 'react-router-dom'; +import { useLocales } from '../../../../utils/hooks/useLocales'; const { RICH_TEXT } = rangeType; @@ -357,6 +358,7 @@ class SimsCreation extends Component { const withParentWithSims = (Component) => { return (props) => { + const langs = useLocales(); const history = useHistory(); const [parentWithSims, setParentWithSims] = useState([]); const parentType = props.parentType; @@ -378,7 +380,12 @@ const withParentWithSims = (Component) => { } }, [seriesId, parentType, familyId]); return ( - + ); }; }; diff --git a/src/packages/modules-operations/operations/edition/index.js b/src/packages/modules-operations/operations/edition/index.js index 9845ceedf..cf797a92a 100644 --- a/src/packages/modules-operations/operations/edition/index.js +++ b/src/packages/modules-operations/operations/edition/index.js @@ -5,16 +5,16 @@ import { Loading } from '../../../components'; import OperationsOperationEdition from '../../../modules-operations/operations/edition/edition'; import { useGoBack } from '../../../utils/hooks/useGoBack'; import D from '../../../deprecated-locales'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; import { OperationsApi } from '../../../sdk/operations-api'; +import { useLocales } from '../../../utils/hooks/useLocales'; const OperationEditionContainer = (props) => { const { id } = useParams(); const [series, setSeries] = useState([]); const [operation, setOperation] = useState({}); const stamp = useSelector((state) => state.app.auth.user.stamp); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const goBack = useGoBack(); diff --git a/src/packages/modules-operations/operations/visualization/index.js b/src/packages/modules-operations/operations/visualization/index.js index afb9e661a..469983cad 100644 --- a/src/packages/modules-operations/operations/visualization/index.js +++ b/src/packages/modules-operations/operations/visualization/index.js @@ -1,24 +1,23 @@ -import { useEffect, useState, useCallback } from 'react'; -import { useSelector } from 'react-redux'; +import { useCallback, useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; import { - Loading, + CheckSecondLang, ErrorBloc, + Loading, PageTitleBlock, - CheckSecondLang, } from '../../../components'; import OperationsOperationVisualization from './home'; import D from '../../../deprecated-locales'; -import { getLocales } from '../../../redux/selectors'; import { OperationsApi } from '../../../sdk/operations-api'; import { Menu } from './menu'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const OperationVisualizationContainer = () => { const { id } = useParams(); const [operation, setOperation] = useState({}); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const [secondLang] = useSecondLang(); const [serverSideError, setServerSideError] = useState(); const [publishing, setPublishing] = useState(false); diff --git a/src/packages/modules-operations/series/edition/index.js b/src/packages/modules-operations/series/edition/index.js index 15af2d8c1..05b1ba82b 100644 --- a/src/packages/modules-operations/series/edition/index.js +++ b/src/packages/modules-operations/series/edition/index.js @@ -7,13 +7,13 @@ import OperationsSerieEdition from '../../../modules-operations/series/edition/e import { useGoBack } from '../../../utils/hooks/useGoBack'; import { useCodesList } from '../../../utils/hooks/codeslist'; import D from '../../../deprecated-locales'; -import { getLocales } from '../../../redux/selectors'; import { useTitle } from '../../../utils/hooks/useTitle'; import { OperationsApi } from '../../../sdk/operations-api'; import { CL_FREQ, CL_SOURCE_CATEGORY, } from '../../../redux/actions/constants/codeList'; +import { useLocales } from '../../../utils/hooks/useLocales'; const OperationsSeriesEditionContainer = (props) => { const { id } = useParams(); @@ -28,7 +28,7 @@ const OperationsSeriesEditionContainer = (props) => { const organisations = useSelector( (state) => state.operationsOrganisations.results || [] ); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const goBack = useGoBack(); diff --git a/src/packages/modules-operations/series/visualization/index.js b/src/packages/modules-operations/series/visualization/index.js index bc1eb0507..129ea3080 100644 --- a/src/packages/modules-operations/series/visualization/index.js +++ b/src/packages/modules-operations/series/visualization/index.js @@ -1,17 +1,16 @@ -import { useEffect, useState, useCallback } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import D from '../../../deprecated-locales'; import { useSelector } from 'react-redux'; import { useParams } from 'react-router-dom'; import OperationsSerieVisualization from '../../../modules-operations/series/visualization/home'; import { - Loading, + CheckSecondLang, ErrorBloc, + Loading, PageTitleBlock, - CheckSecondLang, } from '../../../components'; import { useCodesList } from '../../../utils/hooks/codeslist'; -import { getLocales } from '../../../redux/selectors'; import { OperationsApi } from '../../../sdk/operations-api'; import { CL_FREQ, @@ -19,6 +18,7 @@ import { } from '../../../redux/actions/constants/codeList'; import { Menu } from './menu'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const SeriesVisualizationContainer = () => { const { id } = useParams(); @@ -31,7 +31,7 @@ const SeriesVisualizationContainer = () => { (state) => state.operationsOrganisations.results || [] ); const categories = useCodesList(CL_SOURCE_CATEGORY); - const langs = useSelector((state) => getLocales(state)); + const langs = useLocales(); const [secondLang] = useSecondLang(); const frequency = frequencies.codes.find( diff --git a/src/packages/modules-structures/components/component-detail/index.js b/src/packages/modules-structures/components/component-detail/index.js index b2c30a18d..64a6a293f 100644 --- a/src/packages/modules-structures/components/component-detail/index.js +++ b/src/packages/modules-structures/components/component-detail/index.js @@ -2,15 +2,12 @@ import { useCallback, useState } from 'react'; import { DumbComponentDetailEdit } from './edit'; import { ComponentDetailView } from './view'; import ComponentTitle from './title'; -import { useSelector } from 'react-redux'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; export const ComponentDetail = (props) => { const [secondLang] = useSecondLang(); - const langs = useSelector((state) => { - const { lg1, lg2 } = state.app; - return { lg1, lg2 }; - }); + const langs = useLocales(); const [mode, setMode] = useState( !props.component?.labelLg1 ? 'EDIT' : 'VIEW' diff --git a/src/packages/modules-structures/components/component-detail/view-container.js b/src/packages/modules-structures/components/component-detail/view-container.js index 21856757b..ca900e77e 100644 --- a/src/packages/modules-structures/components/component-detail/view-container.js +++ b/src/packages/modules-structures/components/component-detail/view-container.js @@ -1,13 +1,13 @@ -import { useState, useEffect, useCallback } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { useGoBack } from '../../../utils/hooks/useGoBack'; import { Loading } from '../../../components'; import { ComponentDetailView } from './view'; import { ConceptsApi, StructureApi } from '../../../sdk'; import ComponentTitle from './title'; -import { useSelector } from 'react-redux'; import { useParams } from 'react-router-dom'; import { getFormattedCodeList } from '../../apis'; import { useSecondLang } from '../../../utils/hooks/second-lang'; +import { useLocales } from '../../../utils/hooks/useLocales'; const ViewContainer = (props) => { const goBack = useGoBack(); @@ -20,10 +20,7 @@ const ViewContainer = (props) => { const [codesLists, setCodesLists] = useState([]); const [serverSideError, setServerSideError] = useState(); const [attributes, setAttributes] = useState([]); - const langs = useSelector((state) => { - const { lg1, lg2 } = state.app; - return { lg1, lg2 }; - }); + const langs = useLocales(); const handleBack = useCallback( () => goBack('/structures/components'), diff --git a/src/packages/modules-structures/components/component-detail/view-container.spec.js b/src/packages/modules-structures/components/component-detail/view-container.spec.js index 55e493604..94d5ca756 100644 --- a/src/packages/modules-structures/components/component-detail/view-container.spec.js +++ b/src/packages/modules-structures/components/component-detail/view-container.spec.js @@ -3,12 +3,7 @@ import { renderWithAppContext } from '../../../tests-utils/render'; import configureStore from '../../../redux/configure-store'; import { Provider } from 'react-redux'; -const store = configureStore({ - app: { - lg1: 'fr', - lg2: 'en', - }, -}); +const store = configureStore({}); jest.mock('./view', () => ({ ComponentDetailView: () => <>, })); diff --git a/src/packages/redux/index.spec.ts b/src/packages/redux/index.spec.ts index d8ebf6af6..75dcab911 100644 --- a/src/packages/redux/index.spec.ts +++ b/src/packages/redux/index.spec.ts @@ -1,22 +1,6 @@ import * as selector from './selectors'; import { ReduxModel } from './model'; -describe('getLocales', () => { - it('should return the langs object', () => { - const input = { - app: { - lg1: 'lg1', - lg2: 'lg2', - }, - }; - const output = { - lg1: 'lg1', - lg2: 'lg2', - }; - expect(selector.getLocales(input as ReduxModel)).toEqual(output); - }); -}); - describe('getSerie', () => { it('should return an empty object if the value is undefined', () => { const input = {} as ReduxModel; diff --git a/src/packages/redux/model.ts b/src/packages/redux/model.ts index ac90bddef..da2e9c8fd 100644 --- a/src/packages/redux/model.ts +++ b/src/packages/redux/model.ts @@ -16,8 +16,6 @@ export type ReduxModel = { results: any[]; }; app?: { - lg1: string; - lg2: string; auth: { type: string; user: { diff --git a/src/packages/redux/selectors.ts b/src/packages/redux/selectors.ts index 89733e13e..0b032bc49 100644 --- a/src/packages/redux/selectors.ts +++ b/src/packages/redux/selectors.ts @@ -8,10 +8,6 @@ export const getPermission = (state: ReduxModel) => { return { authType, roles, stamp }; }; -export const getLocales = (state: ReduxModel) => { - const { lg1, lg2 } = state.app!; - return { lg1, lg2 }; -}; export const getSerie = (state: ReduxModel) => { return state.operationsSeriesCurrent || {}; }; diff --git a/src/packages/utils/hooks/useLocales.spec.tsx b/src/packages/utils/hooks/useLocales.spec.tsx new file mode 100644 index 000000000..cfc106f53 --- /dev/null +++ b/src/packages/utils/hooks/useLocales.spec.tsx @@ -0,0 +1,19 @@ +import { renderHook } from '@testing-library/react'; +import { useLocales } from './useLocales'; +import { AppContextProvider } from '../../application/app-context'; + +describe('useLocales', () => { + it('returns the correct initial value and toggle function', () => { + const wrapper = ({ children }: { children: React.ReactNode }) => ( + + {children} + + ); + + const { result } = renderHook(() => useLocales(), { wrapper }); + + const { lg1, lg2 } = result.current; + expect(lg1).toBe('English'); + expect(lg2).toBe('French'); + }); +}); diff --git a/src/packages/utils/hooks/useLocales.ts b/src/packages/utils/hooks/useLocales.ts new file mode 100644 index 000000000..502b6f982 --- /dev/null +++ b/src/packages/utils/hooks/useLocales.ts @@ -0,0 +1,6 @@ +import { useAppContext } from '../../application/app-context'; + +export const useLocales = (): { lg1: string; lg2: string } => { + const { lg1, lg2 } = useAppContext(); + return { lg1, lg2 }; +}; From b4bfba031ba0cf6ece26c9bcf06da1f22e9217d4 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Sat, 7 Sep 2024 12:38:29 +0100 Subject: [PATCH 2/4] feat: remove unused data from redux --- src/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index f447c3bda..f9f6b687d 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -52,8 +52,6 @@ const renderApp = (Component: any, initState: any, props?: any) => { const store = configureStore({ app: { auth: { type, user: { roles: [], stamp: '' } }, - lg1, - lg2, version, properties, error: false, From 3936879efddbdbbf6a4d72711440e298764d89d0 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Sat, 7 Sep 2024 20:45:52 +0100 Subject: [PATCH 3/4] feat: remove other properties from redux --- src/index.tsx | 4 +--- src/packages/application/app-context.tsx | 4 +++- src/packages/application/router/index.tsx | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index f9f6b687d..81abcbdc4 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -52,9 +52,7 @@ const renderApp = (Component: any, initState: any, props?: any) => { const store = configureStore({ app: { auth: { type, user: { roles: [], stamp: '' } }, - version, properties, - error: false, }, }); @@ -67,7 +65,7 @@ const renderApp = (Component: any, initState: any, props?: any) => { root.render( - +
diff --git a/src/packages/application/app-context.tsx b/src/packages/application/app-context.tsx index 457a268f2..07af45ccc 100644 --- a/src/packages/application/app-context.tsx +++ b/src/packages/application/app-context.tsx @@ -3,6 +3,7 @@ import { createContext, PropsWithChildren, useContext, useState } from 'react'; type AppContextTypes = { lg1: string; lg2: string; + version?: string; secondLang: { value: boolean; toggle: () => void; @@ -14,8 +15,9 @@ const AppContext = createContext(undefined); export const AppContextProvider = ({ lg1, lg2, + version, children, -}: PropsWithChildren>) => { +}: PropsWithChildren>) => { const [secondLang, setSecondLang] = useState(false); return ( diff --git a/src/packages/application/router/index.tsx b/src/packages/application/router/index.tsx index b3a1e987b..34a05b3e0 100644 --- a/src/packages/application/router/index.tsx +++ b/src/packages/application/router/index.tsx @@ -10,6 +10,7 @@ import { PropsWithChildren } from 'react'; import Auth from '../../auth/components/auth'; import { removeToken } from '../../auth/open-id-connect-auth/token-utils'; import { ADMIN } from '../../auth/roles'; +import { useAppContext } from '../app-context'; const logout = () => { removeToken(); @@ -22,7 +23,7 @@ export const RBACLink = ({ children }: PropsWithChildren<{}>) => { const authorizationHost = useSelector( (state) => (state as any).app.properties.authorizationHost ); - const version = useSelector((state) => (state as any).app.version); + const { version } = useAppContext(); const footer = `${getEnvVar('NAME')} - Front ${getEnvVar( 'VERSION' )} - API ${version}`; From 9f01a878b39372c844637aa8126c15550115693f Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Sun, 8 Sep 2024 20:26:51 +0100 Subject: [PATCH 4/4] feat: remove properties from redux --- src/index.tsx | 8 ++++++-- src/packages/application/app-context.spec.tsx | 2 +- src/packages/application/app-context.tsx | 14 +++++++++++++- src/packages/application/app.tsx | 6 ++---- src/packages/application/router/index.spec.tsx | 10 +++------- src/packages/application/router/index.tsx | 7 +++---- src/packages/application/router/routes.tsx | 12 +++++++----- .../edition-creation/creation-container.js | 9 +++++---- .../edition-creation/creation-container.js | 13 ++++++------- .../edition-creation/edition-container.js | 9 +++++---- src/packages/modules-operations/msd/index.js | 2 +- src/packages/tests-utils/render.tsx | 7 ++++++- src/packages/utils/hooks/second-lang.spec.tsx | 2 +- src/packages/utils/hooks/useLocales.spec.tsx | 2 +- 14 files changed, 60 insertions(+), 43 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 81abcbdc4..d703185bf 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -52,7 +52,6 @@ const renderApp = (Component: any, initState: any, props?: any) => { const store = configureStore({ app: { auth: { type, user: { roles: [], stamp: '' } }, - properties, }, }); @@ -65,7 +64,12 @@ const renderApp = (Component: any, initState: any, props?: any) => { root.render( - +
diff --git a/src/packages/application/app-context.spec.tsx b/src/packages/application/app-context.spec.tsx index ff8c2ae1a..72a3f33a0 100644 --- a/src/packages/application/app-context.spec.tsx +++ b/src/packages/application/app-context.spec.tsx @@ -19,7 +19,7 @@ const TestComponent = () => { describe('AppContext', () => { it('provides the correct context values', () => { render( - + ); diff --git a/src/packages/application/app-context.tsx b/src/packages/application/app-context.tsx index 07af45ccc..69f404a99 100644 --- a/src/packages/application/app-context.tsx +++ b/src/packages/application/app-context.tsx @@ -8,6 +8,13 @@ type AppContextTypes = { value: boolean; toggle: () => void; }; + properties: { + authorizationHost: string; + modules: string[]; + activeModules: string[]; + defaultContributor: string; + maxLengthScopeNote: string; + }; }; const AppContext = createContext(undefined); @@ -16,8 +23,11 @@ export const AppContextProvider = ({ lg1, lg2, version, + properties, children, -}: PropsWithChildren>) => { +}: PropsWithChildren< + Pick +>) => { const [secondLang, setSecondLang] = useState(false); return ( @@ -25,6 +35,8 @@ export const AppContextProvider = ({ value={{ lg1, lg2, + version, + properties, secondLang: { value: secondLang, toggle: () => setSecondLang((value) => !value), diff --git a/src/packages/application/app.tsx b/src/packages/application/app.tsx index 1662171b0..e0499ec8d 100755 --- a/src/packages/application/app.tsx +++ b/src/packages/application/app.tsx @@ -2,14 +2,12 @@ import { Link } from 'react-router-dom'; import D from '../deprecated-locales'; import 'bootstrap/dist/css/bootstrap.css'; import './app.scss'; -import { useSelector } from 'react-redux'; import { useTitle } from '../utils/hooks/useTitle'; +import { useAppContext } from './app-context'; function App() { useTitle(); - const modules = useSelector((state) => { - return (state as any).app.properties?.modules ?? []; - }); + const { properties: { modules = [] } = {} } = useAppContext(); const apps = modules.map((appName: string) => { const app = appName.trim(); diff --git a/src/packages/application/router/index.spec.tsx b/src/packages/application/router/index.spec.tsx index 500a500fc..9acd6eb23 100644 --- a/src/packages/application/router/index.spec.tsx +++ b/src/packages/application/router/index.spec.tsx @@ -3,7 +3,7 @@ import { Provider } from 'react-redux'; import { RBACLink } from '.'; import D from '../../i18n'; import configureStore from '../../redux/configure-store'; -import { renderWithRouter } from '../../tests-utils/render'; +import { renderWithAppContext } from '../../tests-utils/render'; import { removeToken } from '../../auth/open-id-connect-auth/token-utils'; jest.mock('react-router-dom', () => ({ @@ -27,10 +27,6 @@ const store = configureStore({ stamp: 'stamp', }, }, - properties: { - authorizationHost: 'http:/auth-host.com', - }, - version: '2.0.0', }, }); @@ -44,7 +40,7 @@ describe('RBACLink Component', () => { it('should render children and footer correctly', () => { useLocationMock.mockReturnValue({ pathname: '/' }); - renderWithRouter( + renderWithAppContext(
Child Component
@@ -60,7 +56,7 @@ describe('RBACLink Component', () => { it('should call logout and remove token when logout button is clicked', () => { useLocationMock.mockReturnValue({ pathname: '/' }); - renderWithRouter( + renderWithAppContext(
Child Component
diff --git a/src/packages/application/router/index.tsx b/src/packages/application/router/index.tsx index 34a05b3e0..58642e91c 100644 --- a/src/packages/application/router/index.tsx +++ b/src/packages/application/router/index.tsx @@ -5,7 +5,6 @@ import bauhausLogo from '../../../img/logo_noir.svg'; import { getEnvVar } from '../../utils/env'; import D from '../../i18n'; import 'react-app-polyfill/stable'; -import { useSelector } from 'react-redux'; import { PropsWithChildren } from 'react'; import Auth from '../../auth/components/auth'; import { removeToken } from '../../auth/open-id-connect-auth/token-utils'; @@ -20,9 +19,9 @@ const logout = () => { export const RBACLink = ({ children }: PropsWithChildren<{}>) => { const location = useLocation(); - const authorizationHost = useSelector( - (state) => (state as any).app.properties.authorizationHost - ); + const { + properties: { authorizationHost }, + } = useAppContext(); const { version } = useAppContext(); const footer = `${getEnvVar('NAME')} - Front ${getEnvVar( 'VERSION' diff --git a/src/packages/application/router/routes.tsx b/src/packages/application/router/routes.tsx index f951b3d3e..3a52fccd9 100644 --- a/src/packages/application/router/routes.tsx +++ b/src/packages/application/router/routes.tsx @@ -4,8 +4,8 @@ import { Route, Switch, Redirect } from 'react-router-dom'; import auth from '../../auth/hoc'; import App from '../app'; -import { useSelector } from 'react-redux'; import { Loading, NotFound, UnderMaintenance } from '../../components'; +import { useAppContext } from '../app-context'; const getComponent = ( pageName: string, @@ -43,10 +43,12 @@ const getHomePage = (pages: Record) => { ); }; export default auth(() => { - const activeModules = useSelector( - (state) => (state as any).app.properties.activeModules - ); - const modules = useSelector((state) => (state as any).app.properties.modules); + const { + properties: { activeModules }, + } = useAppContext(); + const { + properties: { modules }, + } = useAppContext(); const pages = useMemo(() => { return modules.reduce((acc: Record, appName: string) => { const app = appName.trim(); diff --git a/src/packages/modules-concepts/collections/edition-creation/creation-container.js b/src/packages/modules-concepts/collections/edition-creation/creation-container.js index afb1c17c7..4fb0954f2 100644 --- a/src/packages/modules-concepts/collections/edition-creation/creation-container.js +++ b/src/packages/modules-concepts/collections/edition-creation/creation-container.js @@ -1,5 +1,4 @@ import { useCallback, useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import buildPayload from '../../../modules-concepts/collections/utils/build-payload/build-payload'; import CollectionEditionCreation from './home'; @@ -12,13 +11,15 @@ import { ConceptsApi } from '../../../sdk'; import { CollectionApi } from '../../../sdk/collection-api'; import { useTitle } from '../../../utils/hooks/useTitle'; import { useLocales } from '../../../utils/hooks/useLocales'; +import { useAppContext } from '../../../application/app-context'; const CreationContainer = () => { const history = useHistory(); const langs = useLocales(); - const collection = useSelector((state) => - emptyCollection(state.app.properties.defaultContributor) - ); + const { + properties: { defaultContributor }, + } = useAppContext(); + const collection = emptyCollection(defaultContributor); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); diff --git a/src/packages/modules-concepts/edition-creation/creation-container.js b/src/packages/modules-concepts/edition-creation/creation-container.js index 0ac37f4c0..e341b1e9c 100644 --- a/src/packages/modules-concepts/edition-creation/creation-container.js +++ b/src/packages/modules-concepts/edition-creation/creation-container.js @@ -1,5 +1,4 @@ import { useCallback, useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import ConceptEditionCreation from './home'; import D from '../../deprecated-locales'; @@ -12,13 +11,15 @@ import emptyConcept from '../../modules-concepts/utils/empty-concept'; import { mergeWithAllConcepts } from '../utils/links'; import { sortArrayByLabel } from '../../utils/array-utils'; import { useLocales } from '../../utils/hooks/useLocales'; +import { useAppContext } from '../../application/app-context'; const CreationContainer = () => { const langs = useLocales(); - const maxLengthScopeNote = useSelector((state) => - Number(state.app.properties.maxLengthScopeNote) - ); + const maxLengthScopeNoteString = + useAppContext().properties.maxLengthScopeNote; + const maxLengthScopeNote = Number(maxLengthScopeNoteString); + const defaultContributor = useAppContext().properties.defaultContributor; const history = useHistory(); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); @@ -35,9 +36,7 @@ const CreationContainer = () => { .finally(() => setLoading(false)); }, []); - const concept = useSelector((state) => - emptyConcept(state.app.properties.defaultContributor) - ); + const concept = emptyConcept(defaultContributor); const handleCreation = useCallback( (data) => { diff --git a/src/packages/modules-concepts/edition-creation/edition-container.js b/src/packages/modules-concepts/edition-creation/edition-container.js index 08558200d..396bf063b 100644 --- a/src/packages/modules-concepts/edition-creation/edition-container.js +++ b/src/packages/modules-concepts/edition-creation/edition-container.js @@ -1,5 +1,4 @@ import { useCallback, useEffect, useState } from 'react'; -import { useSelector } from 'react-redux'; import { useHistory, useParams } from 'react-router-dom'; import ConceptEditionCreation from './home'; import D from '../../deprecated-locales'; @@ -14,6 +13,7 @@ import buildPayloadUpdate from '../../modules-concepts/utils/build-payload-creat import { mergeWithAllConcepts } from '../utils/links'; import { emptyNotes } from '../utils/notes'; import { useLocales } from '../../utils/hooks/useLocales'; +import { useAppContext } from '../../application/app-context'; const formatNotes = (notes) => { return Object.assign( @@ -30,9 +30,10 @@ const EditionContainer = () => { const history = useHistory(); const langs = useLocales(); - const maxLengthScopeNote = useSelector((state) => - Number(state.app.properties.maxLengthScopeNote) - ); + const maxLengthScopeNoteString = + useAppContext().properties.maxLengthScopeNote; + + const maxLengthScopeNote = Number(maxLengthScopeNoteString); const [concept, setConcept] = useState({}); const [concepts, setConcepts] = useState([]); diff --git a/src/packages/modules-operations/msd/index.js b/src/packages/modules-operations/msd/index.js index a8f376f4c..ddac5d5f6 100644 --- a/src/packages/modules-operations/msd/index.js +++ b/src/packages/modules-operations/msd/index.js @@ -282,7 +282,7 @@ const mapDispatchToProps = { const MSDContainerWithParent = (props) => { const match = useRouteMatch(); const { idParent } = props; - const parentType = match.params[0]; // withRouter utile + const parentType = match.params[0]; const [parent, setParent] = useState(props.parent); const [loading, setLoading] = useState(true); const [documentStores, setDocumentStores] = useState([]); diff --git a/src/packages/tests-utils/render.tsx b/src/packages/tests-utils/render.tsx index b93adefcc..1504d98b2 100644 --- a/src/packages/tests-utils/render.tsx +++ b/src/packages/tests-utils/render.tsx @@ -9,7 +9,12 @@ export const renderWithRouter = (component: React.ReactNode) => { export const renderWithAppContext = (component: React.ReactNode) => { return renderWithRouter( - + {component} ); diff --git a/src/packages/utils/hooks/second-lang.spec.tsx b/src/packages/utils/hooks/second-lang.spec.tsx index 702d42f99..638023a93 100644 --- a/src/packages/utils/hooks/second-lang.spec.tsx +++ b/src/packages/utils/hooks/second-lang.spec.tsx @@ -5,7 +5,7 @@ import { AppContextProvider } from '../../application/app-context'; describe('useSecondLang', () => { it('returns the correct initial value and toggle function', () => { const wrapper = ({ children }: { children: React.ReactNode }) => ( - + {children} ); diff --git a/src/packages/utils/hooks/useLocales.spec.tsx b/src/packages/utils/hooks/useLocales.spec.tsx index cfc106f53..e356d6ce4 100644 --- a/src/packages/utils/hooks/useLocales.spec.tsx +++ b/src/packages/utils/hooks/useLocales.spec.tsx @@ -5,7 +5,7 @@ import { AppContextProvider } from '../../application/app-context'; describe('useLocales', () => { it('returns the correct initial value and toggle function', () => { const wrapper = ({ children }: { children: React.ReactNode }) => ( - + {children} );