mirror of
https://github.com/strapi/strapi.git
synced 2025-08-05 15:29:04 +00:00
create getActionState
This commit is contained in:
parent
1b184551f8
commit
e1012e5b51
@ -1,8 +1,8 @@
|
||||
export const permissions = {
|
||||
collectionTypes: {
|
||||
'api::category': {
|
||||
create: true,
|
||||
findOne: true,
|
||||
create: false,
|
||||
findOne: false,
|
||||
find: false,
|
||||
update: false,
|
||||
delete: false,
|
||||
|
@ -29,8 +29,7 @@ import { useRouteMatch, useHistory } from 'react-router-dom';
|
||||
import { useQuery } from 'react-query';
|
||||
import { formatAPIErrors } from '../../../../../utils';
|
||||
import { axiosInstance } from '../../../../../core/utils';
|
||||
import schema from './utils/schema';
|
||||
import getDateOfExpiration from './utils/getDateOfExpiration';
|
||||
import { getDateOfExpiration, schema, getActionsState } from './utils';
|
||||
import LoadingView from './components/LoadingView';
|
||||
import HeaderContentBox from './components/ContentBox';
|
||||
import Permissions from './components/Permissions';
|
||||
@ -140,13 +139,23 @@ const ApiTokenCreateView = () => {
|
||||
|
||||
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
||||
|
||||
const areAllActionsSelected = Object.values(dataToCheck).every(actions =>
|
||||
Object.values(actions).every(action => action)
|
||||
);
|
||||
const areAllActionsSelected = getActionsState(dataToCheck, true);
|
||||
|
||||
return areAllActionsSelected;
|
||||
}, [state]);
|
||||
|
||||
const hasAllActionsNotSelected = useMemo(() => {
|
||||
const {
|
||||
modifiedData: { collectionTypes, singleTypes, custom },
|
||||
} = state;
|
||||
|
||||
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
||||
|
||||
const areAllActionsNotSelected = getActionsState(dataToCheck, false);
|
||||
|
||||
return areAllActionsNotSelected;
|
||||
}, [state]);
|
||||
|
||||
const hasReadOnlyActionsSelected = useMemo(() => {
|
||||
const {
|
||||
modifiedData: { collectionTypes, singleTypes, custom },
|
||||
@ -154,15 +163,7 @@ const ApiTokenCreateView = () => {
|
||||
|
||||
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
||||
|
||||
const areAllActionsReadOnly = Object.values(dataToCheck).every(actions =>
|
||||
Object.values(actions).every(action => {
|
||||
if (action === 'find' || action === 'findOne') {
|
||||
return actions[action];
|
||||
}
|
||||
|
||||
return actions[action] === false;
|
||||
})
|
||||
);
|
||||
const areAllActionsReadOnly = getActionsState(dataToCheck, false, ['find', 'findOne']);
|
||||
|
||||
return areAllActionsReadOnly;
|
||||
}, [state]);
|
||||
@ -172,8 +173,10 @@ const ApiTokenCreateView = () => {
|
||||
|
||||
if (hasReadOnlyActionsSelected) return 'read-only';
|
||||
|
||||
if (hasAllActionsNotSelected) return null;
|
||||
|
||||
return 'custom';
|
||||
}, [hasAllActionsSelected, hasReadOnlyActionsSelected]);
|
||||
}, [hasAllActionsSelected, hasReadOnlyActionsSelected, hasAllActionsNotSelected]);
|
||||
|
||||
const handleChangeCheckbox = ({ target: { name, value } }) => {
|
||||
dispatch({
|
||||
@ -191,42 +194,28 @@ const ApiTokenCreateView = () => {
|
||||
});
|
||||
|
||||
const handleChangeSelectApiTokenType = ({ target: { value } }) => {
|
||||
const {
|
||||
modifiedData: { collectionTypes, singleTypes },
|
||||
} = state;
|
||||
const { modifiedData } = state;
|
||||
|
||||
if (value === 'full-access') {
|
||||
Object.keys(collectionTypes).forEach(collectionType => {
|
||||
Object.keys(modifiedData).forEach(contentTypes => {
|
||||
Object.keys(modifiedData[contentTypes]).forEach(contentType => {
|
||||
dispatch({
|
||||
type: 'ON_CHANGE_SELECT_ALL',
|
||||
keys: ['collectionTypes', collectionType],
|
||||
keys: [contentTypes, contentType],
|
||||
value: true,
|
||||
});
|
||||
});
|
||||
|
||||
Object.keys(singleTypes).forEach(singleType => {
|
||||
dispatch({
|
||||
type: 'ON_CHANGE_SELECT_ALL',
|
||||
keys: ['singleTypes', singleType],
|
||||
value: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
if (value === 'read-only') {
|
||||
Object.keys(collectionTypes).forEach(collectionType => {
|
||||
Object.keys(modifiedData).forEach(contentTypes => {
|
||||
Object.keys(modifiedData[contentTypes]).forEach(contentType => {
|
||||
dispatch({
|
||||
type: 'ON_CHANGE_READ_ONLY',
|
||||
keys: ['collectionTypes', collectionType],
|
||||
keys: [contentTypes, contentType],
|
||||
value: false,
|
||||
});
|
||||
});
|
||||
|
||||
Object.keys(singleTypes).forEach(singleType => {
|
||||
dispatch({
|
||||
type: 'ON_CHANGE_READ_ONLY',
|
||||
keys: ['singleTypes', singleType],
|
||||
value: false,
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -253,7 +242,7 @@ const ApiTokenCreateView = () => {
|
||||
initialValues={{
|
||||
name: apiToken?.name || '',
|
||||
description: apiToken?.description || '',
|
||||
type: apiToken?.type || 'read-only',
|
||||
type: apiToken?.type,
|
||||
duration: apiToken?.duration,
|
||||
}}
|
||||
onSubmit={handleSubmit}
|
||||
@ -440,6 +429,7 @@ const ApiTokenCreateView = () => {
|
||||
handleChangeSelectApiTokenType({ target: { value } });
|
||||
handleChange({ target: { name: 'type', value } });
|
||||
}}
|
||||
placeholder="Select"
|
||||
required
|
||||
>
|
||||
<Option value="read-only">
|
||||
|
@ -0,0 +1,13 @@
|
||||
const getActionsState = (dataToCheck, value, exceptions = []) => {
|
||||
return Object.values(dataToCheck).every(actions =>
|
||||
Object.keys(actions).every(action => {
|
||||
if (exceptions.includes(action)) {
|
||||
return actions[action] === !value;
|
||||
}
|
||||
|
||||
return actions[action] === value;
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export default getActionsState;
|
@ -0,0 +1,6 @@
|
||||
import getActionsState from './getActionsState';
|
||||
import getDateOfExpiration from './getDateOfExpiration';
|
||||
import schema from './schema';
|
||||
import togglePermissions from './togglePermissions';
|
||||
|
||||
export { getActionsState, getDateOfExpiration, schema, togglePermissions };
|
@ -0,0 +1,33 @@
|
||||
import getActionsState from '../getActionsState';
|
||||
|
||||
const data = {
|
||||
'api::category': {
|
||||
create: false,
|
||||
findOne: true,
|
||||
find: true,
|
||||
update: false,
|
||||
delete: false,
|
||||
},
|
||||
'api::country': {
|
||||
create: false,
|
||||
findOne: true,
|
||||
find: true,
|
||||
update: false,
|
||||
delete: false,
|
||||
},
|
||||
'api::homepage': {
|
||||
delete: false,
|
||||
find: true,
|
||||
update: false,
|
||||
},
|
||||
};
|
||||
|
||||
describe('ADMIN | Pages | API TOKENS | EditView', () => {
|
||||
it('should return true when only find and findOne are true', () => {
|
||||
expect(getActionsState(data, false, ['find', 'findOne'])).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if not all are true', () => {
|
||||
expect(getActionsState(data, true)).toBe(false);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user