Skip to content

Commit

Permalink
feat: populate picklists dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Feb 4, 2022
1 parent ebdab00 commit c3b2944
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,6 +24,7 @@ const App = () => {
useEffect(() => {
dispatch(initiateSocket());
dispatch(getOrganizationData());
dispatch(getPickLists());
}, [dispatch]);

useEffect(
Expand Down Expand Up @@ -49,7 +53,8 @@ const App = () => {
<IntlProvider
locale="en"
defaultLocale="en"
messages={translationTokens.default}>
messages={translationTokens.default}
>
<AppNavigator />
</IntlProvider>
</ThemeProvider>
Expand Down
42 changes: 42 additions & 0 deletions src/store/actions/climateWarehouseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const actions = keyMirror(
'GET_VINTAGES',
'GET_STAGING_DATA',
'GET_ORGANIZATIONS',
'GET_PICKLISTS',
);

const getClimateWarehouseTable = (
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/store/reducers/climateWarehouseReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ const initialState = {
vintages: null,
stagingData: null,
organizations: null,
pickLists: null,
};

const climateWarehouseReducer = (state = initialState, action) => {
switch (action.type) {
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);

Expand Down

0 comments on commit c3b2944

Please sign in to comment.