Skip to content

check for undefined state in thunk actions when hmr #3686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/javascript/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ export const DASHBOARDS = 'location/DASHBOARDS';
export const DASHBOARDS_EMBED = 'location/DASHBOARDS_EMBED';

const routeChangeThunk = (dispatch, getState) => {
const currentLocation = getState().location.pathname;
const prevLocation = getState().location.prev.pathname;
const { location } = getState() || {};
const currentLocation = location.pathname;
const prevLocation = location.prev.pathname;
if (currentLocation !== prevLocation) {
handlePageTrack(getState().location);
handlePageTrack(location);
}
};

const redirectThunk = (dispatch, getState) => {
const { location } = getState();
const { location } = getState() || {};
const routeSlugs = location.pathname && location.pathname.split('/');
const isOldMap = routeSlugs.includes('map');
if (isOldMap) {
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/components/maps/components/analysis/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export const uploadShape = createThunkAction(
export const clearAnalysis = createThunkAction(
'clearAnalysis',
() => (dispatch, getState) => {
const { query, type } = getState().location;
const { query, type } = getState().location || {};
dispatch({
type,
...(query && {
Expand All @@ -179,7 +179,8 @@ export const clearAnalysis = createThunkAction(
export const goToDashboard = createThunkAction(
'goToDashboard',
() => (dispatch, getState) => {
const { payload, query } = getState().location.query;
const { location } = getState() || {};
const { payload, query } = location || {};
dispatch({
type: DASHBOARDS,
payload,
Expand Down
13 changes: 7 additions & 6 deletions app/javascript/components/maps/components/menu/menu-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,17 @@ export const getLocationFromSearch = createThunkAction(
export const handleClickLocation = createThunkAction(
'handleClickLocation',
({ gid_0, gid_1, gid_2 }) => (dispatch, getState) => {
const query = getState().location.query || {};
const location = parseGadm36Id(gid_2 || gid_1 || gid_0);
const { map, menu, mainMap } = getState().location.query || {};
const { location } = getState();
const query = (location && location.query) || {};
const newLocation = parseGadm36Id(gid_2 || gid_1 || gid_0);
const { map, menu, mainMap } = query || {};

if (location) {
if (newLocation) {
dispatch({
type: MAP,
payload: {
type: 'country',
...location
...newLocation
},
query: {
...query,
Expand Down Expand Up @@ -154,7 +155,7 @@ export const handleViewOnMap = createThunkAction(
export const showAnalysis = createThunkAction(
'showAnalysis',
() => (dispatch, getState) => {
const { query, type, payload } = getState().location;
const { query, type, payload } = getState().location || {};
const { menu } = query || {};
dispatch({
type,
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/components/maps/main-map/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const setMainMapAnalysisView = createThunkAction(
({ data, layer }) => (dispatch, getState) => {
const { cartodb_id, wdpaid } = data || {};
const { analysisEndpoint, tableName } = layer || {};
const { query, type } = getState().location;
const { query, type } = getState().location || {};
const { map, mainMap } = query || {};

// get location payload based on layer type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const setMapTourOpen = createAction('setMapTourOpen');
export const setExploreView = createThunkAction(
'setExploreView',
() => (dispatch, getState) => {
const { query, type, payload } = getState().location;
const { query, type, payload } = getState().location || {};
dispatch({
type,
payload,
Expand All @@ -22,7 +22,7 @@ export const setExploreView = createThunkAction(
export const setAnalysisView = createThunkAction(
'setAnalysisView',
() => (dispatch, getState) => {
const { query, type, payload } = getState().location;
const { query, type, payload } = getState().location || {};
dispatch({
type,
payload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const setRecentImagerySettings = createThunkAction(
export const getRecentImageryData = createThunkAction(
'getRecentImageryData',
params => (dispatch, getState) => {
if (!getState().recentImagery.loading) {
const { recentImagery } = getState();
if (recentImagery && !recentImagery.loading) {
dispatch(setRecentImageryLoading(true));
getRecentTiles({ ...params })
.then(response => {
Expand Down Expand Up @@ -74,7 +75,8 @@ export const getRecentImageryData = createThunkAction(
export const getMoreTiles = createThunkAction(
'getMoreTiles',
params => (dispatch, getState) => {
if (!getState().recentImagery.loadingMoreTiles) {
const { recentImagery } = getState();
if (recentImagery && !recentImagery.loadingMoreTiles) {
dispatch(
setRecentImageryLoadingMoreTiles({
loadingMoreTiles: true,
Expand All @@ -96,7 +98,7 @@ export const getMoreTiles = createThunkAction(
thumbsReponse.data.data.attributes;

if (tiles && thumbs) {
const data = getState().recentImagery.data.slice();
const data = recentImagery && recentImagery.data.slice();
const requestedTiles = dataStatus.requestedTiles + tiles.length;
const haveAllData = requestedTiles >= data.length;
const newData = data.map((d, i) => {
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/components/maps/map/components/draw/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const setDrawLoading = createAction('setDrawLoading');
export const getGeostoreId = createThunkAction(
'getGeostoreId',
geojson => (dispatch, getState) => {
if (!getState().analysis.loading) {
const { analysis } = getState();
if (analysis && !analysis.loading) {
dispatch(setDrawLoading({ loading: true, error: false, geostoreId: '' }));
getGeostoreKey(geojson)
.then(geostore => {
Expand All @@ -33,7 +34,7 @@ export const setDrawnGeostore = createThunkAction(
'setDrawnGeostore',
geostoreId => (dispatch, getState) => {
track('analysisDrawComplete');
const { query, type } = getState().location;
const { query, type } = getState().location || {};
const { map } = query || {};
dispatch({
type,
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/components/modals/subscribe/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const setSubscribeSettings = createThunkAction(
export const saveSubscription = createThunkAction(
'saveSubscription',
data => (dispatch, getState) => {
if (!getState().modalSubscribe.saving) {
const { modalSubscribe } = getState();
if (modalSubscribe && !modalSubscribe.saving) {
dispatch(setSubscribeSaving({ saving: true, error: false }));
const {
name,
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/components/modals/welcome/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const setModalWelcome = createThunkAction(
export const setExploreView = createThunkAction(
'setExploreView',
() => (dispatch, getState) => {
const { query, type, payload } = getState().location;
const { query, type, payload } = getState().location || {};
dispatch(setModalWelcome(false));
dispatch({
type,
Expand All @@ -31,7 +31,7 @@ export const setExploreView = createThunkAction(
export const setAnalysisView = createThunkAction(
'setAnalysisView',
() => (dispatch, getState) => {
const { query, type, payload } = getState().location;
const { query, type, payload } = getState().location || {};
dispatch(setModalWelcome(false));
dispatch({
type,
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/components/widgets/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const setWidgetLoading = createAction('setWidgetLoading');
export const getWidgetsData = createThunkAction(
'getWidgetsData',
() => (dispatch, getState) => {
if (!getState().widgets.loading) {
const { widgets } = getState();
if (widgets && !widgets.loading) {
dispatch(setWidgetsLoading({ loading: true, error: false }));
getNonGlobalDatasets()
.then(response => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const setImpactsProjectsData = createAction('setImpactsProjectsData');
export const fetchImpactProjects = createThunkAction(
'fetchImpactProjects',
() => (dispatch, getState) => {
if (!getState().impacts.loading) {
const { impacts } = getState();
if (impacts && !impacts.loading) {
dispatch(setImpactsProjectsLoading({ loading: true, error: false }));
fetchAboutProjects()
.then(data => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const setCategorySelected = createAction('setCategorySelected');
export const fetchProjects = createThunkAction(
'fetchProjects',
() => (dispatch, getState) => {
if (!getState().aboutProjects.loading) {
const { aboutProjects } = getState();
if (aboutProjects && !aboutProjects.loading) {
dispatch(setProjectsLoading({ loading: true, error: false }));
fetchAllProjects()
.then(data => {
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/pages/dashboards/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createThunkAction } from 'redux-tools';
export const handleCategoryChange = createThunkAction(
'handleCategoryChange',
category => (dispatch, getState) => {
const { query, type, payload } = getState().location;
const { query, type, payload } = getState().location || {};
dispatch({
type,
payload,
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/pages/dashboards/header/header-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const getHeaderData = createThunkAction(
export const handleLocationChange = createThunkAction(
'handleLocationChange',
location => (dispatch, getState) => {
const { query, type } = getState().location;
const { query, type } = getState().location || {};
const newQuery = {};

if (query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const setSearch = createAction('setSearch');
export const fetchProjects = createThunkAction(
'fetchProjects',
() => (dispatch, getState) => {
if (!getState().sgfProjects.loading) {
const { sgfProjects } = getState();
if (sgfProjects && !sgfProjects.loading) {
dispatch(setProjectsLoading({ loading: true, error: false }));
axios
.all([fetchSGFProjects(), getCountriesProvider(), getCountriesLatLng()])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const setLatestDates = createAction('setLatestDates');
export const getLatest = createThunkAction(
'getLatest',
latestEndpoints => (dispatch, getState) => {
const currentLatestDates = Object.keys(getState().latest.data);
const { latest } = getState();
const currentLatestDates = latest && Object.keys(latest.data);
const newEndpoints = latestEndpoints.filter(
l => !currentLatestDates.includes(l.id)
);
Expand All @@ -23,13 +24,13 @@ export const getLatest = createThunkAction(
const latestDates =
responses &&
responses.reduce((obj, response, index) => {
const latest = response.data.data || response.data;
let date = latest.date;
const latestResponse = response.data.data || response.data;
let date = latestResponse.date;
if (!date) {
const data = Array.isArray(latest)
? latest[0].attributes
: latest.attributes;
date = data.date || data.latest;
const data = Array.isArray(latestResponse)
? latestResponse[0].attributes
: latestResponse.attributes;
date = data.date || data.latestResponse;
}
return {
...obj,
Expand Down
23 changes: 0 additions & 23 deletions app/javascript/utils/stateToUrl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// import isEqual from 'lodash/isEqual';
// import pick from 'lodash/pick';
import queryString from 'query-string';

export const decodeUrlForState = url => {
Expand Down Expand Up @@ -47,24 +45,3 @@ export const setComponentStateToUrl = ({ key, subKey, change, state }) => {
}
};
};

// export const isObjectContained = (contained, container) =>
// isEqual(pick(container, Object.keys(contained)), contained);

// export const setUrlStateToStore = ({
// key,
// query,
// setState,
// dispatch,
// getState
// }) => {
// if (query && query[key]) {
// const state = decodeUrlForState(query[key]);
// const { settings } = getState()[key];
// // Check if the state needs and update checking the values of the new config
// // with the existing in the url to avoid dispatch actions without changes
// if (!isObjectContained(state, settings)) {
// dispatch(setState(state));
// }
// }
// };