From d22b48f845b40ef717a17e593681a6d6cdc00591 Mon Sep 17 00:00:00 2001 From: Simona Domnisoru Date: Fri, 19 Jan 2024 14:55:39 +0100 Subject: [PATCH] feat: handle 404 response --- cypress/e2e/sharedSteps.js | 4 ++-- .../components/DataStore/DataStore.epics.js | 21 +++++++++++++------ .../useNewDashboard.reducerDescription.js | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cypress/e2e/sharedSteps.js b/cypress/e2e/sharedSteps.js index a61bdd5b89..6fa9febef3 100644 --- a/cypress/e2e/sharedSteps.js +++ b/cypress/e2e/sharedSteps.js @@ -214,7 +214,7 @@ Then(/^you see the opt in component for (.*)$/, (program) => { }); And('the data store is clean', () => { - cy.buildApiUrl('dataStore', 'capture/useNewDashboard') + cy.buildApiUrl('dataStore/capture/useNewDashboard') .then(dataStoreUrl => - cy.request('PUT', dataStoreUrl, {})); + cy.request({ method: 'DELETE', url: dataStoreUrl, failOnStatusCode: false })); }); diff --git a/src/core_modules/capture-core/components/DataStore/DataStore.epics.js b/src/core_modules/capture-core/components/DataStore/DataStore.epics.js index c79542d77d..71c1cee90c 100644 --- a/src/core_modules/capture-core/components/DataStore/DataStore.epics.js +++ b/src/core_modules/capture-core/components/DataStore/DataStore.epics.js @@ -1,6 +1,6 @@ // @flow import { ofType } from 'redux-observable'; -import { mergeMap, catchError } from 'rxjs/operators'; +import { flatMap, catchError } from 'rxjs/operators'; import { EMPTY } from 'rxjs'; import { saveDataStore } from './DataStore.actions'; import { type UseNewDashboard } from './DataStore.types'; @@ -8,10 +8,13 @@ import { appStartActionTypes } from '../../../../components/AppStart'; import { programCollection } from '../../metaDataMemoryStores'; const setNewDashboardByDefault = (key: string, dataStoreValues) => { + if (!dataStoreValues) { + return {}; + } const programs = [...programCollection.keys()]; const valuesWithDefault = programs.reduce((acc, program) => { const dataStoreValue = dataStoreValues[program]; - acc[program] = dataStoreValue !== undefined ? dataStoreValue : true; + acc[program] = dataStoreValue === undefined ? true : dataStoreValue; return acc; }, {}); @@ -31,9 +34,15 @@ const getUserDataStoreFromApi = async querySingleResource => export const fetchDataStoreEpic = (action$: InputObservable, _: ReduxStore, { querySingleResource }: ApiUtils) => action$.pipe( ofType(appStartActionTypes.APP_LOAD_SUCESS), - mergeMap(async () => { - const apiDataStore: UseNewDashboard = await getDataStoreFromApi(querySingleResource); - // $FlowFixMe + flatMap(async () => { + const apiDataStore: ?UseNewDashboard = await getDataStoreFromApi(querySingleResource) + .catch((error) => { + if (error.details.httpStatusCode === 404) { + return {}; + } + return undefined; + }); + return saveDataStore(setNewDashboardByDefault('dataStore', apiDataStore)); }), catchError(() => EMPTY), @@ -42,7 +51,7 @@ export const fetchDataStoreEpic = (action$: InputObservable, _: ReduxStore, { qu export const fetchUserDataStoreEpic = (action$: InputObservable, _: ReduxStore, { querySingleResource }: ApiUtils) => action$.pipe( ofType(appStartActionTypes.APP_LOAD_SUCESS), - mergeMap(async () => { + flatMap(async () => { const apiUserDataStore: UseNewDashboard = await getUserDataStoreFromApi(querySingleResource); // $FlowFixMe return saveDataStore(setNewDashboardByDefault('userDataStore', apiUserDataStore)); diff --git a/src/core_modules/capture-core/reducers/descriptions/useNewDashboard.reducerDescription.js b/src/core_modules/capture-core/reducers/descriptions/useNewDashboard.reducerDescription.js index c1dc56f889..a2bd768063 100644 --- a/src/core_modules/capture-core/reducers/descriptions/useNewDashboard.reducerDescription.js +++ b/src/core_modules/capture-core/reducers/descriptions/useNewDashboard.reducerDescription.js @@ -6,8 +6,8 @@ export const useNewDashboardDesc = createReducerDescription({ [dataStoreActionTypes.SAVE_DATA_STORE]: (state, action) => { const newState = { ...state }; const { dataStore, userDataStore } = action.payload; - dataStore && (newState.dataStore = dataStore); - userDataStore && (newState.userDataStore = userDataStore); + newState.dataStore = dataStore; + newState.userDataStore = userDataStore; return newState; },