mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 23:30:32 +00:00
feat: delete & edit role.
This commit is contained in:
@@ -20,6 +20,9 @@ function RoleDeleteAlert({
|
|||||||
// #withAlertStoreConnect
|
// #withAlertStoreConnect
|
||||||
isOpen,
|
isOpen,
|
||||||
payload: { roleId },
|
payload: { roleId },
|
||||||
|
|
||||||
|
// #withAlertActions
|
||||||
|
closeAlert,
|
||||||
}) {
|
}) {
|
||||||
const { mutateAsync: deleteRole, isLoading } = useDeleteRole();
|
const { mutateAsync: deleteRole, isLoading } = useDeleteRole();
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import ExpensesAlerts from '../Expenses/ExpensesAlerts';
|
|||||||
import AccountTransactionsAlerts from '../CashFlow/AccountTransactions/AccountTransactionsAlerts';
|
import AccountTransactionsAlerts from '../CashFlow/AccountTransactions/AccountTransactionsAlerts';
|
||||||
import UsersAlerts from '../Preferences/Users/UsersAlerts';
|
import UsersAlerts from '../Preferences/Users/UsersAlerts';
|
||||||
import CurrenciesAlerts from '../Preferences/Currencies/CurrenciesAlerts';
|
import CurrenciesAlerts from '../Preferences/Currencies/CurrenciesAlerts';
|
||||||
|
import RolesAlerts from '../Preferences/Users/Roles/RolesAlerts';
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
...AccountsAlerts,
|
...AccountsAlerts,
|
||||||
@@ -36,4 +37,5 @@ export default [
|
|||||||
...AccountTransactionsAlerts,
|
...AccountTransactionsAlerts,
|
||||||
...UsersAlerts,
|
...UsersAlerts,
|
||||||
...CurrenciesAlerts,
|
...CurrenciesAlerts,
|
||||||
|
...RolesAlerts,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import RoleDeleteAlert from '../../../Alerts/Roles/RoleDeleteAlert';
|
const RoleDeleteAlert = React.lazy(() =>
|
||||||
|
import('../../../Alerts/Roles/RoleDeleteAlert'),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Roles alerts
|
||||||
|
*/
|
||||||
export default [{ name: 'role-delete', component: RoleDeleteAlert }];
|
export default [{ name: 'role-delete', component: RoleDeleteAlert }];
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import { Formik } from 'formik';
|
import { Formik } from 'formik';
|
||||||
import { mapKeys, omit, pick } from 'lodash';
|
import { defaultTo, sumBy, isEmpty } from 'lodash';
|
||||||
|
|
||||||
import 'style/pages/Preferences/Roles/Form.scss';
|
import 'style/pages/Preferences/Roles/Form.scss';
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ import { AppToaster, FormattedMessage as T } from 'components';
|
|||||||
import { CreateRolesFormSchema, EditRolesFormSchema } from './RolesForm.schema';
|
import { CreateRolesFormSchema, EditRolesFormSchema } from './RolesForm.schema';
|
||||||
|
|
||||||
import { useRolesFormContext } from './RolesFormProvider';
|
import { useRolesFormContext } from './RolesFormProvider';
|
||||||
import { mapperPermissionSchema } from './utils';
|
import { transformToArray } from './utils';
|
||||||
|
|
||||||
import RolesFormContent from './RolesFormContent';
|
import RolesFormContent from './RolesFormContent';
|
||||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||||
@@ -43,7 +43,7 @@ function RolesForm({
|
|||||||
// Initial values.
|
// Initial values.
|
||||||
const initialValues = {
|
const initialValues = {
|
||||||
...defaultValues,
|
...defaultValues,
|
||||||
// ...transformToForm(permissionSchema, defaultValues),
|
...transformToForm(permissionSchema, defaultValues),
|
||||||
};
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
@@ -51,7 +51,7 @@ function RolesForm({
|
|||||||
}, [changePreferencesPageTitle]);
|
}, [changePreferencesPageTitle]);
|
||||||
|
|
||||||
const handleFormSubmit = (values, { setSubmitting }) => {
|
const handleFormSubmit = (values, { setSubmitting }) => {
|
||||||
const permission = mapperPermissionSchema(values);
|
const permission = transformToArray(values);
|
||||||
const form = {
|
const form = {
|
||||||
...values,
|
...values,
|
||||||
permissions: permission,
|
permissions: permission,
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ export default function RolesFormContent() {
|
|||||||
const handleCloseClick = () => {
|
const handleCloseClick = () => {
|
||||||
history.go(-1);
|
history.go(-1);
|
||||||
};
|
};
|
||||||
console.log(values, 'EE');
|
|
||||||
return (
|
return (
|
||||||
<Form>
|
<Form>
|
||||||
{/* ---------- name ---------- */}
|
{/* ---------- name ---------- */}
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
rfc
|
|
||||||
@@ -7,7 +7,7 @@ import { RolesFormProvider } from './RolesFormProvider';
|
|||||||
*/
|
*/
|
||||||
export default function RolesFormPage() {
|
export default function RolesFormPage() {
|
||||||
return (
|
return (
|
||||||
<RolesFormProvider>
|
<RolesFormProvider roleId={undefined}>
|
||||||
<RolesForm />
|
<RolesForm />
|
||||||
</RolesFormProvider>
|
</RolesFormProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
import _ from 'lodash';
|
import _, { isArray } from 'lodash';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useCreateRolePermissionSchema,
|
useCreateRolePermissionSchema,
|
||||||
@@ -10,14 +10,14 @@ import {
|
|||||||
useRolePermission,
|
useRolePermission,
|
||||||
} from 'hooks/query';
|
} from 'hooks/query';
|
||||||
import PreferencesPageLoader from '../../../PreferencesPageLoader';
|
import PreferencesPageLoader from '../../../PreferencesPageLoader';
|
||||||
import { mapperPermissionSchema } from './utils';
|
import { transformToObject } from './utils';
|
||||||
|
|
||||||
const RolesFormContext = React.createContext();
|
const RolesFormContext = React.createContext();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Roles Form page provider.
|
* Roles Form page provider.
|
||||||
*/
|
*/
|
||||||
function RolesFormProvider({ ...props }) {
|
function RolesFormProvider({ roleId, ...props }) {
|
||||||
// Create and edit roles mutations.
|
// Create and edit roles mutations.
|
||||||
const { mutateAsync: createRolePermissionMutate } =
|
const { mutateAsync: createRolePermissionMutate } =
|
||||||
useCreateRolePermissionSchema();
|
useCreateRolePermissionSchema();
|
||||||
@@ -31,15 +31,17 @@ function RolesFormProvider({ ...props }) {
|
|||||||
isFetching: isPermissionsSchemaFetching,
|
isFetching: isPermissionsSchemaFetching,
|
||||||
} = usePermissionsSchema();
|
} = usePermissionsSchema();
|
||||||
|
|
||||||
const roleId = 6;
|
// const roleId = 6;
|
||||||
|
|
||||||
const { data: permissionSchema, isLoading: isPermissionLoading } =
|
const { data: permission, isLoading: isPermissionLoading } =
|
||||||
useRolePermission(roleId, {
|
useRolePermission(roleId, {
|
||||||
enabled: !!roleId,
|
enabled: !!roleId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const isNewMode = !roleId;
|
const isNewMode = !roleId;
|
||||||
|
|
||||||
|
const permissionSchema = transformToObject(permission);
|
||||||
|
|
||||||
// Provider state.
|
// Provider state.
|
||||||
const provider = {
|
const provider = {
|
||||||
isNewMode,
|
isNewMode,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
export const mapperPermissionSchema = ({ permissions }) => {
|
import { isEmpty } from 'lodash';
|
||||||
|
|
||||||
|
export const transformToArray = ({ permissions }) => {
|
||||||
return Object.keys(permissions).map((index) => {
|
return Object.keys(permissions).map((index) => {
|
||||||
const [value, key] = index.split('/');
|
const [value, key] = index.split('/');
|
||||||
|
|
||||||
@@ -9,3 +11,17 @@ export const mapperPermissionSchema = ({ permissions }) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const transformToObject = ({ name, description, permissions }) => {
|
||||||
|
if (!isEmpty(permissions)) {
|
||||||
|
const output = {};
|
||||||
|
permissions.forEach((item) => {
|
||||||
|
output[`${item.subject}/${item.ability}`] = !!item.value;
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
role_name: name,
|
||||||
|
role_description: description,
|
||||||
|
permissions: { ...output },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,15 +5,24 @@ import { DataTable } from 'components';
|
|||||||
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
||||||
|
|
||||||
import { useRolesTableColumns, ActionsMenu } from './components';
|
import { useRolesTableColumns, ActionsMenu } from './components';
|
||||||
|
import withAlertsActions from 'containers/Alert/withAlertActions';
|
||||||
|
|
||||||
import { compose } from 'utils';
|
import { compose } from 'utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Roles data table.
|
* Roles data table.
|
||||||
*/
|
*/
|
||||||
export default function RolesDataTable() {
|
function RolesDataTable({
|
||||||
|
// #withAlertsActions
|
||||||
|
openAlert,
|
||||||
|
}) {
|
||||||
const columns = useRolesTableColumns();
|
const columns = useRolesTableColumns();
|
||||||
|
|
||||||
|
const handleDeleteRole = ({ id }) => {
|
||||||
|
openAlert('role-delete', { roleId: id });
|
||||||
|
};
|
||||||
|
|
||||||
|
// const Data = [{ name: 'AH', description: 'Description' }];
|
||||||
return (
|
return (
|
||||||
<DataTable
|
<DataTable
|
||||||
columns={columns}
|
columns={columns}
|
||||||
@@ -22,7 +31,11 @@ export default function RolesDataTable() {
|
|||||||
// progressBarLoading={}
|
// progressBarLoading={}
|
||||||
TableLoadingRenderer={TableSkeletonRows}
|
TableLoadingRenderer={TableSkeletonRows}
|
||||||
ContextMenu={ActionsMenu}
|
ContextMenu={ActionsMenu}
|
||||||
// payload={{}}
|
payload={{
|
||||||
|
onDeleteRole: handleDeleteRole,
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default compose(withAlertsActions)(RolesDataTable);
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ import React from 'react';
|
|||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
|
|
||||||
import { Intent, Button, Menu, MenuItem, MenuDivider } from '@blueprintjs/core';
|
import { Intent, Button, Menu, MenuItem, MenuDivider } from '@blueprintjs/core';
|
||||||
import { safeInvoke } from 'utils';
|
import { safeCallback } from 'utils';
|
||||||
import { Icon, If } from 'components';
|
import { Icon, If } from 'components';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context menu of roles.
|
* Context menu of roles.
|
||||||
*/
|
*/
|
||||||
export function ActionsMenu({ payload: {}, row: { original } }) {
|
export function ActionsMenu({ payload: { onDeleteRole }, row: { original } }) {
|
||||||
return (
|
return (
|
||||||
<Menu>
|
<Menu>
|
||||||
<MenuItem
|
<MenuItem
|
||||||
@@ -19,6 +19,7 @@ export function ActionsMenu({ payload: {}, row: { original } }) {
|
|||||||
<MenuItem
|
<MenuItem
|
||||||
icon={<Icon icon="trash-16" iconSize={16} />}
|
icon={<Icon icon="trash-16" iconSize={16} />}
|
||||||
text={intl.get('roles.delete_roles')}
|
text={intl.get('roles.delete_roles')}
|
||||||
|
onClick={safeCallback(onDeleteRole, original)}
|
||||||
intent={Intent.DANGER}
|
intent={Intent.DANGER}
|
||||||
/>
|
/>
|
||||||
</Menu>
|
</Menu>
|
||||||
@@ -35,7 +36,7 @@ export function useRolesTableColumns() {
|
|||||||
{
|
{
|
||||||
id: 'name',
|
id: 'name',
|
||||||
Header: intl.get('roles.column.name'),
|
Header: intl.get('roles.column.name'),
|
||||||
// accessor: ,
|
accessor: 'name',
|
||||||
className: 'name',
|
className: 'name',
|
||||||
width: '100',
|
width: '100',
|
||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
@@ -43,7 +44,7 @@ export function useRolesTableColumns() {
|
|||||||
{
|
{
|
||||||
id: 'description',
|
id: 'description',
|
||||||
Header: intl.get('roles.column.description'),
|
Header: intl.get('roles.column.description'),
|
||||||
// accessor: ,
|
accessor: 'description',
|
||||||
className: 'description',
|
className: 'description',
|
||||||
width: '120',
|
width: '120',
|
||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
|
|||||||
@@ -86,7 +86,9 @@ export function useRolePermission(role_id, props, requestProps) {
|
|||||||
{ method: 'get', url: `roles/${role_id}`, ...requestProps },
|
{ method: 'get', url: `roles/${role_id}`, ...requestProps },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.role,
|
select: (res) => res.data.role,
|
||||||
defaultData: [],
|
defaultData: {
|
||||||
|
permission: [],
|
||||||
|
},
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1476,7 +1476,7 @@
|
|||||||
"roles.label.role_name":"Role name",
|
"roles.label.role_name":"Role name",
|
||||||
"sidebar.transactions_locaking":"Transactions Locaking",
|
"sidebar.transactions_locaking":"Transactions Locaking",
|
||||||
"transactions_locking.dialog.label":"Transactions locking",
|
"transactions_locking.dialog.label":"Transactions locking",
|
||||||
"roles.permissions_schema.success_message":"The role has been created successfully.",
|
"roles.permission_schema.success_message":"The role has been created successfully.",
|
||||||
"roles.permission_schema.upload_message":"The given role hsa been updated successfully.",
|
"roles.permission_schema.upload_message":"The given role hsa been updated successfully.",
|
||||||
"roles.permission_schema.delete.alert_message":"The given role has been deleted successfully.",
|
"roles.permission_schema.delete.alert_message":"The given role has been deleted successfully.",
|
||||||
"roles.permission_schema.once_delete_this_role_you_will_able_to_restore_it":""
|
"roles.permission_schema.once_delete_this_role_you_will_able_to_restore_it":""
|
||||||
|
|||||||
Reference in New Issue
Block a user