2020-08-06 16:27:51 +02:00
|
|
|
import React, { useReducer, useCallback, forwardRef, useImperativeHandle } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Padded, Flex } from '@buffetjs/core';
|
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import FormCard from '../FormBloc';
|
|
|
|
import getTrad from '../../utils/getTrad';
|
|
|
|
import Policies from '../Policies';
|
|
|
|
import Permissions from '../Permissions';
|
|
|
|
import reducer, { initialState } from './reducer';
|
|
|
|
import { UsersPermissionsProvider } from '../../contexts/UsersPermissionsContext';
|
|
|
|
import init from './init';
|
|
|
|
|
|
|
|
const UsersPermissions = forwardRef(({ permissions, routes, policies }, ref) => {
|
|
|
|
const { formatMessage } = useIntl();
|
|
|
|
const [state, dispatch] = useReducer(reducer, initialState, state =>
|
|
|
|
init(state, permissions, routes, policies)
|
|
|
|
);
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
getPermissions: () => {
|
|
|
|
return {
|
2020-08-27 15:00:10 +02:00
|
|
|
permissions: state.modifiedData,
|
2020-08-06 16:27:51 +02:00
|
|
|
};
|
|
|
|
},
|
2020-08-27 15:57:20 +02:00
|
|
|
resetForm: () => {
|
|
|
|
dispatch({ type: 'ON_RESET' });
|
|
|
|
},
|
|
|
|
setFormAfterSubmit: () => {
|
|
|
|
dispatch({ type: 'ON_SUBMIT_SUCCEEDED' });
|
|
|
|
},
|
2020-08-06 16:27:51 +02:00
|
|
|
}));
|
|
|
|
|
2020-08-27 15:00:10 +02:00
|
|
|
const handleChange = useCallback(({ target: { name, value } }) => {
|
2020-08-06 16:27:51 +02:00
|
|
|
dispatch({
|
2020-08-27 15:00:10 +02:00
|
|
|
type: 'ON_CHANGE',
|
|
|
|
keys: name.split('.'),
|
|
|
|
value: value === 'empty__string_value' ? '' : value,
|
2020-08-06 16:27:51 +02:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2020-08-27 15:00:10 +02:00
|
|
|
const handleChangeSelectAll = useCallback(({ target: { name, value } }) => {
|
2020-08-06 16:27:51 +02:00
|
|
|
dispatch({
|
2020-08-27 15:00:10 +02:00
|
|
|
type: 'ON_CHANGE_SELECT_ALL',
|
|
|
|
keys: name.split('.'),
|
|
|
|
value,
|
2020-08-06 16:27:51 +02:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2020-08-27 15:00:10 +02:00
|
|
|
const handleSelectedAction = useCallback(actionToSelect => {
|
2020-08-06 16:27:51 +02:00
|
|
|
dispatch({
|
2020-08-27 15:00:10 +02:00
|
|
|
type: 'SELECT_ACTION',
|
|
|
|
actionToSelect,
|
2020-08-06 16:27:51 +02:00
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const providerValue = {
|
|
|
|
...state,
|
2020-08-27 15:00:10 +02:00
|
|
|
onChange: handleChange,
|
|
|
|
onChangeSelectAll: handleChangeSelectAll,
|
2020-08-06 16:27:51 +02:00
|
|
|
onSelectedAction: handleSelectedAction,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<UsersPermissionsProvider value={providerValue}>
|
|
|
|
<Flex>
|
|
|
|
<FormCard
|
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('Plugins.header.title'),
|
|
|
|
})}
|
|
|
|
subtitle={formatMessage({
|
|
|
|
id: getTrad('Plugins.header.description'),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Padded left right size="xs">
|
|
|
|
<Permissions />
|
|
|
|
</Padded>
|
|
|
|
</FormCard>
|
|
|
|
<Policies />
|
|
|
|
</Flex>
|
|
|
|
</UsersPermissionsProvider>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
UsersPermissions.propTypes = {
|
|
|
|
permissions: PropTypes.object.isRequired,
|
|
|
|
routes: PropTypes.object.isRequired,
|
|
|
|
policies: PropTypes.array.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UsersPermissions;
|