diff --git a/src/App.js b/src/App.js index ef65fcce..6969fafe 100644 --- a/src/App.js +++ b/src/App.js @@ -4,7 +4,10 @@ import { IntlProvider } from 'react-intl'; import { useDispatch, useSelector } from 'react-redux'; import { setLocale, setThemeFromLocalStorage } from './store/actions/app'; -import { getOrganizationData } from './store/actions/climateWarehouseActions'; +import { + getOrganizationData, + getPickLists, +} from './store/actions/climateWarehouseActions'; import { initiateSocket } from './store/actions/socket'; import { loadLocaleData } from './translations'; @@ -21,6 +24,7 @@ const App = () => { useEffect(() => { dispatch(initiateSocket()); dispatch(getOrganizationData()); + dispatch(getPickLists()); }, [dispatch]); useEffect( @@ -49,7 +53,8 @@ const App = () => { + messages={translationTokens.default} + > diff --git a/src/store/actions/climateWarehouseActions.js b/src/store/actions/climateWarehouseActions.js index b9884ed6..40a3982e 100644 --- a/src/store/actions/climateWarehouseActions.js +++ b/src/store/actions/climateWarehouseActions.js @@ -36,6 +36,7 @@ export const actions = keyMirror( 'GET_VINTAGES', 'GET_STAGING_DATA', 'GET_ORGANIZATIONS', + 'GET_PICKLISTS', ); const getClimateWarehouseTable = ( @@ -130,6 +131,47 @@ export const getOrganizationData = () => { }; }; +export const getPickLists = () => { + return async dispatch => { + dispatch(activateProgressIndicator); + + const tryToGetPickListsFromStorage = () => { + const fromStorage = localStorage.getItem('pickLists'); + if (fromStorage) { + dispatch({ + type: actions.GET_PICKLISTS, + payload: JSON.parse(fromStorage), + }); + } else { + dispatch(setGlobalErrorMessage('Something went wrong...')); + } + }; + + try { + const response = await fetch( + `https://climate-warehouse.s3.us-west-2.amazonaws.com/public/picklists.json`, + ); + + if (response.ok) { + const results = await response.json(); + + dispatch({ + type: actions.GET_PICKLISTS, + payload: results, + }); + + localStorage.setItem('pickLists', JSON.stringify(results)); + } else { + tryToGetPickListsFromStorage(); + } + } catch { + tryToGetPickListsFromStorage(); + } finally { + dispatch(deactivateProgressIndicator); + } + }; +}; + export const getStagingData = ({ useMockedResponse = false }) => { return async dispatch => { dispatch(activateProgressIndicator); diff --git a/src/store/reducers/climateWarehouseReducer.js b/src/store/reducers/climateWarehouseReducer.js index b105711f..4c1e0d4a 100644 --- a/src/store/reducers/climateWarehouseReducer.js +++ b/src/store/reducers/climateWarehouseReducer.js @@ -14,6 +14,7 @@ const initialState = { vintages: null, stagingData: null, organizations: null, + pickLists: null, }; const climateWarehouseReducer = (state = initialState, action) => { @@ -21,6 +22,9 @@ const climateWarehouseReducer = (state = initialState, action) => { case climateWarehouseActions.GET_ORGANIZATIONS: return u({ organizations: action.payload }, state); + case climateWarehouseActions.GET_PICKLISTS: + return u({ pickLists: action.payload }, state); + case climateWarehouseActions.GET_RATINGS: return u({ ratings: action.payload }, state);