mirror of
https://github.com/strapi/strapi.git
synced 2025-08-11 02:07:51 +00:00
Fix admin roles bugs. Wrong redirection after creating a role, reset and submit logic
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
38216554d5
commit
ae1fcdecf3
@ -1,4 +1,4 @@
|
|||||||
import React, { useReducer, forwardRef, useMemo, useImperativeHandle } from 'react';
|
import React, { memo, useReducer, forwardRef, useMemo, useImperativeHandle } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import Tabs from '../Tabs';
|
import Tabs from '../Tabs';
|
||||||
@ -24,6 +24,12 @@ const Permissions = forwardRef(({ role, permissionsLayout, rolePermissions }, re
|
|||||||
pluginsAndSettingsPermissions: state.pluginsAndSettingsPermissions,
|
pluginsAndSettingsPermissions: state.pluginsAndSettingsPermissions,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
resetForm: () => {
|
||||||
|
dispatch({ type: 'ON_RESET' });
|
||||||
|
},
|
||||||
|
setFormAfterSubmit: () => {
|
||||||
|
dispatch({ type: 'ON_SUBMIT_SUCCEEDED' });
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const allSingleTypesAttributes = useMemo(() => {
|
const allSingleTypesAttributes = useMemo(() => {
|
||||||
@ -211,4 +217,5 @@ Permissions.propTypes = {
|
|||||||
rolePermissions: PropTypes.object,
|
rolePermissions: PropTypes.object,
|
||||||
role: PropTypes.object,
|
role: PropTypes.object,
|
||||||
};
|
};
|
||||||
export default Permissions;
|
|
||||||
|
export default memo(Permissions);
|
||||||
|
@ -3,6 +3,7 @@ const init = (state, permissionsLayout, permissions, role) => {
|
|||||||
...state,
|
...state,
|
||||||
...permissions,
|
...permissions,
|
||||||
permissionsLayout,
|
permissionsLayout,
|
||||||
|
initialData: permissions,
|
||||||
isSuperAdmin: role && role.code === 'strapi-super-admin',
|
isSuperAdmin: role && role.code === 'strapi-super-admin',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -11,6 +11,7 @@ export const initialState = {
|
|||||||
permissionsLayout: {},
|
permissionsLayout: {},
|
||||||
contentTypesPermissions: {},
|
contentTypesPermissions: {},
|
||||||
pluginsAndSettingsPermissions: [],
|
pluginsAndSettingsPermissions: [],
|
||||||
|
initialData: {},
|
||||||
isSuperAdmin: false,
|
isSuperAdmin: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -455,6 +456,16 @@ const reducer = (state, action) =>
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'ON_RESET': {
|
||||||
|
draftState.contentTypesPermissions = state.initialData.contentTypesPermissions;
|
||||||
|
draftState.pluginsAndSettingsPermissions = state.initialData.pluginsAndSettingsPermissions;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'ON_SUBMIT_SUCCEEDED': {
|
||||||
|
draftState.initialData.contentTypesPermissions = state.contentTypesPermissions;
|
||||||
|
draftState.initialData.pluginsAndSettingsPermissions = state.pluginsAndSettingsPermissions;
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return draftState;
|
return draftState;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ describe('ADMIN | COMPONENTS | ROLE | init', () => {
|
|||||||
contentTypesPermissions: {},
|
contentTypesPermissions: {},
|
||||||
pluginsAndSettingsPermissions: [],
|
pluginsAndSettingsPermissions: [],
|
||||||
permissionsLayout: {},
|
permissionsLayout: {},
|
||||||
|
initialData: {},
|
||||||
isSuperAdmin: false,
|
isSuperAdmin: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,6 +39,11 @@ describe('ADMIN | COMPONENTS | ROLE | init', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
isSuperAdmin: true,
|
isSuperAdmin: true,
|
||||||
|
initialData: {
|
||||||
|
contentTypesPermissions: {
|
||||||
|
firstPermission: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(init(initialState, permissionsLayout, permissions, role)).toEqual(expected);
|
expect(init(initialState, permissionsLayout, permissions, role)).toEqual(expected);
|
||||||
|
@ -24,7 +24,12 @@ const EditPage = () => {
|
|||||||
const permissionsRef = useRef();
|
const permissionsRef = useRef();
|
||||||
|
|
||||||
const { isLoading: isLayoutLoading, data: permissionsLayout } = useFetchPermissionsLayout(id);
|
const { isLoading: isLayoutLoading, data: permissionsLayout } = useFetchPermissionsLayout(id);
|
||||||
const { role, permissions: rolePermissions, isLoading: isRoleLoading } = useFetchRole(id);
|
const {
|
||||||
|
role,
|
||||||
|
permissions: rolePermissions,
|
||||||
|
isLoading: isRoleLoading,
|
||||||
|
onSubmitSucceeded,
|
||||||
|
} = useFetchRole(id);
|
||||||
|
|
||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
const headerActions = (handleSubmit, handleReset) =>
|
const headerActions = (handleSubmit, handleReset) =>
|
||||||
@ -37,7 +42,10 @@ const EditPage = () => {
|
|||||||
defaultMessage: 'Reset',
|
defaultMessage: 'Reset',
|
||||||
}),
|
}),
|
||||||
disabled: role.code === 'strapi-super-admin',
|
disabled: role.code === 'strapi-super-admin',
|
||||||
onClick: handleReset,
|
onClick: () => {
|
||||||
|
handleReset();
|
||||||
|
permissionsRef.current.resetForm();
|
||||||
|
},
|
||||||
color: 'cancel',
|
color: 'cancel',
|
||||||
type: 'button',
|
type: 'button',
|
||||||
},
|
},
|
||||||
@ -95,6 +103,9 @@ const EditPage = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
permissionsRef.current.setFormAfterSubmit();
|
||||||
|
onSubmitSucceeded({ name: data.name, description: data.description });
|
||||||
|
|
||||||
strapi.notification.success('notification.success.saved');
|
strapi.notification.success('notification.success.saved');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.response);
|
console.error(err.response);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useReducer, useEffect } from 'react';
|
import { useCallback, useReducer, useEffect } from 'react';
|
||||||
import { request } from 'strapi-helper-plugin';
|
import { request } from 'strapi-helper-plugin';
|
||||||
|
|
||||||
import reducer, { initialState } from './reducer';
|
import reducer, { initialState } from './reducer';
|
||||||
@ -44,7 +44,14 @@ const useFetchRole = id => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return state;
|
const handleSubmitSucceeded = useCallback(data => {
|
||||||
|
dispatch({
|
||||||
|
type: 'ON_SUBMIT_SUCCEEDED',
|
||||||
|
...data,
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { ...state, onSubmitSucceeded: handleSubmitSucceeded };
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useFetchRole;
|
export default useFetchRole;
|
||||||
|
@ -20,6 +20,11 @@ const reducer = (state, action) =>
|
|||||||
draftState.isLoading = false;
|
draftState.isLoading = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'ON_SUBMIT_SUCCEEDED': {
|
||||||
|
draftState.role.name = action.name;
|
||||||
|
draftState.role.description = action.description;
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return draftState;
|
return draftState;
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ import schema from './utils/schema';
|
|||||||
const CreatePage = () => {
|
const CreatePage = () => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const [isSubmiting, setIsSubmiting] = useState(false);
|
const [isSubmiting, setIsSubmiting] = useState(false);
|
||||||
const { push } = useHistory();
|
const { replace } = useHistory();
|
||||||
const permissionsRef = useRef();
|
const permissionsRef = useRef();
|
||||||
const { emitEvent, settingsBaseURL } = useGlobalContext();
|
const { emitEvent, settingsBaseURL } = useGlobalContext();
|
||||||
const params = useRouteMatch(`${settingsBaseURL}/roles/duplicate/:id`);
|
const params = useRouteMatch(`${settingsBaseURL}/roles/duplicate/:id`);
|
||||||
@ -68,7 +68,7 @@ const CreatePage = () => {
|
|||||||
body: data,
|
body: data,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.then(res => {
|
.then(async res => {
|
||||||
const permissionsToSend = permissionsRef.current.getPermissions();
|
const permissionsToSend = permissionsRef.current.getPermissions();
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
@ -78,7 +78,7 @@ const CreatePage = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (res.data.id && !isEmpty(permissionsToSend)) {
|
if (res.data.id && !isEmpty(permissionsToSend)) {
|
||||||
return request(`/admin/roles/${res.data.id}/permissions`, {
|
await request(`/admin/roles/${res.data.id}/permissions`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: { permissions: formatPermissionsToApi(permissionsToSend) },
|
body: { permissions: formatPermissionsToApi(permissionsToSend) },
|
||||||
});
|
});
|
||||||
@ -87,15 +87,16 @@ const CreatePage = () => {
|
|||||||
return res;
|
return res;
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
setIsSubmiting(false);
|
||||||
strapi.notification.success('Settings.roles.created');
|
strapi.notification.success('Settings.roles.created');
|
||||||
push(`${settingsBaseURL}/roles/${res.data.id}`);
|
replace(`${settingsBaseURL}/roles/${res.data.id}`);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
setIsSubmiting(false);
|
||||||
strapi.notification.error('notification.error');
|
strapi.notification.error('notification.error');
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
setIsSubmiting(false);
|
|
||||||
strapi.unlockApp();
|
strapi.unlockApp();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -124,6 +124,7 @@ exports[`<PopUpWarning /> should render properly should match snapshot if onlyCo
|
|||||||
|
|
||||||
.c4 {
|
.c4 {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.c4 > div {
|
.c4 > div {
|
||||||
@ -140,8 +141,9 @@ exports[`<PopUpWarning /> should render properly should match snapshot if onlyCo
|
|||||||
}
|
}
|
||||||
|
|
||||||
.c4 > div > p {
|
.c4 > div > p {
|
||||||
|
width: 100%;
|
||||||
line-height: 1.8rem;
|
line-height: 1.8rem;
|
||||||
margin-bottom: 0;
|
margin: auto;
|
||||||
min-height: 36px;
|
min-height: 36px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,6 +362,7 @@ exports[`<PopUpWarning /> should render properly should match snapshot if onlyCo
|
|||||||
|
|
||||||
.c4 {
|
.c4 {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.c4 > div {
|
.c4 > div {
|
||||||
@ -376,8 +379,9 @@ exports[`<PopUpWarning /> should render properly should match snapshot if onlyCo
|
|||||||
}
|
}
|
||||||
|
|
||||||
.c4 > div > p {
|
.c4 > div > p {
|
||||||
|
width: 100%;
|
||||||
line-height: 1.8rem;
|
line-height: 1.8rem;
|
||||||
margin-bottom: 0;
|
margin: auto;
|
||||||
min-height: 36px;
|
min-height: 36px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user