Skip to content

Commit

Permalink
Merge branch 'develop' into feat-Create-Redux-Store-for-Project-Ratings-
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Dec 1, 2021
2 parents 3b13d02 + a3cd122 commit 23f73c0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/mocks/co-benefits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
52 changes: 48 additions & 4 deletions src/store/actions/climateWarehouseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import _ from 'lodash';
import { keyMirror } from '../store-functions';
import constants from '../../constants';

import { coBenefitResponseStub } from '../../mocks';
import { mockedProjectRatingsResponse } from '../../mocks';

import {
activateProgressIndicator,
deactivateProgressIndicator,
setGlobalErrorMessage,
} from './app';

import { mockedProjectRatingsResponse } from '../../mocks';

export const actions = keyMirror('GET_RATINGS_DATA');
export const actions = keyMirror('GET_RATINGS_DATA', 'GET_COBENEFITS');

export const mockRatingsResponse = {
type: actions.GET_RATINGS_DATA,
Expand All @@ -22,7 +23,10 @@ export const mockRatingsResponse = {
),
};

export const getRatingsData = ({ useMockedResponse = false, useApiMock = false }) => {
export const getRatingsData = ({
useMockedResponse = false,
useApiMock = false,
}) => {
return async dispatch => {
dispatch(activateProgressIndicator);

Expand All @@ -49,3 +53,43 @@ export const getRatingsData = ({ useMockedResponse = false, useApiMock = false }
}
};
};

const mockedCoBenefitResponse = {
type: actions.GET_COBENEFITS,
// Different envs import this differently
payload: _.get(coBenefitResponseStub, 'default', coBenefitResponseStub),
};

export const getCoBenefits = ({
useMockedResponse = false,
useApiMock = false,
}) => {
return async dispatch => {
try {
dispatch(activateProgressIndicator);

if (useMockedResponse) {
dispatch(mockedCoBenefitResponse);
} else {
let url = `${constants.API_HOST}/co-benefits`;
if (useApiMock) {
url = `${url}?useMock=true`;
}

const response = fetch(url);

if (response.ok) {
const results = await response.json();
dispatch({
type: actions.GET_COBENEFITS,
payload: results,
});
}
}
} catch {
dispatch(setGlobalErrorMessage('Something went wrong...'));
} finally {
dispatch(deactivateProgressIndicator);
}
};
};
8 changes: 6 additions & 2 deletions src/store/reducers/climateWarehouseReducer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import u from 'updeep';

import { actions as appActions } from '../actions/climateWarehouseActions';
import { actions as climateWarehouseActions } from '../actions/climateWarehouseActions';

const initialState = {
ratings: null,
coBenefits: null,
};

const climateWarehouseReducer = (state = initialState, action) => {
switch (action.type) {
case appActions.GET_RATINGS_DATA:
case climateWarehouseActions.GET_RATINGS_DATA:
return u({ ratings: action.payload }, state);

case climateWarehouseActions.GET_COBENEFITS:
return u({ coBenefits: action.payload }, state);

default:
return state;
}
Expand Down

0 comments on commit 23f73c0

Please sign in to comment.