feat: roles data & delete alert.

This commit is contained in:
elforjani13
2021-11-26 19:43:06 +02:00
parent c2ccb7f879
commit 97c421e2f1
9 changed files with 75 additions and 18 deletions

View File

@@ -5,6 +5,7 @@ import { Intent, Alert } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { useDeleteRole } from 'hooks/query';
import { handleDeleteErrors } from '../../Preferences/Users/Roles/utils';
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
import withAlertActions from 'containers/Alert/withAlertActions';
@@ -45,7 +46,9 @@ function RoleDeleteAlert({
response: {
data: { errors },
},
}) => {},
}) => {
handleDeleteErrors(errors);
},
)
.finally(() => {
closeAlert(name);

View File

@@ -1,7 +1,7 @@
import React from 'react';
import { useHistory } from 'react-router-dom';
import intl from 'react-intl-universal';
import { Formik } from 'formik';
import { defaultTo, sumBy, isEmpty } from 'lodash';
import 'style/pages/Preferences/Roles/Form.scss';
@@ -32,6 +32,9 @@ function RolesForm({
// #withDashboardActions
changePreferencesPageTitle,
}) {
// History context.
const history = useHistory();
const {
isNewMode,
createRolePermissionMutate,
@@ -67,6 +70,7 @@ function RolesForm({
intent: Intent.SUCCESS,
});
setSubmitting(false);
history.push('/preferences/users');
};
const onError = (errors) => {

View File

@@ -1,4 +1,5 @@
import React from 'react';
import { useParams } from 'react-router-dom';
import RolesForm from './RolesForm';
import { RolesFormProvider } from './RolesFormProvider';
@@ -6,8 +7,11 @@ import { RolesFormProvider } from './RolesFormProvider';
* Roles Form page.
*/
export default function RolesFormPage() {
const { id } = useParams();
const idInteger = parseInt(id, 10);
return (
<RolesFormProvider roleId={undefined}>
<RolesFormProvider roleId={idInteger}>
<RolesForm />
</RolesFormProvider>
);

View File

@@ -31,8 +31,6 @@ function RolesFormProvider({ roleId, ...props }) {
isFetching: isPermissionsSchemaFetching,
} = usePermissionsSchema();
// const roleId = 6;
const { data: permission, isLoading: isPermissionLoading } =
useRolePermission(roleId, {
enabled: !!roleId,

View File

@@ -1,4 +1,6 @@
import React from 'react';
import { useHistory } from 'react-router-dom';
import intl from 'react-intl-universal';
import { DataTable } from 'components';
@@ -6,6 +8,7 @@ import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
import { useRolesTableColumns, ActionsMenu } from './components';
import withAlertsActions from 'containers/Alert/withAlertActions';
import { useRolesContext } from './RolesListProvider';
import { compose } from 'utils';
@@ -16,23 +19,33 @@ function RolesDataTable({
// #withAlertsActions
openAlert,
}) {
// History context.
const history = useHistory();
const columns = useRolesTableColumns();
const { roles, isRolesFetching, isRolesLoading } = useRolesContext();
const handleDeleteRole = ({ id }) => {
openAlert('role-delete', { roleId: id });
};
// const Data = [{ name: 'AH', description: 'Description' }];
const handleEditRole = ({ id }) => {
history.push(`/preferences/roles/${id}`);
};
return (
<DataTable
columns={columns}
data={[]}
// loading={}
// progressBarLoading={}
data={roles}
loading={isRolesLoading}
headerLoading={isRolesFetching}
progressBarLoading={isRolesFetching}
TableLoadingRenderer={TableSkeletonRows}
ContextMenu={ActionsMenu}
payload={{
onDeleteRole: handleDeleteRole,
onEditRole: handleEditRole,
}}
/>
);

View File

@@ -1,7 +1,7 @@
import React from 'react';
import classNames from 'classnames';
import { CLASSES } from 'common/classes';
// import {} from 'hooks/query';
import { useRoles } from 'hooks/query';
const RolesListContext = React.createContext();
@@ -9,8 +9,20 @@ const RolesListContext = React.createContext();
* Roles list provider.
*/
function RolesListProvider({ ...props }) {
// fetch roles list.
const {
data: roles,
isFetching: isRolesFetching,
isLoading: isRolesLoading,
} = useRoles();
// Provider state.
const provider = {};
const provider = {
roles,
isRolesFetching,
isRolesLoading,
};
return (
<div
className={classNames(

View File

@@ -8,12 +8,16 @@ import { Icon, If } from 'components';
/**
* Context menu of roles.
*/
export function ActionsMenu({ payload: { onDeleteRole }, row: { original } }) {
export function ActionsMenu({
payload: { onDeleteRole, onEditRole },
row: { original },
}) {
return (
<Menu>
<MenuItem
icon={<Icon icon="pen-18" />}
text={intl.get('roles.edit_roles')}
onClick={safeCallback(onEditRole, original)}
/>
<MenuDivider />
<MenuItem
@@ -38,16 +42,16 @@ export function useRolesTableColumns() {
Header: intl.get('roles.column.name'),
accessor: 'name',
className: 'name',
width: '100',
disableSortBy: true,
width: '80',
textOverview: true,
},
{
id: 'description',
Header: intl.get('roles.column.description'),
accessor: 'description',
className: 'description',
width: '120',
disableSortBy: true,
width: '180',
textOverview: true,
},
],
[],

View File

@@ -5,8 +5,9 @@ import t from './types';
// Common invalidate queries.
const commonInvalidateQueries = (queryClient) => {
queryClient.invalidateQueries(t.ROLES_PERMISSIONS_SCHEMA);
queryClient.invalidateQueries(t.ROLE);
queryClient.invalidateQueries(t.ROLES);
queryClient.invalidateQueries(t.ROLES_PERMISSIONS_SCHEMA);
};
/**
@@ -93,3 +94,18 @@ export function useRolePermission(role_id, props, requestProps) {
},
);
}
/**
* Retrieve the roles.
*/
export function useRoles(props, query) {
return useRequestQuery(
[t.ROLES, query],
{ method: 'get', url: `roles`, params: query },
{
select: (res) => res.data.roles,
defaultData: [],
...props,
},
);
}

View File

@@ -1470,11 +1470,14 @@
"list.create":"Create {value}",
"roles.label":"Roles",
"roles.column.name":"Name",
"roles.column.description":"description",
"roles.column.description":"Description",
"roles.column.role_name":"Role Name",
"roles.edit_roles":"Edit Roles",
"roles.delete_roles":"Delete Roles",
"roles.label.role_name":"Role Name",
"roles.label.role_name_":"Role name",
"roles.error.role_is_predefined":"Role is predefined, you cannot delete predefined roles.",
"roles.error.you_cannot_change_your_own_role":"You cannot change your own role.",
"sidebar.transactions_locaking":"Transactions Locaking",
"transactions_locking.dialog.label":"Transactions locking",
"roles.permission_schema.success_message":"The role has been created successfully.",