Skip to content

Commit

Permalink
PATCH: Move impersonateRestore out of the redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
grebaldi committed May 30, 2024
1 parent eac76c0 commit 4c78c28
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import I18n from '@neos-project/neos-ui-i18n';
import {I18nRegistry} from '@neos-project/neos-ts-interfaces';
import backend from '@neos-project/neos-ui-backend-connector';

import {routes} from '../../../System';

import buttonTheme from './style.module.css';

type ImpersonateAccount = {
Expand All @@ -31,8 +33,9 @@ export const RestoreButtonItem: React.FC<{
originUser?: {
fullName: string;
};
onClick: () => void;
onError: (message: string) => void;
onLoadError: (message: string) => void;
onRestoreSuccess: (message: string) => void;
onRestoreError: (message: string) => void;
i18n: I18nRegistry;
}> = (props) => {
const [impersonateStatus, setImpersonateStatus] = React.useState<null | ImpersonateStatus>(null);
Expand All @@ -43,6 +46,42 @@ export const RestoreButtonItem: React.FC<{
'Neos.Neos',
'Main'
);
const errorMessage = props.i18n.translate(
'impersonate.error.restoreUser',
'Could not switch back to the original user.',
{},
'Neos.Neos',
'Main'
);
const handleClick = React.useCallback(
async function restoreOriginalUser() {
const {impersonateRestore} = backend.get().endpoints;
const feedback = await impersonateRestore();
const originUser = feedback?.origin?.accountIdentifier;
const user = feedback?.impersonate?.accountIdentifier;
const status = feedback?.status;

const restoreMessage = props.i18n.translate(
'impersonate.success.restoreUser',
'Switched back from {0} to the orginal user {1}.',
{
0: user,
1: originUser
},
'Neos.Neos',
'Main'
);

if (status) {
props.onRestoreSuccess(restoreMessage);
} else {
props.onRestoreError(errorMessage);
}

window.location.href = routes?.core?.modules?.defaultModule;
},
[props.i18n]
);

React.useEffect(
() => {
Expand All @@ -57,7 +96,7 @@ export const RestoreButtonItem: React.FC<{
setImpersonateStatus(impersonateStatus);
}
} catch (error) {
props.onError((error as Error).message);
props.onLoadError((error as Error).message);
}
})();
},
Expand All @@ -76,7 +115,7 @@ export const RestoreButtonItem: React.FC<{
<li className={buttonTheme.dropDown__item}>
<button
title={title}
onClick={props.onClick}
onClick={handleClick}
>
<Icon
icon="random"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {user} from '../../../System';

const withReduxState = connect(() => ({
}), {
impersonateRestore: actions.User.Impersonate.restore,
addFlashMessage: actions.UI.FlashMessages.add
});

Expand All @@ -37,7 +36,6 @@ const withNeosGlobals = neos(globalRegistry => ({
}));

const UserDropDown: React.FC<{
impersonateRestore: () => void;
addFlashMessage: typeof actions.UI.FlashMessages.add;
i18nRegistry: I18nRegistry;
neos: NeosContextInterface;
Expand Down Expand Up @@ -74,12 +72,21 @@ const UserDropDown: React.FC<{
</li>
<RestoreButtonItem
i18n={props.i18nRegistry}
onClick={() => props.impersonateRestore()}
onError={(message) => props.addFlashMessage(
onLoadError={(message) => props.addFlashMessage(
'impersonateStatusError',
message,
'error'
)}
onRestoreSuccess={(message) => props.addFlashMessage(
'restoreUserImpersonateUser',
message,
'success'
)}
onRestoreError={(message) => props.addFlashMessage(
'restoreUserImpersonateUser',
message,
'success'
)}
/>
</DropDown.Contents>
</DropDown>
Expand Down

0 comments on commit 4c78c28

Please sign in to comment.