delete a role added

This commit is contained in:
ronronscelestes 2021-09-02 17:37:38 +02:00
parent 9928d066d1
commit c8f47d74a8

View File

@ -1,11 +1,11 @@
import { import {
LoadingIndicatorPage, LoadingIndicatorPage,
PopUpWarning,
SettingsPageTitle, SettingsPageTitle,
request, request,
useNotification, useNotification,
useQuery, useQuery,
useRBAC, useRBAC,
ConfirmDialog,
} from '@strapi/helper-plugin'; } from '@strapi/helper-plugin';
import { AddIcon, DeleteIcon, Duplicate, EditIcon } from '@strapi/icons'; import { AddIcon, DeleteIcon, Duplicate, EditIcon } from '@strapi/icons';
import { import {
@ -69,27 +69,17 @@ const useRoleActions = ({ getData, canCreate, canDelete, canUpdate, roles, sorte
const toggleNotification = useNotification(); const toggleNotification = useNotification();
const [isWarningDeleteAllOpened, setIsWarningDeleteAllOpenend] = useState(false); const [isWarningDeleteAllOpened, setIsWarningDeleteAllOpenend] = useState(false);
const { push } = useHistory(); const { push } = useHistory();
const [ const [{ selectedRoles, showModalConfirmButtonLoading }, dispatch] = useReducer(
{ selectedRoles, showModalConfirmButtonLoading, shouldRefetchData }, reducer,
dispatch, initialState
] = useReducer(reducer, initialState); );
const handleClosedModal = () => { const handleDeleteData = async () => {
if (shouldRefetchData) {
getData();
}
// Empty the selected ids when the modal closes
dispatch({
type: 'RESET_DATA_TO_DELETE',
});
};
const handleConfirmDeleteData = async () => {
try { try {
dispatch({ dispatch({
type: 'ON_REMOVE_ROLES', type: 'ON_REMOVE_ROLES',
}); });
const filteredRoles = selectedRoles.filter(currentId => { const filteredRoles = selectedRoles.filter(currentId => {
const currentRole = roles.find(role => role.id === currentId); const currentRole = roles.find(role => role.id === currentId);
@ -111,14 +101,13 @@ const useRoleActions = ({ getData, canCreate, canDelete, canUpdate, roles, sorte
}, },
}); });
// Empty the selectedRolesId and set the shouldRefetchData to true so the await getData();
// list is updated when closing the modal
dispatch({ dispatch({
type: 'ON_REMOVE_ROLES_SUCCEEDED', type: 'RESET_DATA_TO_DELETE',
}); });
} }
} catch (err) { } catch (err) {
console.error(err);
const errorIds = get(err, ['response', 'payload', 'data', 'ids'], null); const errorIds = get(err, ['response', 'payload', 'data', 'ids'], null);
if (errorIds && Array.isArray(errorIds)) { if (errorIds && Array.isArray(errorIds)) {
@ -133,9 +122,8 @@ const useRoleActions = ({ getData, canCreate, canDelete, canUpdate, roles, sorte
message: { id: 'notification.error' }, message: { id: 'notification.error' },
}); });
} }
} finally {
handleToggleModal();
} }
handleToggleModal();
}; };
const onRoleDuplicate = useCallback( const onRoleDuplicate = useCallback(
@ -246,8 +234,8 @@ const useRoleActions = ({ getData, canCreate, canDelete, canUpdate, roles, sorte
); );
return { return {
handleClosedModal, // handleClosedModal,
handleConfirmDeleteData, // handleConfirmDeleteData,
handleNewRoleClick, handleNewRoleClick,
onRoleToggle, onRoleToggle,
onAllRolesToggle, onAllRolesToggle,
@ -256,6 +244,7 @@ const useRoleActions = ({ getData, canCreate, canDelete, canUpdate, roles, sorte
isWarningDeleteAllOpened, isWarningDeleteAllOpened,
showModalConfirmButtonLoading, showModalConfirmButtonLoading,
handleToggleModal, handleToggleModal,
handleDeleteData,
}; };
}; };
@ -275,8 +264,8 @@ const RoleListPage = () => {
} = useSortedRoles(); } = useSortedRoles();
const { const {
handleClosedModal, // handleClosedModal,
handleConfirmDeleteData, // handleConfirmDeleteData,
handleNewRoleClick, handleNewRoleClick,
onRoleToggle, onRoleToggle,
onAllRolesToggle, onAllRolesToggle,
@ -285,6 +274,7 @@ const RoleListPage = () => {
isWarningDeleteAllOpened, isWarningDeleteAllOpened,
showModalConfirmButtonLoading, showModalConfirmButtonLoading,
handleToggleModal, handleToggleModal,
handleDeleteData,
} = useRoleActions({ getData, canCreate, canDelete, canUpdate, roles, sortedRoles }); } = useRoleActions({ getData, canCreate, canDelete, canUpdate, roles, sortedRoles });
// ! TODO - Show the search bar only if the user is allowed to read - add the search input // ! TODO - Show the search bar only if the user is allowed to read - add the search input
@ -409,12 +399,18 @@ const RoleListPage = () => {
{!rowCount && !isLoading && <EmptyRole />} {!rowCount && !isLoading && <EmptyRole />}
</ContentLayout> </ContentLayout>
)} )}
<PopUpWarning {/* <PopUpWarning
isOpen={isWarningDeleteAllOpened} isOpen={isWarningDeleteAllOpened}
onClosed={handleClosedModal} onClosed={handleClosedModal}
onConfirm={handleConfirmDeleteData} onConfirm={handleConfirmDeleteData}
toggleModal={handleToggleModal} toggleModal={handleToggleModal}
isConfirmButtonLoading={showModalConfirmButtonLoading} isConfirmButtonLoading={showModalConfirmButtonLoading}
/> */}
<ConfirmDialog
isVisible={isWarningDeleteAllOpened}
onConfirm={handleDeleteData}
isConfirmButtonLoading={showModalConfirmButtonLoading}
onToggleDialog={handleToggleModal}
/> />
</Main> </Main>
); );