mirror of
https://github.com/strapi/strapi.git
synced 2025-08-05 23:40:04 +00:00
create getActionState
This commit is contained in:
parent
1b184551f8
commit
e1012e5b51
@ -1,8 +1,8 @@
|
|||||||
export const permissions = {
|
export const permissions = {
|
||||||
collectionTypes: {
|
collectionTypes: {
|
||||||
'api::category': {
|
'api::category': {
|
||||||
create: true,
|
create: false,
|
||||||
findOne: true,
|
findOne: false,
|
||||||
find: false,
|
find: false,
|
||||||
update: false,
|
update: false,
|
||||||
delete: false,
|
delete: false,
|
||||||
|
@ -29,8 +29,7 @@ import { useRouteMatch, useHistory } from 'react-router-dom';
|
|||||||
import { useQuery } from 'react-query';
|
import { useQuery } from 'react-query';
|
||||||
import { formatAPIErrors } from '../../../../../utils';
|
import { formatAPIErrors } from '../../../../../utils';
|
||||||
import { axiosInstance } from '../../../../../core/utils';
|
import { axiosInstance } from '../../../../../core/utils';
|
||||||
import schema from './utils/schema';
|
import { getDateOfExpiration, schema, getActionsState } from './utils';
|
||||||
import getDateOfExpiration from './utils/getDateOfExpiration';
|
|
||||||
import LoadingView from './components/LoadingView';
|
import LoadingView from './components/LoadingView';
|
||||||
import HeaderContentBox from './components/ContentBox';
|
import HeaderContentBox from './components/ContentBox';
|
||||||
import Permissions from './components/Permissions';
|
import Permissions from './components/Permissions';
|
||||||
@ -140,13 +139,23 @@ const ApiTokenCreateView = () => {
|
|||||||
|
|
||||||
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
||||||
|
|
||||||
const areAllActionsSelected = Object.values(dataToCheck).every(actions =>
|
const areAllActionsSelected = getActionsState(dataToCheck, true);
|
||||||
Object.values(actions).every(action => action)
|
|
||||||
);
|
|
||||||
|
|
||||||
return areAllActionsSelected;
|
return areAllActionsSelected;
|
||||||
}, [state]);
|
}, [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 hasReadOnlyActionsSelected = useMemo(() => {
|
||||||
const {
|
const {
|
||||||
modifiedData: { collectionTypes, singleTypes, custom },
|
modifiedData: { collectionTypes, singleTypes, custom },
|
||||||
@ -154,15 +163,7 @@ const ApiTokenCreateView = () => {
|
|||||||
|
|
||||||
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
const dataToCheck = { ...collectionTypes, ...singleTypes, ...custom };
|
||||||
|
|
||||||
const areAllActionsReadOnly = Object.values(dataToCheck).every(actions =>
|
const areAllActionsReadOnly = getActionsState(dataToCheck, false, ['find', 'findOne']);
|
||||||
Object.values(actions).every(action => {
|
|
||||||
if (action === 'find' || action === 'findOne') {
|
|
||||||
return actions[action];
|
|
||||||
}
|
|
||||||
|
|
||||||
return actions[action] === false;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return areAllActionsReadOnly;
|
return areAllActionsReadOnly;
|
||||||
}, [state]);
|
}, [state]);
|
||||||
@ -172,8 +173,10 @@ const ApiTokenCreateView = () => {
|
|||||||
|
|
||||||
if (hasReadOnlyActionsSelected) return 'read-only';
|
if (hasReadOnlyActionsSelected) return 'read-only';
|
||||||
|
|
||||||
|
if (hasAllActionsNotSelected) return null;
|
||||||
|
|
||||||
return 'custom';
|
return 'custom';
|
||||||
}, [hasAllActionsSelected, hasReadOnlyActionsSelected]);
|
}, [hasAllActionsSelected, hasReadOnlyActionsSelected, hasAllActionsNotSelected]);
|
||||||
|
|
||||||
const handleChangeCheckbox = ({ target: { name, value } }) => {
|
const handleChangeCheckbox = ({ target: { name, value } }) => {
|
||||||
dispatch({
|
dispatch({
|
||||||
@ -191,42 +194,28 @@ const ApiTokenCreateView = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const handleChangeSelectApiTokenType = ({ target: { value } }) => {
|
const handleChangeSelectApiTokenType = ({ target: { value } }) => {
|
||||||
const {
|
const { modifiedData } = state;
|
||||||
modifiedData: { collectionTypes, singleTypes },
|
|
||||||
} = state;
|
|
||||||
|
|
||||||
if (value === 'full-access') {
|
if (value === 'full-access') {
|
||||||
Object.keys(collectionTypes).forEach(collectionType => {
|
Object.keys(modifiedData).forEach(contentTypes => {
|
||||||
|
Object.keys(modifiedData[contentTypes]).forEach(contentType => {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'ON_CHANGE_SELECT_ALL',
|
type: 'ON_CHANGE_SELECT_ALL',
|
||||||
keys: ['collectionTypes', collectionType],
|
keys: [contentTypes, contentType],
|
||||||
value: true,
|
value: true,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.keys(singleTypes).forEach(singleType => {
|
|
||||||
dispatch({
|
|
||||||
type: 'ON_CHANGE_SELECT_ALL',
|
|
||||||
keys: ['singleTypes', singleType],
|
|
||||||
value: true,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (value === 'read-only') {
|
if (value === 'read-only') {
|
||||||
Object.keys(collectionTypes).forEach(collectionType => {
|
Object.keys(modifiedData).forEach(contentTypes => {
|
||||||
|
Object.keys(modifiedData[contentTypes]).forEach(contentType => {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'ON_CHANGE_READ_ONLY',
|
type: 'ON_CHANGE_READ_ONLY',
|
||||||
keys: ['collectionTypes', collectionType],
|
keys: [contentTypes, contentType],
|
||||||
value: false,
|
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={{
|
initialValues={{
|
||||||
name: apiToken?.name || '',
|
name: apiToken?.name || '',
|
||||||
description: apiToken?.description || '',
|
description: apiToken?.description || '',
|
||||||
type: apiToken?.type || 'read-only',
|
type: apiToken?.type,
|
||||||
duration: apiToken?.duration,
|
duration: apiToken?.duration,
|
||||||
}}
|
}}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
@ -440,6 +429,7 @@ const ApiTokenCreateView = () => {
|
|||||||
handleChangeSelectApiTokenType({ target: { value } });
|
handleChangeSelectApiTokenType({ target: { value } });
|
||||||
handleChange({ target: { name: 'type', value } });
|
handleChange({ target: { name: 'type', value } });
|
||||||
}}
|
}}
|
||||||
|
placeholder="Select"
|
||||||
required
|
required
|
||||||
>
|
>
|
||||||
<Option value="read-only">
|
<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