Skip to content
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

Fix "Please login to continue" notification remains after login #5789

Merged
merged 1 commit into from
Jan 17, 2021
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
10 changes: 10 additions & 0 deletions packages/ra-core/src/actions/notificationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ export interface HideNotificationAction {
export const hideNotification = (): HideNotificationAction => ({
type: HIDE_NOTIFICATION,
});

export const RESET_NOTIFICATION = 'RA/RESET_NOTIFICATION';

export interface ResetNotificationAction {
readonly type: typeof RESET_NOTIFICATION;
}

export const resetNotification = (): ResetNotificationAction => ({
type: RESET_NOTIFICATION,
});
11 changes: 8 additions & 3 deletions packages/ra-core/src/auth/useLogin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useCallback } from 'react';
import { useLocation, useHistory } from 'react-router-dom';
import { useDispatch } from 'react-redux';

import useAuthProvider, { defaultAuthParams } from './useAuthProvider';
import { useLocation, useHistory } from 'react-router-dom';
import { resetNotification } from '../actions/notificationActions';

/**
* Get a callback for calling the authProvider.login() method
Expand Down Expand Up @@ -31,26 +33,29 @@ const useLogin = (): Login => {
const location = useLocation();
const locationState = location.state as any;
const history = useHistory();
const dispatch = useDispatch();
const nextPathName = locationState && locationState.nextPathname;

const login = useCallback(
(params: any = {}, pathName) =>
authProvider.login(params).then(ret => {
dispatch(resetNotification());
const redirectUrl = pathName
? pathName
: nextPathName || defaultAuthParams.afterLoginUrl;
history.push(redirectUrl);
return ret;
}),
[authProvider, history, nextPathName]
[authProvider, history, nextPathName, dispatch]
);

const loginWithoutProvider = useCallback(
(_, __) => {
dispatch(resetNotification());
history.push(defaultAuthParams.afterLoginUrl);
return Promise.resolve();
},
[history]
[history, dispatch]
);

return authProvider ? login : loginWithoutProvider;
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-core/src/reducer/admin/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ import {
ShowNotificationAction,
HIDE_NOTIFICATION,
HideNotificationAction,
RESET_NOTIFICATION,
ResetNotificationAction,
NotificationPayload,
} from '../../actions/notificationActions';
import { UNDO, UndoAction } from '../../actions/undoActions';

type ActionTypes =
| ShowNotificationAction
| HideNotificationAction
| ResetNotificationAction
| UndoAction
| { type: 'OTHER_TYPE' };

type State = NotificationPayload[];

const initialState = [];

const notificationsReducer: Reducer<State> = (
previousState = [],
previousState = initialState,
action: ActionTypes
) => {
switch (action.type) {
Expand All @@ -26,6 +31,8 @@ const notificationsReducer: Reducer<State> = (
case HIDE_NOTIFICATION:
case UNDO:
return previousState.slice(1);
case RESET_NOTIFICATION:
return initialState;
default:
return previousState;
}
Expand Down