-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Refactor loading actions #2996
Refactor loading actions #2996
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, { useEffect, useState } from 'react'; | ||
import React, { useEffect } from 'react'; | ||
import { Helmet } from 'react-helmet'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
@@ -13,6 +13,7 @@ import { | |
import getSortedCollections from '../selectors/collections'; | ||
import QuickAddList from './QuickAddList'; | ||
import { remSize } from '../../../theme'; | ||
import { startLoader, stopLoader } from '../reducers/loading'; | ||
|
||
export const CollectionAddSketchWrapper = styled.div` | ||
width: ${remSize(600)}; | ||
|
@@ -29,20 +30,14 @@ export const QuickAddWrapper = styled.div` | |
|
||
const AddToCollectionList = ({ projectId }) => { | ||
const { t } = useTranslation(); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
const username = useSelector((state) => state.user.username); | ||
|
||
const collections = useSelector(getSortedCollections); | ||
|
||
// TODO: improve loading state | ||
const loading = useSelector((state) => state.loading); | ||
const [hasLoadedData, setHasLoadedData] = useState(false); | ||
const showLoader = loading && !hasLoadedData; | ||
|
||
useEffect(() => { | ||
dispatch(getCollections(username)).then(() => setHasLoadedData(true)); | ||
dispatch(startLoader()); | ||
dispatch(getCollections(username)).then(() => dispatch(stopLoader())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out the source of the |
||
}, [dispatch, username]); | ||
|
||
const handleCollectionAdd = (collection) => { | ||
|
@@ -60,7 +55,7 @@ const AddToCollectionList = ({ projectId }) => { | |
})); | ||
|
||
const getContent = () => { | ||
if (showLoader) { | ||
if (loading) { | ||
return <Loader />; | ||
} else if (collections.length === 0) { | ||
return t('AddToCollectionList.Empty'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import * as ActionTypes from '../../../constants'; | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
const loading = (state = false, action) => { | ||
switch (action.type) { | ||
case ActionTypes.START_LOADING: | ||
return true; | ||
case ActionTypes.STOP_LOADING: | ||
return false; | ||
default: | ||
return state; | ||
const loadingSlice = createSlice({ | ||
name: 'loading', | ||
initialState: false, | ||
reducers: { | ||
startLoader: () => true, | ||
stopLoader: () => false | ||
} | ||
}; | ||
}); | ||
|
||
export default loading; | ||
export const { startLoader, stopLoader } = loadingSlice.actions; | ||
export default loadingSlice.reducer; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is subjective. Personally I would use the local component state. The policy loading state is not used in any other component so I don't think that it needs to be in a global Redux state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either of these should work:
startLoader()
is an action creator, so it's a function that returns the action. If you just call that function without dispatching you will get the value{ type: 'loading/startLoader' }
.