Skip to content

Commit

Permalink
Use local var to store the redux store
Browse files Browse the repository at this point in the history
Signed-off-by: Florent MILLOT <millotflo@gmail.com>
  • Loading branch information
flomillot committed May 15, 2024
1 parent 613ffcf commit f6f1560
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 82 deletions.
4 changes: 0 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@
"react-hook-form": "^7.51.2",
"react-intl": "^6.6.4",
"react-papaparse": "^4.1.0",
"react-redux": "^9.1.2",
"react-router-dom": "^6.22.3",
"redux": "^5.0.1",
"yup": "^1.4.0"
},
"devDependencies": {
Expand Down Expand Up @@ -120,10 +118,8 @@
"react-hook-form": "^7.51.2",
"react-intl": "^6.6.4",
"react-papaparse": "^4.1.0",
"react-redux": "^9.1.2",
"react-resizable": "^3.0.5",
"react-router-dom": "^6.22.3",
"redux": "^5.0.1",
"ts-node": "^10.9.2",
"type-fest": "^4.14.0",
"typescript": "5.1.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import { FilterType } from '../constants/filter-constants';
import { FetchStatus } from '../../../utils/FetchStatus.ts';
import { ElementType } from '../../../utils/ElementType.ts';
import { saveFilter } from '../../../services/explore';
import { useSelector } from 'react-redux';
import { CommonReduxState } from '../../../redux/reducer.type';

export type SelectionCopy = {
sourceItemUuid: UUID | null;
Expand Down Expand Up @@ -98,10 +96,6 @@ const CriteriaBasedFilterEditionDialog: FunctionComponent<
}) => {
const { snackError } = useSnackMessage();

const userToken = useSelector(
(state: CommonReduxState) => state.user.id_token
);

const [dataFetchStatus, setDataFetchStatus] = useState(FetchStatus.IDLE);

// default values are set via reset when we fetch data
Expand Down Expand Up @@ -148,8 +142,7 @@ const CriteriaBasedFilterEditionDialog: FunctionComponent<
(filterForm: any) => {
saveFilter(
frontToBackTweak(id, filterForm),
filterForm[FieldConstants.NAME],
userToken
filterForm[FieldConstants.NAME]
)
.then(() => {
if (selectionForCopy.sourceItemUuid === id) {
Expand All @@ -171,7 +164,6 @@ const CriteriaBasedFilterEditionDialog: FunctionComponent<
selectionForCopy.sourceItemUuid,
snackError,
setSelelectionForCopy,
userToken,
]
);

Expand Down
10 changes: 1 addition & 9 deletions src/components/filter/expert/expert-filter-edition-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import { FilterContext } from '../filter-context';
import { FilterType } from '../constants/filter-constants';
import { FetchStatus } from '../../../utils/FetchStatus';
import { ElementAttributes } from '../../../utils/types.ts';
import { CommonReduxState } from '../../../redux/reducer.type';
import { useSelector } from 'react-redux';

const formSchema = yup
.object()
Expand Down Expand Up @@ -84,10 +82,6 @@ const ExpertFilterEditionDialog: FunctionComponent<
}) => {
const { snackError } = useSnackMessage();

const userToken = useSelector(
(state: CommonReduxState) => state.user.id_token
);

const [dataFetchStatus, setDataFetchStatus] = useState(FetchStatus.IDLE);

// default values are set via reset when we fetch data
Expand Down Expand Up @@ -148,8 +142,7 @@ const ExpertFilterEditionDialog: FunctionComponent<
snackError({
messageTxt: error,
});
},
userToken
}
);
if (selectionForCopy.sourceItemUuid === id) {
setSelectionForCopy(noSelectionForCopy);
Expand All @@ -165,7 +158,6 @@ const ExpertFilterEditionDialog: FunctionComponent<
selectionForCopy.sourceItemUuid,
snackError,
setSelectionForCopy,
userToken,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { UUID } from 'crypto';
import { elementExistsType } from '../criteria-based/criteria-based-filter-edition-dialog';
import { FilterType } from '../constants/filter-constants';
import { FetchStatus } from '../../../utils/FetchStatus.ts';
import { useSelector } from 'react-redux';
import { CommonReduxState } from '../../../redux/reducer.type';

const formSchema = yup
.object()
Expand Down Expand Up @@ -73,10 +71,6 @@ const ExplicitNamingFilterEditionDialog: FunctionComponent<
const { snackError } = useSnackMessage();
const [dataFetchStatus, setDataFetchStatus] = useState(FetchStatus.IDLE);

const userToken = useSelector(
(state: CommonReduxState) => state.user.id_token
);

// default values are set via reset when we fetch data
const formMethods = {
...useForm({
Expand Down Expand Up @@ -138,8 +132,7 @@ const ExplicitNamingFilterEditionDialog: FunctionComponent<
});
},
onClose,
undefined,
userToken
undefined
);
if (selectionForCopy.sourceItemUuid === id) {
setSelectionForCopy(noSelectionForCopy);
Expand All @@ -155,7 +148,6 @@ const ExplicitNamingFilterEditionDialog: FunctionComponent<
onClose,
snackError,
setSelectionForCopy,
userToken,
]
);

Expand Down
17 changes: 4 additions & 13 deletions src/components/filter/filter-creation-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import { StudyMetadata } from '../../hooks/predefined-properties-hook.ts';
import { FilterContext } from './filter-context';
import { FilterType } from './constants/filter-constants';
import { ElementAttributes } from '../../utils/types.ts';
import { useSelector } from 'react-redux';
import { CommonReduxState } from '../../redux/reducer.type';

const emptyFormData = {
[FieldConstants.NAME]: '',
Expand Down Expand Up @@ -101,10 +99,6 @@ const FilterCreationDialog: FunctionComponent<FilterCreationDialogProps> = ({
}) => {
const { snackError } = useSnackMessage();

const userToken = useSelector(
(state: CommonReduxState) => state.user.id_token
);

const formMethods = {
...useForm({
defaultValues: emptyFormData,
Expand Down Expand Up @@ -139,8 +133,7 @@ const FilterCreationDialog: FunctionComponent<FilterCreationDialogProps> = ({
});
},
onClose,
activeDirectory,
userToken
activeDirectory
);
} else if (
filterForm[FieldConstants.FILTER_TYPE] ===
Expand All @@ -154,8 +147,7 @@ const FilterCreationDialog: FunctionComponent<FilterCreationDialogProps> = ({
snackError({
messageTxt: error,
});
},
userToken
}
);
} else if (
filterForm[FieldConstants.FILTER_TYPE] === FilterType.EXPERT.id
Expand All @@ -173,12 +165,11 @@ const FilterCreationDialog: FunctionComponent<FilterCreationDialogProps> = ({
snackError({
messageTxt: error,
});
},
userToken
}
);
}
},
[activeDirectory, snackError, onClose, userToken]
[activeDirectory, snackError, onClose]
);

return (
Expand Down
6 changes: 3 additions & 3 deletions src/components/filter/utils/filter-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const saveExplicitNamingFilter = (
setCreateFilterErr: (value: any) => void,
handleClose: () => void,
activeDirectory: UUID | undefined,
token: string
token?: string
) => {
// we remove unnecessary fields from the table
let cleanedTableValues;
Expand Down Expand Up @@ -82,7 +82,7 @@ export const saveCriteriaBasedFilter = (
activeDirectory: any,
onClose: () => void,
onError: (message: string) => void,
token: string
token?: string
) => {
const filterForBack = frontToBackTweak(undefined, filter); // no need ID for creation
createFilter(
Expand Down Expand Up @@ -110,7 +110,7 @@ export const saveExpertFilter = (
activeDirectory: any,
onClose: () => void,
onError: (message: string) => void,
token: string
token?: string
) => {
if (isFilterCreation) {
createFilter(
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,5 @@ export {
getCriteriaBasedFormData,
getCriteriaBasedSchema,
} from './components/filter/criteria-based/criteria-based-filter-utils';

export { setCommonStore } from './redux/commonStore';
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,5 @@ export {
getCriteriaBasedFormData,
getCriteriaBasedSchema,
} from './components/filter/criteria-based/criteria-based-filter-utils';

export { setCommonStore } from './redux/commonStore';
27 changes: 27 additions & 0 deletions src/redux/commonStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2024, 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/.
*/

import { User } from 'oidc-client';

interface CommonStore {
getState: () => { user: User };
}

let commonStore: CommonStore | undefined;

/**
* Set a copy of the reference to the store to be able to access it from this library.
* It's useful to get access to the user token outside of the React context in API files.
* NB : temporary solution before refactoring the token management in the whole gridsuite stack.
*/
export function setCommonStore(store: CommonStore): void {
commonStore = store;
}

export function getUserToken() {
return commonStore?.getState().user.id_token;
}
26 changes: 0 additions & 26 deletions src/redux/reducer.type.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/services/explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createFilter(
name: string,
description: string,
parentDirectoryUuid: UUID | undefined,
token: string
token?: string
) {
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('name', name);
Expand All @@ -36,7 +36,7 @@ export function createFilter(
);
}

export function saveFilter(filter: any, name: string, token: string) {
export function saveFilter(filter: any, name: string, token?: string) {
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('name', name);
return backendFetch(
Expand Down
11 changes: 8 additions & 3 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

export const backendFetch = (url: string, init: any, token: string) => {
import { getUserToken } from '../redux/commonStore.ts';

export const backendFetch = (url: string, init: any, token?: string) => {
const initCopy = prepareRequest(init, token);
return safeFetch(url, initCopy);
};

const prepareRequest = (init: any, token: string) => {
const prepareRequest = (init: any, token?: string) => {
if (!(typeof init == 'undefined' || typeof init == 'object')) {
throw new TypeError(
'Argument 2 of backendFetch is not an object : ' + typeof init
);
}
const initCopy = Object.assign({}, init);
initCopy.headers = new Headers(initCopy.headers || {});
initCopy.headers.append('Authorization', 'Bearer ' + token);
initCopy.headers.append(
'Authorization',
'Bearer ' + token ?? getUserToken()
);
return initCopy;
};

Expand Down

0 comments on commit f6f1560

Please sign in to comment.