Skip to content

Commit

Permalink
Use one action
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Mar 18, 2021
1 parent 9144154 commit 7b54575
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 68 deletions.
16 changes: 3 additions & 13 deletions packages/ra-core/src/actions/uiActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,16 @@ export const setSidebarVisibility = (
payload: isOpen,
});

// refresh increases version (i.e. forces refetch) and empties the cache
export const REFRESH_VIEW = 'RA/REFRESH_VIEW';

export interface RefreshViewAction {
readonly type: typeof REFRESH_VIEW;
readonly payload: { hard: boolean };
}

export const refreshView = (): RefreshViewAction => ({
export const refreshView = (hard?: boolean): RefreshViewAction => ({
type: REFRESH_VIEW,
});

// soft refresh only increases version (i.e. forces refetch)
export const SOFT_REFRESH_VIEW = 'RA/SOFT_REFRESH_VIEW';

export interface SoftRefreshViewAction {
readonly type: typeof SOFT_REFRESH_VIEW;
}

export const softRefreshView = (): SoftRefreshViewAction => ({
type: SOFT_REFRESH_VIEW,
payload: { hard },
});

export const SET_AUTOMATIC_REFRESH = 'RA/SET_AUTOMATIC_REFRESH';
Expand Down
10 changes: 3 additions & 7 deletions packages/ra-core/src/dataProvider/Mutation.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { fireEvent, waitFor, act, render } from '@testing-library/react';
import expect from 'expect';

import Mutation from './Mutation';
import {
showNotification,
softRefreshView,
setListSelectedIds,
} from '../actions';
import { showNotification, refreshView, setListSelectedIds } from '../actions';
import DataProviderContext from './DataProviderContext';
import { renderWithRedux, TestContext } from 'ra-test';
import { useNotify } from '../sideEffect';
Expand Down Expand Up @@ -104,7 +100,7 @@ describe('Mutation', () => {
})
);
expect(historyForAssertions.location.pathname).toEqual('/a_path');
expect(dispatchSpy).toHaveBeenCalledWith(softRefreshView());
expect(dispatchSpy).toHaveBeenCalledWith(refreshView());
expect(dispatchSpy).toHaveBeenCalledWith(
setListSelectedIds('foo', [])
);
Expand Down Expand Up @@ -226,7 +222,7 @@ describe('Mutation', () => {
})
);
expect(historyForAssertions.location.pathname).toEqual('/a_path');
expect(dispatchSpy).toHaveBeenCalledWith(softRefreshView());
expect(dispatchSpy).toHaveBeenCalledWith(refreshView());
expect(dispatchSpy).toHaveBeenCalledWith(
setListSelectedIds('foo', [])
);
Expand Down
10 changes: 3 additions & 7 deletions packages/ra-core/src/dataProvider/Query.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import Query from './Query';
import { CoreAdmin, Resource } from '../core';
import { renderWithRedux, TestContext } from 'ra-test';
import DataProviderContext from './DataProviderContext';
import {
showNotification,
softRefreshView,
setListSelectedIds,
} from '../actions';
import { showNotification, refreshView, setListSelectedIds } from '../actions';
import { useNotify, useRefresh } from '../sideEffect';
import { History } from 'history';

Expand Down Expand Up @@ -315,7 +311,7 @@ describe('Query', () => {
})
);
expect(historyForAssertions.location.pathname).toEqual('/a_path');
expect(dispatchSpy).toHaveBeenCalledWith(softRefreshView());
expect(dispatchSpy).toHaveBeenCalledWith(refreshView());
expect(dispatchSpy).toHaveBeenCalledWith(
setListSelectedIds('foo', [])
);
Expand Down Expand Up @@ -436,7 +432,7 @@ describe('Query', () => {
})
);
expect(historyForAssertions.location.pathname).toEqual('/a_path');
expect(dispatchSpy).toHaveBeenCalledWith(softRefreshView());
expect(dispatchSpy).toHaveBeenCalledWith(refreshView());
expect(dispatchSpy).toHaveBeenCalledWith(
setListSelectedIds('foo', [])
);
Expand Down
5 changes: 0 additions & 5 deletions packages/ra-core/src/reducer/admin/resource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
UnregisterResourceAction,
REFRESH_VIEW,
RefreshViewAction,
SOFT_REFRESH_VIEW,
SoftRefreshViewAction,
} from '../../../actions';

import data from './data';
Expand All @@ -19,7 +17,6 @@ type ActionTypes =
| RegisterResourceAction
| UnregisterResourceAction
| RefreshViewAction
| SoftRefreshViewAction
| { type: 'OTHER_ACTION'; payload?: any; meta?: { resource?: string } };

export default (previousState = initialState, action: ActionTypes) => {
Expand Down Expand Up @@ -48,7 +45,6 @@ export default (previousState = initialState, action: ActionTypes) => {

if (
action.type !== REFRESH_VIEW &&
action.type !== SOFT_REFRESH_VIEW &&
(!action.meta || !action.meta.resource)
) {
return previousState;
Expand All @@ -60,7 +56,6 @@ export default (previousState = initialState, action: ActionTypes) => {
...acc,
[resource]:
action.type === REFRESH_VIEW ||
action.type === SOFT_REFRESH_VIEW ||
action.meta.resource === resource
? {
props: previousState[resource].props,
Expand Down
33 changes: 15 additions & 18 deletions packages/ra-core/src/reducer/admin/resource/list/cachedRequests.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Reducer } from 'redux';

import { Identifier } from '../../../../types';
import {
FETCH_END,
REFRESH_VIEW,
SOFT_REFRESH_VIEW,
} from '../../../../actions';
import { FETCH_END, REFRESH_VIEW } from '../../../../actions';
import {
GET_LIST,
CREATE,
Expand Down Expand Up @@ -36,19 +32,20 @@ const cachedRequestsReducer: Reducer<State> = (
action
) => {
if (action.type === REFRESH_VIEW) {
// force refresh
return initialState;
}
if (action.type === SOFT_REFRESH_VIEW) {
// remove validity only
const newState = {};
Object.keys(previousState).forEach(key => {
newState[key] = {
...previousState[key],
validity: undefined,
};
});
return newState;
if (action.payload?.hard) {
// force refresh
return initialState;
} else {
// remove validity only
const newState = {};
Object.keys(previousState).forEach(key => {
newState[key] = {
...previousState[key],
validity: undefined,
};
});
return newState;
}
}
if (action.meta && action.meta.optimistic) {
if (
Expand Down
8 changes: 2 additions & 6 deletions packages/ra-core/src/reducer/admin/resource/list/validity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Reducer } from 'redux';
import {
FETCH_END,
REFRESH_VIEW,
SOFT_REFRESH_VIEW,
} from '../../../../actions';
import { FETCH_END, REFRESH_VIEW } from '../../../../actions';
import { GET_LIST, CREATE } from '../../../../core';

interface ValidityRegistry {
Expand All @@ -16,7 +12,7 @@ const validityReducer: Reducer<ValidityRegistry> = (
previousState = initialState,
{ type, payload, requestPayload, meta }
) => {
if (type === REFRESH_VIEW || type === SOFT_REFRESH_VIEW) {
if (type === REFRESH_VIEW) {
return initialState;
}
if (
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/reducer/admin/resource/validity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Reducer } from 'redux';
import { FETCH_END, REFRESH_VIEW, SOFT_REFRESH_VIEW } from '../../../actions';
import { FETCH_END, REFRESH_VIEW } from '../../../actions';
import {
CREATE,
DELETE,
Expand All @@ -25,7 +25,7 @@ const validityReducer: Reducer<ValidityRegistry> = (
previousState = initialState,
{ type, payload, requestPayload, meta }
) => {
if (type === REFRESH_VIEW || type === SOFT_REFRESH_VIEW) {
if (type === REFRESH_VIEW) {
return initialState;
}
if (
Expand Down
4 changes: 0 additions & 4 deletions packages/ra-core/src/reducer/admin/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
SetSidebarVisibilityAction,
REFRESH_VIEW,
RefreshViewAction,
SOFT_REFRESH_VIEW,
SoftRefreshViewAction,
START_OPTIMISTIC_MODE,
StartOptimisticModeAction,
STOP_OPTIMISTIC_MODE,
Expand All @@ -22,7 +20,6 @@ type ActionTypes =
| ToggleSidebarAction
| SetSidebarVisibilityAction
| RefreshViewAction
| SoftRefreshViewAction
| StartOptimisticModeAction
| StopOptimisticModeAction
| SetAutomaticRefreshAction
Expand Down Expand Up @@ -70,7 +67,6 @@ const uiReducer: Reducer<UIState> = (
automaticRefreshEnabled: action.payload,
};
case REFRESH_VIEW:
case SOFT_REFRESH_VIEW:
return {
...previousState,
viewVersion: previousState.viewVersion + 1,
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/sideEffect/useRefresh.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { refreshView, softRefreshView } from '../actions/uiActions';
import { refreshView } from '../actions/uiActions';

/**
* Hook for Refresh Side Effect
Expand All @@ -24,7 +24,7 @@ const useRefresh = () => {
const dispatch = useDispatch();
return useCallback(
(hard?: boolean) => {
dispatch(hard ? refreshView() : softRefreshView());
dispatch(refreshView(hard));
},
[dispatch]
);
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/button/RefreshButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FC, ReactElement, MouseEvent, useCallback } from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import NavigationRefresh from '@material-ui/icons/Refresh';
import { softRefreshView } from 'ra-core';
import { refreshView } from 'ra-core';

import Button, { ButtonProps } from './Button';

Expand All @@ -17,7 +17,7 @@ const RefreshButton: FC<RefreshButtonProps> = ({
const handleClick = useCallback(
event => {
event.preventDefault();
dispatch(softRefreshView());
dispatch(refreshView());
if (typeof onClick === 'function') {
onClick(event);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-ui-materialui/src/button/RefreshIconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useDispatch } from 'react-redux';
import Tooltip from '@material-ui/core/Tooltip';
import IconButton, { IconButtonProps } from '@material-ui/core/IconButton';
import NavigationRefresh from '@material-ui/icons/Refresh';
import { softRefreshView, useTranslate } from 'ra-core';
import { refreshView, useTranslate } from 'ra-core';

const RefreshIconButton: FC<RefreshIconProps> = ({
label = 'ra.action.refresh',
Expand All @@ -19,7 +19,7 @@ const RefreshIconButton: FC<RefreshIconProps> = ({
const handleClick = useCallback(
event => {
event.preventDefault();
dispatch(softRefreshView());
dispatch(refreshView());
if (typeof onClick === 'function') {
onClick(event);
}
Expand Down

0 comments on commit 7b54575

Please sign in to comment.