Skip to content

Commit

Permalink
fix: move props out of redux selected that dont belong there
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferarnesen committed Mar 3, 2021
1 parent f1c3b9b commit 8d9c6ec
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 92 deletions.
6 changes: 6 additions & 0 deletions src/actions/isLoading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SET_ISLOADING } from '../reducers/isLoading'

export const acSetIsLoading = value => ({
type: SET_ISLOADING,
value,
})
36 changes: 5 additions & 31 deletions src/actions/selected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import i18n from '@dhis2/d2-i18n'
import { sGetDashboardById } from '../reducers/dashboards'
import {
SET_SELECTED_ID,
SET_SELECTED_ISLOADING,
SET_SELECTED_SHOWDESCRIPTION,
SET_SELECTED_ITEM_ACTIVE_TYPE,
CLEAR_SELECTED_ITEM_ACTIVE_TYPES,
sGetSelectedIsLoading,
sGetSelectedId,
} from '../reducers/selected'
import { sGetIsLoading } from '../reducers/isLoading'
import { sGetUserUsername } from '../reducers/user'
import { acSetIsLoading } from './isLoading'

import { acSetDashboardItems, acAppendDashboards } from './dashboards'
import { acClearItemFilters } from './itemFilters'
Expand All @@ -18,7 +17,6 @@ import { acSetAlertMessage, acClearAlertMessage } from './alert'
import { acAddVisualization, acClearVisualizations } from './visualizations'
import { apiFetchDashboard } from '../api/fetchDashboard'
import { storePreferredDashboardId } from '../api/localStorage'
import { apiGetShowDescription } from '../api/description'

import { withShape } from '../modules/gridUtil'
import { getVisualizationFromItem } from '../modules/item'
Expand All @@ -40,16 +38,6 @@ export const acSetSelectedId = value => ({
value,
})

export const acSetSelectedIsLoading = value => ({
type: SET_SELECTED_ISLOADING,
value,
})

export const acSetSelectedShowDescription = value => ({
type: SET_SELECTED_SHOWDESCRIPTION,
value,
})

export const acSetSelectedItemActiveType = (id, activeType) => {
const action = {
type: SET_SELECTED_ITEM_ACTIVE_TYPE,
Expand All @@ -70,12 +58,12 @@ export const tSetSelectedDashboardById = (id, mode) => async (
getState,
dataEngine
) => {
dispatch(acSetSelectedIsLoading(true))
dispatch(acSetIsLoading(true))

const alertTimeout = setTimeout(() => {
const name = sGetDashboardById(getState(), id)?.displayName

if (sGetSelectedIsLoading(getState()) && name) {
if (sGetIsLoading(getState()) && name) {
dispatch(
acSetAlertMessage(
i18n.t('Loading dashboard – {{name}}', { name })
Expand Down Expand Up @@ -118,7 +106,7 @@ export const tSetSelectedDashboardById = (id, mode) => async (

dispatch(acSetSelectedId(id))

dispatch(acSetSelectedIsLoading(false))
dispatch(acSetIsLoading(false))

clearTimeout(alertTimeout)

Expand All @@ -136,17 +124,3 @@ export const tSetSelectedDashboardById = (id, mode) => async (
return err
}
}

export const tSetShowDescription = () => async dispatch => {
const onSuccess = value => {
dispatch(acSetSelectedShowDescription(value))
}

try {
const showDescription = await apiGetShowDescription()
return onSuccess(showDescription)
} catch (err) {
console.error('Error (apiGetShowDescription): ', err)
return err
}
}
22 changes: 22 additions & 0 deletions src/actions/showDescription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { apiGetShowDescription } from '../api/description'

import { SET_SHOWDESCRIPTION } from '../reducers/showDescription'

export const acSetShowDescription = value => ({
type: SET_SHOWDESCRIPTION,
value,
})

export const tSetShowDescription = () => async dispatch => {
const onSuccess = value => {
dispatch(acSetShowDescription(value))
}

try {
const showDescription = await apiGetShowDescription()
return onSuccess(showDescription)
} catch (err) {
console.error('Error (apiGetShowDescription): ', err)
return err
}
}
4 changes: 2 additions & 2 deletions src/api/description.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
apiGetUserDataStoreValue,
apiPostUserDataStoreValue,
} from './userDataStore'
import { DEFAULT_STATE_SELECTED_SHOWDESCRIPTION } from '../reducers/selected'
import { DEFAULT_STATE_SHOWDESCRIPTION } from '../reducers/showDescription'

const KEY_SHOW_DESCRIPTION = 'showDescription'

export const apiGetShowDescription = async () =>
await apiGetUserDataStoreValue(
KEY_SHOW_DESCRIPTION,
DEFAULT_STATE_SELECTED_SHOWDESCRIPTION
DEFAULT_STATE_SHOWDESCRIPTION
)

export const apiPostShowDescription = value =>
Expand Down
2 changes: 1 addition & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useSystemSettings } from './SystemSettingsProvider'
import { acReceivedUser } from '../actions/user'
import { tFetchDashboards } from '../actions/dashboards'
import { tSetControlBarRows } from '../actions/controlBar'
import { tSetShowDescription } from '../actions/selected'
import { tSetShowDescription } from '../actions/showDescription'
import { acSetDimensions } from '../actions/dimensions'
import getFilteredDimensions from '../modules/getFilteredDimensions'

Expand Down
8 changes: 3 additions & 5 deletions src/components/Item/PrintTitlePageItem/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import i18n from '@dhis2/d2-i18n'

import {
sGetSelectedId,
sGetSelectedShowDescription,
} from '../../../reducers/selected'
import { sGetSelectedId } from '../../../reducers/selected'
import { sGetShowDescription } from '../../../reducers/showDescription'
import {
sGetDashboardById,
EMPTY_DASHBOARD,
Expand Down Expand Up @@ -84,7 +82,7 @@ const mapStateToProps = state => {
name,
description,
itemFilters: sGetNamedItemFilters(state),
showDescription: sGetSelectedShowDescription(state),
showDescription: sGetShowDescription(state),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/ItemGrid/EditItemGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getBreakpoint } from '../../modules/smallScreen'
import getGridItemDomId from '../../modules/getGridItemDomId'
import { orArray } from '../../modules/util'
import NoContentMessage from '../../widgets/NoContentMessage'
import { sGetSelectedIsLoading } from '../../reducers/selected'
import { sGetIsLoading } from '../../reducers/isLoading'
import {
sGetEditDashboardRoot,
sGetEditDashboardItems,
Expand Down Expand Up @@ -143,7 +143,7 @@ const mapStateToProps = state => {
)

return {
isLoading: sGetSelectedIsLoading(state) || !selectedDashboard,
isLoading: sGetIsLoading(state) || !selectedDashboard,
dashboardItems,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/ItemGrid/PrintItemGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Item } from '../Item/Item'

import { hasShape } from '../../modules/gridUtil'
import { PRINT } from '../Dashboard/dashboardModes'
import { sGetSelectedIsLoading } from '../../reducers/selected'
import { sGetIsLoading } from '../../reducers/isLoading'
import {
sGetPrintDashboardRoot,
sGetPrintDashboardItems,
Expand Down Expand Up @@ -45,7 +45,7 @@ const mapStateToProps = state => {
const selectedDashboard = sGetPrintDashboardRoot(state)

return {
isLoading: sGetSelectedIsLoading(state) || !selectedDashboard,
isLoading: sGetIsLoading(state) || !selectedDashboard,
dashboardItems: orArray(sGetPrintDashboardItems(state)).filter(
hasShape
),
Expand Down
4 changes: 2 additions & 2 deletions src/components/ItemGrid/PrintLayoutItemGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import StaticGrid from './StaticGrid'
import { Item } from '../Item/Item'

import { acUpdatePrintDashboardLayout } from '../../actions/printDashboard'
import { sGetSelectedIsLoading } from '../../reducers/selected'
import { sGetIsLoading } from '../../reducers/isLoading'
import {
sGetPrintDashboardRoot,
sGetPrintDashboardItems,
Expand Down Expand Up @@ -148,7 +148,7 @@ const mapStateToProps = state => {
const selectedDashboard = sGetPrintDashboardRoot(state)

return {
isLoading: sGetSelectedIsLoading(state) || !selectedDashboard,
isLoading: sGetIsLoading(state) || !selectedDashboard,
dashboardItems: sGetPrintDashboardItems(state).filter(hasShape),
isEditing: sGetIsEditing(state),
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/ItemGrid/ViewItemGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
} from '../../modules/gridUtil'
import { orArray } from '../../modules/util'
import NoContentMessage from '../../widgets/NoContentMessage'
import { sGetSelectedId, sGetSelectedIsLoading } from '../../reducers/selected'
import { sGetSelectedId } from '../../reducers/selected'
import { sGetIsLoading } from '../../reducers/isLoading'
import {
sGetDashboardById,
sGetDashboardItems,
Expand Down Expand Up @@ -110,6 +111,7 @@ const ResponsiveItemGrid = ({ isLoading, dashboardItems }) => {
}

if (!isLoading && !dashboardItems.length) {
console.log('ViewItemGrid no items', dashboardItems)
return (
<NoContentMessage
text={i18n.t('There are no items on this dashboard')}
Expand Down Expand Up @@ -163,7 +165,7 @@ const mapStateToProps = state => {
const dashboardItems = orArray(sGetDashboardItems(state)).filter(hasShape)

return {
isLoading: sGetSelectedIsLoading(state) || !selectedDashboard,
isLoading: sGetIsLoading(state) || !selectedDashboard,
dashboardItems,
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/components/TitleBar/ViewTitleBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import { apiStarDashboard } from '../../api/starDashboard'
import { apiPostShowDescription } from '../../api/description'

import { acSetDashboardStarred } from '../../actions/dashboards'
import { acSetSelectedShowDescription } from '../../actions/selected'
import { acSetShowDescription } from '../../actions/showDescription'
import FilterSelector from '../ItemFilter/FilterSelector'
import {
sGetSelectedId,
sGetSelectedShowDescription,
} from '../../reducers/selected'
import { sGetSelectedId } from '../../reducers/selected'
import { sGetShowDescription } from '../../reducers/showDescription'
import {
sGetDashboardById,
sGetDashboardItems,
Expand Down Expand Up @@ -266,7 +264,7 @@ const mapStateToProps = state => {
name: dashboard.displayName,
description: dashboard.displayDescription,
dashboardItems: sGetDashboardItems(state),
showDescription: sGetSelectedShowDescription(state),
showDescription: sGetShowDescription(state),
starred: dashboard.starred,
access: dashboard.access,
restrictFilters: dashboard.restrictFilters,
Expand All @@ -276,5 +274,5 @@ const mapStateToProps = state => {

export default connect(mapStateToProps, {
setDashboardStarred: acSetDashboardStarred,
updateShowDescription: acSetSelectedShowDescription,
updateShowDescription: acSetShowDescription,
})(ViewTitleBar)
4 changes: 4 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dashboards from './dashboards'
import selected from './selected'
import dashboardsFilter from './dashboardsFilter'
import controlBar from './controlBar'
import isLoading from './isLoading'
import showDescription from './showDescription'
import visualizations from './visualizations'
import editDashboard from './editDashboard'
import printDashboard from './printDashboard'
Expand All @@ -21,6 +23,8 @@ export default combineReducers({
selected,
dashboardsFilter,
controlBar,
isLoading,
showDescription,
visualizations,
messages,
user,
Expand Down
21 changes: 21 additions & 0 deletions src/reducers/isLoading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { validateReducer } from '../modules/util'

export const SET_ISLOADING = 'SET_ISLOADING'

export const DEFAULT_STATE_SELECTED_ISLOADING = false

const isLoading = (state = DEFAULT_STATE_SELECTED_ISLOADING, action) => {
switch (action.type) {
case SET_ISLOADING:
return validateReducer(
action.value,
DEFAULT_STATE_SELECTED_ISLOADING
)
default:
return state
}
}

export default isLoading

export const sGetIsLoading = state => state.isLoading
38 changes: 0 additions & 38 deletions src/reducers/selected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { combineReducers } from 'redux'
import { validateReducer } from '../modules/util'

export const SET_SELECTED_ID = 'SET_SELECTED_ID'
export const SET_SELECTED_ISLOADING = 'SET_SELECTED_ISLOADING'
export const SET_SELECTED_SHOWDESCRIPTION = 'SET_SELECTED_SHOWDESCRIPTION'
export const SET_SELECTED_ITEM_ACTIVE_TYPE = 'SET_SELECTED_ITEM_ACTIVE_TYPE'
export const CLEAR_SELECTED_ITEM_ACTIVE_TYPES =
'CLEAR_SELECTED_ITEM_ACTIVE_TYPES'

export const DEFAULT_STATE_SELECTED_ID = null
export const DEFAULT_STATE_SELECTED_ISLOADING = false
export const DEFAULT_STATE_SELECTED_SHOWDESCRIPTION = false
export const DEFAULT_STATE_SELECTED_ITEM_ACTIVE_TYPES = {}

export const NON_EXISTING_DASHBOARD_ID = '0'
Expand All @@ -26,33 +22,6 @@ const id = (state = DEFAULT_STATE_SELECTED_ID, action) => {
}
}

const isLoading = (state = DEFAULT_STATE_SELECTED_ISLOADING, action) => {
switch (action.type) {
case SET_SELECTED_ISLOADING:
return validateReducer(
action.value,
DEFAULT_STATE_SELECTED_ISLOADING
)
default:
return state
}
}

const showDescription = (
state = DEFAULT_STATE_SELECTED_SHOWDESCRIPTION,
action
) => {
switch (action.type) {
case SET_SELECTED_SHOWDESCRIPTION:
return validateReducer(
action.value,
DEFAULT_STATE_SELECTED_SHOWDESCRIPTION
)
default:
return state
}
}

const itemActiveTypes = (
state = DEFAULT_STATE_SELECTED_ITEM_ACTIVE_TYPES,
action
Expand All @@ -74,8 +43,6 @@ const itemActiveTypes = (

export default combineReducers({
id,
isLoading,
showDescription,
itemActiveTypes,
})

Expand All @@ -85,10 +52,5 @@ export const sGetSelectedRoot = state => state.selected

export const sGetSelectedId = state => sGetSelectedRoot(state).id

export const sGetSelectedIsLoading = state => sGetSelectedRoot(state).isLoading

export const sGetSelectedShowDescription = state =>
sGetSelectedRoot(state).showDescription

export const sGetSelectedItemActiveType = (state, id) =>
sGetSelectedRoot(state).itemActiveTypes[id]
Loading

0 comments on commit 8d9c6ec

Please sign in to comment.