Add notification for min when deleting a group field

This commit is contained in:
soupette 2019-07-16 16:06:46 +02:00
parent d57588f728
commit cf6e3e0536
5 changed files with 55 additions and 13 deletions

View File

@ -32,6 +32,6 @@ module.exports = {
// .default, // .default,
'strapi-plugin-email': require('../../../strapi-plugin-email/admin/src') 'strapi-plugin-email': require('../../../strapi-plugin-email/admin/src')
.default, .default,
// 'strapi-plugin-upload': require('../../../strapi-plugin-upload/admin/src') 'strapi-plugin-upload': require('../../../strapi-plugin-upload/admin/src')
// .default, .default,
}; };

View File

@ -36,7 +36,16 @@ function reducer(state, action) {
}); });
}); });
case 'REMOVE_COLLAPSE': case 'REMOVE_COLLAPSE':
return state.removeIn(['collapses', action.index]); return state
.removeIn(['collapses', action.index])
.update('collapses', list => list.map(obj => obj.set('isOpen', false)))
.update('collapses', list => {
if (action.shouldAddEmptyField) {
return list.push(fromJS({ isOpen: true }));
}
return list;
});
default: default:
return state; return state;
} }
@ -61,7 +70,7 @@ function Group({
isRepeatable, isRepeatable,
label, label,
layout, layout,
// min, min,
max, max,
modifiedData, modifiedData,
moveGroupField, moveGroupField,
@ -177,11 +186,20 @@ function Group({
removeField={e => { removeField={e => {
e.stopPropagation(); e.stopPropagation();
if (groupValue.length - 1 < min) {
strapi.notification.info(
'A empty field has been added to match the requirements'
);
}
const shouldAddEmptyField = groupValue.length - 1 < min;
dispatch({ dispatch({
type: 'REMOVE_COLLAPSE', type: 'REMOVE_COLLAPSE',
index, index,
shouldAddEmptyField,
}); });
removeField(`${name}.${index}`); removeField(`${name}.${index}`, shouldAddEmptyField);
}} }}
/> />
</div> </div>
@ -224,6 +242,7 @@ Group.defaultProps = {
label: '', label: '',
layout: {}, layout: {},
max: Infinity, max: Infinity,
min: -Infinity,
modifiedData: {}, modifiedData: {},
onChange: () => {}, onChange: () => {},
}; };
@ -235,6 +254,7 @@ Group.propTypes = {
label: PropTypes.string, label: PropTypes.string,
layout: PropTypes.object, layout: PropTypes.object,
max: PropTypes.number, max: PropTypes.number,
min: PropTypes.number,
modifiedData: PropTypes.object, modifiedData: PropTypes.object,
moveGroupField: PropTypes.func.isRequired, moveGroupField: PropTypes.func.isRequired,
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,

View File

@ -352,10 +352,11 @@ function EditView({
}); });
}} }}
layout={get(groupLayoutsData, group.group, {})} layout={get(groupLayoutsData, group.group, {})}
removeField={keys => { removeField={(keys, shouldAddEmptyField) => {
dispatch({ dispatch({
type: 'ON_REMOVE_FIELD', type: 'ON_REMOVE_FIELD',
keys: keys.split('.'), keys: keys.split('.'),
shouldAddEmptyField,
}); });
}} }}
/> />

View File

@ -10,6 +10,13 @@ const initialState = fromJS({
defaultGroupValues: {}, defaultGroupValues: {},
}); });
const getMax = arr => {
if (arr.size === 0) {
return -1;
}
return Math.max.apply(Math, arr.toJS().map(o => o._temp__id));
};
function reducer(state, action) { function reducer(state, action) {
switch (action.type) { switch (action.type) {
case 'ADD_FIELD_TO_GROUP': case 'ADD_FIELD_TO_GROUP':
@ -21,12 +28,7 @@ function reducer(state, action) {
]); ]);
if (list) { if (list) {
const max = Math.max.apply( const max = getMax(list);
Math,
list.toJS().map(function(o) {
return o._temp__id;
})
);
return list.push( return list.push(
fromJS( fromJS(
@ -81,7 +83,22 @@ function reducer(state, action) {
() => action.value () => action.value
); );
case 'ON_REMOVE_FIELD': case 'ON_REMOVE_FIELD':
return state.removeIn(['modifiedData', ...action.keys]); return state
.removeIn(['modifiedData', ...action.keys])
.updateIn(['modifiedData', action.keys[0]], list => {
if (action.shouldAddEmptyField) {
const defaultAttribute = state.getIn([
'defaultGroupValues',
action.keys[0],
'defaultRepeatable',
]);
const max = getMax(list);
return list.push(defaultAttribute.set('_temp__id', max + 1));
}
return list;
});
case 'RESET_FORM': case 'RESET_FORM':
return state.update('modifiedData', () => state.get('initialData')); return state.update('modifiedData', () => state.get('initialData'));
case 'SET_COLLAPSES_COMPONENTS_STATE': case 'SET_COLLAPSES_COMPONENTS_STATE':

View File

@ -201,6 +201,7 @@ module.exports = {
articles: { articles: {
type: 'relation', type: 'relation',
targetModel: 'article',
relationType: 'manyToMany', relationType: 'manyToMany',
}, },
}, },
@ -305,6 +306,7 @@ module.exports = {
}, },
tags: { tags: {
type: 'relation', type: 'relation',
targetModel: 'tag',
relationType: 'manyToMany', relationType: 'manyToMany',
}, },
pics: { pics: {
@ -520,6 +522,8 @@ module.exports = {
}, },
role: { role: {
type: 'relation', type: 'relation',
targetModel: 'role',
plugin: 'users-permissions',
relationType: 'manyToOne', relationType: 'manyToOne',
}, },
}, },