mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
feat: delete & edit role.
This commit is contained in:
77
src/containers/Alerts/Roles/RoleDeleteAlert.js
Normal file
77
src/containers/Alerts/Roles/RoleDeleteAlert.js
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import intl from 'react-intl-universal';
|
||||||
|
import { FormattedMessage as T, FormattedHTMLMessage } from 'components';
|
||||||
|
import { Intent, Alert } from '@blueprintjs/core';
|
||||||
|
import { AppToaster } from 'components';
|
||||||
|
|
||||||
|
import { useDeleteRole } from 'hooks/query';
|
||||||
|
|
||||||
|
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
|
||||||
|
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||||
|
|
||||||
|
import { compose } from 'utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Role delete alert.
|
||||||
|
*/
|
||||||
|
function RoleDeleteAlert({
|
||||||
|
name,
|
||||||
|
|
||||||
|
// #withAlertStoreConnect
|
||||||
|
isOpen,
|
||||||
|
payload: { roleId },
|
||||||
|
}) {
|
||||||
|
const { mutateAsync: deleteRole, isLoading } = useDeleteRole();
|
||||||
|
|
||||||
|
// Handle cancel delete role alert.
|
||||||
|
const handleCancelDelete = () => {
|
||||||
|
closeAlert(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle confirm delete role.
|
||||||
|
const handleConfirmDeleteRole = () => {
|
||||||
|
deleteRole(roleId)
|
||||||
|
.then(() => {
|
||||||
|
AppToaster.show({
|
||||||
|
message: intl.get('roles.permission_schema.delete.alert_message'),
|
||||||
|
intent: Intent.SUCCESS,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(
|
||||||
|
({
|
||||||
|
response: {
|
||||||
|
data: { errors },
|
||||||
|
},
|
||||||
|
}) => {},
|
||||||
|
)
|
||||||
|
.finally(() => {
|
||||||
|
closeAlert(name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Alert
|
||||||
|
cancelButtonText={<T id={'cancel'} />}
|
||||||
|
confirmButtonText={<T id={'delete'} />}
|
||||||
|
icon="trash"
|
||||||
|
intent={Intent.DANGER}
|
||||||
|
isOpen={isOpen}
|
||||||
|
onCancel={handleCancelDelete}
|
||||||
|
onConfirm={handleConfirmDeleteRole}
|
||||||
|
loading={isLoading}
|
||||||
|
>
|
||||||
|
<p>
|
||||||
|
<FormattedHTMLMessage
|
||||||
|
id={
|
||||||
|
'roles.permission_schema.once_delete_this_role_you_will_able_to_restore_it'
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</p>
|
||||||
|
</Alert>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default compose(
|
||||||
|
withAlertStoreConnect(),
|
||||||
|
withAlertActions,
|
||||||
|
)(RoleDeleteAlert);
|
||||||
5
src/containers/Preferences/Users/Roles/RolesAlerts.js
Normal file
5
src/containers/Preferences/Users/Roles/RolesAlerts.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import RoleDeleteAlert from '../../../Alerts/Roles/RoleDeleteAlert';
|
||||||
|
|
||||||
|
export default [{ name: 'role-delete', component: RoleDeleteAlert }];
|
||||||
@@ -17,7 +17,7 @@ import { mapperPermissionSchema } from './utils';
|
|||||||
import RolesFormContent from './RolesFormContent';
|
import RolesFormContent from './RolesFormContent';
|
||||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||||
|
|
||||||
import { compose } from 'utils';
|
import { compose, transformToForm } from 'utils';
|
||||||
|
|
||||||
const defaultValues = {
|
const defaultValues = {
|
||||||
role_name: '',
|
role_name: '',
|
||||||
@@ -32,12 +32,18 @@ function RolesForm({
|
|||||||
// #withDashboardActions
|
// #withDashboardActions
|
||||||
changePreferencesPageTitle,
|
changePreferencesPageTitle,
|
||||||
}) {
|
}) {
|
||||||
const { createRolePermissionMutate, editRolePermissionMutate, permissions } =
|
const {
|
||||||
useRolesFormContext();
|
isNewMode,
|
||||||
|
createRolePermissionMutate,
|
||||||
|
editRolePermissionMutate,
|
||||||
|
permissionSchema,
|
||||||
|
roleId,
|
||||||
|
} = useRolesFormContext();
|
||||||
|
|
||||||
// Initial values.
|
// Initial values.
|
||||||
const initialValues = {
|
const initialValues = {
|
||||||
...defaultValues,
|
...defaultValues,
|
||||||
|
// ...transformToForm(permissionSchema, defaultValues),
|
||||||
};
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
@@ -53,7 +59,11 @@ function RolesForm({
|
|||||||
setSubmitting(true);
|
setSubmitting(true);
|
||||||
const onSuccess = () => {
|
const onSuccess = () => {
|
||||||
AppToaster.show({
|
AppToaster.show({
|
||||||
message: intl.get('roles.permisssion_schema.success_message'),
|
message: intl.get(
|
||||||
|
isNewMode
|
||||||
|
? 'roles.permission_schema.success_message'
|
||||||
|
: 'roles.permission_schema.upload_message',
|
||||||
|
),
|
||||||
intent: Intent.SUCCESS,
|
intent: Intent.SUCCESS,
|
||||||
});
|
});
|
||||||
setSubmitting(false);
|
setSubmitting(false);
|
||||||
@@ -62,13 +72,17 @@ function RolesForm({
|
|||||||
const onError = (errors) => {
|
const onError = (errors) => {
|
||||||
setSubmitting(false);
|
setSubmitting(false);
|
||||||
};
|
};
|
||||||
|
if (isNewMode) {
|
||||||
createRolePermissionMutate(form).then(onSuccess).catch(onError);
|
createRolePermissionMutate(form).then(onSuccess).catch(onError);
|
||||||
|
} else {
|
||||||
|
editRolePermissionMutate([roleId, form]).then(onSuccess).catch(onError);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
validationSchema={CreateRolesFormSchema}
|
validationSchema={isNewMode ? CreateRolesFormSchema : EditRolesFormSchema}
|
||||||
onSubmit={handleFormSubmit}
|
onSubmit={handleFormSubmit}
|
||||||
>
|
>
|
||||||
<RolesFormContent />
|
<RolesFormContent />
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export default function RolesFormContent() {
|
|||||||
const handleCloseClick = () => {
|
const handleCloseClick = () => {
|
||||||
history.go(-1);
|
history.go(-1);
|
||||||
};
|
};
|
||||||
console.log(values, 'XX');
|
console.log(values, 'EE');
|
||||||
return (
|
return (
|
||||||
<Form>
|
<Form>
|
||||||
{/* ---------- name ---------- */}
|
{/* ---------- name ---------- */}
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { flatMap, map } from 'lodash';
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useCreateRolePermissionSchema,
|
useCreateRolePermissionSchema,
|
||||||
useEditRolePermissionSchema,
|
useEditRolePermissionSchema,
|
||||||
usePermissionsSchema,
|
usePermissionsSchema,
|
||||||
useSaveSettings
|
useRolePermission,
|
||||||
} from 'hooks/query';
|
} from 'hooks/query';
|
||||||
import PreferencesPageLoader from '../../../PreferencesPageLoader';
|
import PreferencesPageLoader from '../../../PreferencesPageLoader';
|
||||||
|
import { mapperPermissionSchema } from './utils';
|
||||||
|
|
||||||
const RolesFormContext = React.createContext();
|
const RolesFormContext = React.createContext();
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ function RolesFormProvider({ ...props }) {
|
|||||||
// Create and edit roles mutations.
|
// Create and edit roles mutations.
|
||||||
const { mutateAsync: createRolePermissionMutate } =
|
const { mutateAsync: createRolePermissionMutate } =
|
||||||
useCreateRolePermissionSchema();
|
useCreateRolePermissionSchema();
|
||||||
|
|
||||||
const { mutateAsync: editRolePermissionMutate } =
|
const { mutateAsync: editRolePermissionMutate } =
|
||||||
useEditRolePermissionSchema();
|
useEditRolePermissionSchema();
|
||||||
|
|
||||||
@@ -29,17 +31,25 @@ function RolesFormProvider({ ...props }) {
|
|||||||
isFetching: isPermissionsSchemaFetching,
|
isFetching: isPermissionsSchemaFetching,
|
||||||
} = usePermissionsSchema();
|
} = usePermissionsSchema();
|
||||||
|
|
||||||
// Save Organization Settings.
|
const roleId = 6;
|
||||||
const { mutateAsync: saveSettingMutate } = useSaveSettings();
|
|
||||||
|
const { data: permissionSchema, isLoading: isPermissionLoading } =
|
||||||
|
useRolePermission(roleId, {
|
||||||
|
enabled: !!roleId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const isNewMode = !roleId;
|
||||||
|
|
||||||
// Provider state.
|
// Provider state.
|
||||||
const provider = {
|
const provider = {
|
||||||
|
isNewMode,
|
||||||
|
roleId,
|
||||||
permissionsSchema,
|
permissionsSchema,
|
||||||
|
permissionSchema,
|
||||||
isPermissionsSchemaLoading,
|
isPermissionsSchemaLoading,
|
||||||
isPermissionsSchemaFetching,
|
isPermissionsSchemaFetching,
|
||||||
createRolePermissionMutate,
|
createRolePermissionMutate,
|
||||||
editRolePermissionMutate,
|
editRolePermissionMutate,
|
||||||
saveSettingMutate
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -50,7 +60,7 @@ function RolesFormProvider({ ...props }) {
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div className={classNames(CLASSES.CARD)}>
|
<div className={classNames(CLASSES.CARD)}>
|
||||||
{isPermissionsSchemaLoading ? (
|
{isPermissionsSchemaLoading || isPermissionLoading ? (
|
||||||
<PreferencesPageLoader />
|
<PreferencesPageLoader />
|
||||||
) : (
|
) : (
|
||||||
<RolesFormContext.Provider value={provider} {...props} />
|
<RolesFormContext.Provider value={provider} {...props} />
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ const AbilitiesList = ({ subject, abilities }) => {
|
|||||||
{abilities?.map(({ key, label }) => (
|
{abilities?.map(({ key, label }) => (
|
||||||
<FastField name={`permissions.${subject}/${key}`} type="checkbox">
|
<FastField name={`permissions.${subject}/${key}`} type="checkbox">
|
||||||
{({ form: { setFieldValue, values }, field }) => (
|
{({ form: { setFieldValue, values }, field }) => (
|
||||||
<Checkbox inline={true} label={label} name={subject} {...field} />
|
<Checkbox inline={true} label={label} {...field} />
|
||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
))}
|
))}
|
||||||
@@ -46,7 +46,7 @@ const ExtraAbilitiesList = ({ subject, extraAbilities }) => {
|
|||||||
<AbilitieList>
|
<AbilitieList>
|
||||||
<FastField name={`permissions.${subject}/${key}`} type="checkbox">
|
<FastField name={`permissions.${subject}/${key}`} type="checkbox">
|
||||||
{({ form: { setFieldValue, values }, field }) => (
|
{({ form: { setFieldValue, values }, field }) => (
|
||||||
<Checkbox inline={true} label={label} name={subject} {...field} />
|
<Checkbox inline={true} label={label} {...field} />
|
||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
</AbilitieList>
|
</AbilitieList>
|
||||||
|
|||||||
@@ -75,3 +75,19 @@ export function usePermissionsSchema(query, props) {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the role permisstion schema.
|
||||||
|
* @param {number} role_id - role id.
|
||||||
|
*/
|
||||||
|
export function useRolePermission(role_id, props, requestProps) {
|
||||||
|
return useRequestQuery(
|
||||||
|
[t.ROLE, role_id],
|
||||||
|
{ method: 'get', url: `roles/${role_id}`, ...requestProps },
|
||||||
|
{
|
||||||
|
select: (res) => res.data.role,
|
||||||
|
defaultData: [],
|
||||||
|
...props,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1476,5 +1476,8 @@
|
|||||||
"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.permisssion_schema.success_message":"The role has been created successfully."
|
"roles.permissions_schema.success_message":"The role has been created 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.once_delete_this_role_you_will_able_to_restore_it":""
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,11 @@ export default [
|
|||||||
component: Roles,
|
component: Roles,
|
||||||
exact: true,
|
exact: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: `${BASE_URL}/roles/:id`,
|
||||||
|
component: Roles,
|
||||||
|
exact: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: `${BASE_URL}/currencies`,
|
path: `${BASE_URL}/currencies`,
|
||||||
component: Currencies,
|
component: Currencies,
|
||||||
|
|||||||
Reference in New Issue
Block a user