Skip to content

Commit 48c4d97

Browse files
committed
Refactor loading actions
1 parent 73ce9f7 commit 48c4d97

File tree

10 files changed

+33
-52
lines changed

10 files changed

+33
-52
lines changed

client/constants.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ export const SET_SORT_PARAMS = 'SET_SORT_PARAMS';
137137
export const SET_SEARCH_TERM = 'SET_SEARCH_TERM';
138138
export const CLOSE_SKETCHLIST_MODAL = 'CLOSE_SKETCHLIST_MODAL';
139139

140-
export const START_LOADING = 'START_LOADING';
141-
export const STOP_LOADING = 'STOP_LOADING';
142-
143140
export const START_SAVING_PROJECT = 'START_SAVING_PROJECT';
144141
export const END_SAVING_PROJECT = 'END_SAVING_PROJECT';
145142

client/modules/IDE/actions/assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
3-
import { startLoader, stopLoader } from './loader';
3+
import { startLoader, stopLoader } from '../reducers/loading';
44
import { assetsActions } from '../reducers/assets';
55

66
const { setAssets, deleteAsset } = assetsActions;

client/modules/IDE/actions/collections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import browserHistory from '../../../browserHistory';
22
import apiClient from '../../../utils/apiClient';
33
import * as ActionTypes from '../../../constants';
4-
import { startLoader, stopLoader } from './loader';
4+
import { startLoader, stopLoader } from '../reducers/loading';
55
import { setToastText, showToast } from './toast';
66

77
const TOAST_DISPLAY_TIME_MS = 1500;

client/modules/IDE/actions/loader.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

client/modules/IDE/actions/projects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
3-
import { startLoader, stopLoader } from './loader';
3+
import { startLoader, stopLoader } from '../reducers/loading';
44

55
// eslint-disable-next-line
66
export function getProjects(username) {

client/modules/IDE/actions/projects.unit.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ describe('projects action creator tests', () => {
3333
store = mockStore(initialTestState);
3434

3535
const expectedActions = [
36-
{ type: ActionTypes.START_LOADING },
36+
{ type: 'loading/startLoader' },
3737
{ type: ActionTypes.SET_PROJECTS, projects: mockProjects },
38-
{ type: ActionTypes.STOP_LOADING }
38+
{ type: 'loading/stopLoader' }
3939
];
4040

4141
return store

client/modules/IDE/components/AddToCollectionList.jsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import React, { useEffect, useState } from 'react';
2+
import React, { useEffect } from 'react';
33
import { Helmet } from 'react-helmet';
44
import { useTranslation } from 'react-i18next';
55
import { useDispatch, useSelector } from 'react-redux';
@@ -13,6 +13,7 @@ import {
1313
import getSortedCollections from '../selectors/collections';
1414
import QuickAddList from './QuickAddList';
1515
import { remSize } from '../../../theme';
16+
import { startLoader, stopLoader } from '../reducers/loading';
1617

1718
export const CollectionAddSketchWrapper = styled.div`
1819
width: ${remSize(600)};
@@ -29,20 +30,14 @@ export const QuickAddWrapper = styled.div`
2930

3031
const AddToCollectionList = ({ projectId }) => {
3132
const { t } = useTranslation();
32-
3333
const dispatch = useDispatch();
34-
3534
const username = useSelector((state) => state.user.username);
36-
3735
const collections = useSelector(getSortedCollections);
38-
39-
// TODO: improve loading state
4036
const loading = useSelector((state) => state.loading);
41-
const [hasLoadedData, setHasLoadedData] = useState(false);
42-
const showLoader = loading && !hasLoadedData;
4337

4438
useEffect(() => {
45-
dispatch(getCollections(username)).then(() => setHasLoadedData(true));
39+
dispatch(startLoader());
40+
dispatch(getCollections(username)).then(() => dispatch(stopLoader()));
4641
}, [dispatch, username]);
4742

4843
const handleCollectionAdd = (collection) => {
@@ -60,7 +55,7 @@ const AddToCollectionList = ({ projectId }) => {
6055
}));
6156

6257
const getContent = () => {
63-
if (showLoader) {
58+
if (loading) {
6459
return <Loader />;
6560
} else if (collections.length === 0) {
6661
return t('AddToCollectionList.Empty');

client/modules/IDE/components/AddToCollectionSketchList.jsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import React, { useEffect, useState } from 'react';
2+
import React, { useEffect } from 'react';
33
import { Helmet } from 'react-helmet';
44
import { useDispatch, useSelector } from 'react-redux';
55
import { useTranslation } from 'react-i18next';
@@ -12,23 +12,18 @@ import {
1212
CollectionAddSketchWrapper,
1313
QuickAddWrapper
1414
} from './AddToCollectionList';
15+
import { startLoader, stopLoader } from '../reducers/loading';
1516

1617
const AddToCollectionSketchList = ({ collection }) => {
1718
const { t } = useTranslation();
18-
1919
const dispatch = useDispatch();
20-
2120
const username = useSelector((state) => state.user.username);
22-
2321
const sketches = useSelector(getSortedSketches);
24-
25-
// TODO: improve loading state
2622
const loading = useSelector((state) => state.loading);
27-
const [hasLoadedData, setHasLoadedData] = useState(false);
28-
const showLoader = loading && !hasLoadedData;
2923

3024
useEffect(() => {
31-
dispatch(getProjects(username)).then(() => setHasLoadedData(true));
25+
dispatch(startLoader());
26+
dispatch(getProjects(username)).then(() => dispatch(stopLoader()));
3227
}, [dispatch, username]);
3328

3429
const handleCollectionAdd = (sketch) => {
@@ -48,7 +43,7 @@ const AddToCollectionSketchList = ({ collection }) => {
4843
}));
4944

5045
const getContent = () => {
51-
if (showLoader) {
46+
if (loading) {
5247
return <Loader />;
5348
} else if (sketches.length === 0) {
5449
// TODO: shouldn't it be NoSketches? -Linda
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import * as ActionTypes from '../../../constants';
1+
import { createSlice } from '@reduxjs/toolkit';
22

3-
const loading = (state = false, action) => {
4-
switch (action.type) {
5-
case ActionTypes.START_LOADING:
6-
return true;
7-
case ActionTypes.STOP_LOADING:
8-
return false;
9-
default:
10-
return state;
3+
const loadingSlice = createSlice({
4+
name: 'loading',
5+
initialState: false,
6+
reducers: {
7+
startLoader: () => true,
8+
stopLoader: () => false
119
}
12-
};
10+
});
1311

14-
export default loading;
12+
export const { startLoader, stopLoader } = loadingSlice.actions;
13+
export default loadingSlice.reducer;

client/modules/Legal/pages/Legal.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import React, { useEffect, useState } from 'react';
44
import Helmet from 'react-helmet';
55
import { useTranslation } from 'react-i18next';
66
import styled from 'styled-components';
7+
import { useDispatch, useSelector } from 'react-redux';
78
import RouterTab from '../../../common/RouterTab';
89
import RootPage from '../../../components/RootPage';
910
import { remSize } from '../../../theme';
1011
import Loader from '../../App/components/loader';
1112
import Nav from '../../IDE/components/Header/Nav';
1213
import PolicyContainer from '../components/PolicyContainer';
14+
import { startLoader, stopLoader } from '../../IDE/reducers/loading';
1315

1416
const StyledTabList = styled.nav`
1517
display: flex;
@@ -24,15 +26,17 @@ const StyledTabList = styled.nav`
2426

2527
function Legal({ policyFile, title }) {
2628
const { t } = useTranslation();
27-
const [isLoading, setIsLoading] = useState(true);
29+
const loading = useSelector((state) => state.loading);
30+
const dispatch = useDispatch();
2831
const [policy, setPolicy] = useState('');
2932

3033
useEffect(() => {
34+
dispatch(startLoader());
3135
axios.get(policyFile).then((response) => {
36+
dispatch(stopLoader());
3237
setPolicy(response.data);
33-
setIsLoading(false);
3438
});
35-
}, [policyFile]);
39+
}, [dispatch, policyFile]);
3640

3741
return (
3842
<RootPage>
@@ -51,7 +55,7 @@ function Legal({ policyFile, title }) {
5155
</ul>
5256
</StyledTabList>
5357
<PolicyContainer policy={policy} />
54-
{isLoading && <Loader />}
58+
{loading && <Loader />}
5559
</RootPage>
5660
);
5761
}

0 commit comments

Comments
 (0)