Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Fetch client country code and write to app state #541

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- Add django-waffle and configure Django & React apps to enable feature flags [#531](https://github.com/open-apparel-registry/open-apparel-registry/pull/531)
- Support uploading Excel files [#532](https://github.com/open-apparel-registry/open-apparel-registry/pull/532)
- Fetch client country code based on IP [#541](https://github.com/open-apparel-registry/open-apparel-registry/pull/541)

### Changed
- Show active facility list names and descriptions on profile page [#534](https://github.com/open-apparel-registry/open-apparel-registry/pull/534)
Expand Down
3 changes: 3 additions & 0 deletions src/app/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import './App.css';

import { sessionLogin } from './actions/auth';
import { fetchFeatureFlags } from './actions/featureFlags';
import { fetchClientInfo } from './actions/clientInfo';

import {
mainRoute,
Expand Down Expand Up @@ -58,6 +59,7 @@ const appStyles = Object.freeze({
class App extends Component {
componentDidMount() {
this.props.getFeatureFlags();
this.props.getClientInfo();
return this.props.logIn();
}

Expand Down Expand Up @@ -137,6 +139,7 @@ App.propTypes = {
function mapDispatchToProps(dispatch) {
return {
getFeatureFlags: () => dispatch(fetchFeatureFlags()),
getClientInfo: () => dispatch(fetchClientInfo()),
logIn: () => dispatch(sessionLogin()),
};
}
Expand Down
27 changes: 27 additions & 0 deletions src/app/src/actions/clientInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createAction } from 'redux-act';

import csrfRequest from '../util/csrfRequest';

import {
makeGetClientInfoURL,
logErrorAndDispatchFailure,
} from '../util/util';

export const startFetchClientInfo = createAction('START_FETCH_CLIENT_INFO');
export const failFetchClientInfo = createAction('FAIL_FETCH_CLIENT_INFO');
export const completeFetchClientInfo = createAction('COMPLETE_FETCH_CLIENT_INFO');

export function fetchClientInfo() {
return (dispatch) => {
dispatch(startFetchClientInfo());

return csrfRequest
.get(makeGetClientInfoURL())
.then(({ data }) => dispatch(completeFetchClientInfo(data)))
.catch(err => dispatch(logErrorAndDispatchFailure(
err,
'An error prevented fetching client info',
failFetchClientInfo,
)));
};
}
21 changes: 21 additions & 0 deletions src/app/src/reducers/ClientInfoReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createReducer } from 'redux-act';
import update from 'immutability-helper';

import {
startFetchClientInfo,
failFetchClientInfo,
completeFetchClientInfo,
} from '../actions/clientInfo';

const initialState = Object.freeze({ fetched: false, countryCode: null });

export default createReducer({
[startFetchClientInfo]: () => initialState,
[failFetchClientInfo]: state => update(state, {
fetched: { $set: true },
}),
[completeFetchClientInfo]: (state, payload) => update(state, {
fetched: { $set: true },
countryCode: { $set: payload.country.code },
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there ever be a need to clear this during the running of the app? It seems like the answer is no but I thought I'd check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we would ever need to clear the fetched client info.

}, initialState);
2 changes: 2 additions & 0 deletions src/app/src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import UIReducer from './UIReducer';
import FacilitiesReducer from './FacilitiesReducer';
import FacilityCountReducer from './FacilityCountReducer';
import FeatureFlagsReducer from './FeatureFlagsReducer';
import ClientInfoReducer from './ClientInfoReducer';

export default combineReducers({
auth: AuthReducer,
Expand All @@ -30,4 +31,5 @@ export default combineReducers({
facilities: FacilitiesReducer,
facilityCount: FacilityCountReducer,
featureFlags: FeatureFlagsReducer,
clientInfo: ClientInfoReducer,
});
2 changes: 2 additions & 0 deletions src/app/src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export const makeGetFacilitiesCountURL = () => '/api/facilities/count/';

export const makeGetAPIFeatureFlagsURL = () => '/api-feature-flags/';

export const makeGetClientInfoURL = () => 'https://api.userinfo.io/userinfos';

export const getValueFromObject = ({ value }) => value;

export const createQueryStringFromSearchFilters = ({
Expand Down