Handle remove logic when removing the edited relation

This commit is contained in:
cyril lopez 2018-07-23 12:34:35 +02:00
parent 21c06998f5
commit 26c53c977e
6 changed files with 55 additions and 19 deletions

View File

@ -10,7 +10,7 @@ import {
DragSource, DragSource,
DropTarget, DropTarget,
} from 'react-dnd'; } from 'react-dnd';
import { flow, upperFirst } from 'lodash'; import { flow } from 'lodash';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import cn from 'classnames'; import cn from 'classnames';
@ -140,7 +140,7 @@ class DraggableAttr extends React.Component {
<i className="fa fa-th" aria-hidden="true" /> <i className="fa fa-th" aria-hidden="true" />
<span>{name}</span> <span>{name}</span>
<ClickOverHint show={isOver && !isDragging} /> <ClickOverHint show={isOver && !isDragging} />
{ !isOver && upperFirst(name) !== label && ( { !isOver && name.toLowerCase() !== label.toLowerCase() && (
<div className={styles.info}> <div className={styles.info}>
{label} {label}
</div> </div>

View File

@ -18,8 +18,8 @@ import {
ON_CHANGE_SETTINGS, ON_CHANGE_SETTINGS,
ON_CLICK_ADD_ATTR, ON_CLICK_ADD_ATTR,
ON_REMOVE, ON_REMOVE,
ON_REMOVE_EDIT_VIEW_ATTR,
ON_REMOVE_EDIT_VIEW_FIELD_ATTR, ON_REMOVE_EDIT_VIEW_FIELD_ATTR,
ON_REMOVE_EDIT_VIEW_RELATION_ATTR,
ON_RESET, ON_RESET,
ON_SUBMIT, ON_SUBMIT,
SUBMIT_SUCCEEDED, SUBMIT_SUCCEEDED,
@ -122,17 +122,17 @@ export function onRemove(index, keys) {
}; };
} }
export function onRemoveEditViewAttr(index, keys) { export function onRemoveEditViewFieldAttr(index, keys) {
return { return {
type: ON_REMOVE_EDIT_VIEW_ATTR, type: ON_REMOVE_EDIT_VIEW_FIELD_ATTR,
index, index,
keys, keys,
}; };
} }
export function onRemoveEditViewFieldAttr(index, keys) { export function onRemoveEditViewRelationAttr(index, keys) {
return { return {
type: ON_REMOVE_EDIT_VIEW_FIELD_ATTR, type: ON_REMOVE_EDIT_VIEW_RELATION_ATTR,
index, index,
keys, keys,
}; };

View File

@ -16,8 +16,8 @@ export const ON_CHANGE = 'contentManager/App/ON_CHANGE';
export const ON_CHANGE_SETTINGS = 'contentManager/App/ON_CHANGE_SETTINGS'; export const ON_CHANGE_SETTINGS = 'contentManager/App/ON_CHANGE_SETTINGS';
export const ON_CLICK_ADD_ATTR = 'contentManager/App/ON_CLICK_ADD_ATTR'; export const ON_CLICK_ADD_ATTR = 'contentManager/App/ON_CLICK_ADD_ATTR';
export const ON_REMOVE = 'contentManager/App/ON_REMOVE'; export const ON_REMOVE = 'contentManager/App/ON_REMOVE';
export const ON_REMOVE_EDIT_VIEW_ATTR = 'contentManager/App/ON_REMOVE_EDIT_VIEW_ATTR';
export const ON_REMOVE_EDIT_VIEW_FIELD_ATTR = 'contentManager/App/ON_REMOVE_EDIT_VIEW_FIELD_ATTR'; export const ON_REMOVE_EDIT_VIEW_FIELD_ATTR = 'contentManager/App/ON_REMOVE_EDIT_VIEW_FIELD_ATTR';
export const ON_REMOVE_EDIT_VIEW_RELATION_ATTR = 'contentManager/App/ON_REMOVE_EDIT_VIEW_RELATION_ATTR';
export const ON_RESET = 'contentManager/App/ON_RESET'; export const ON_RESET = 'contentManager/App/ON_RESET';
export const ON_SUBMIT = 'contentManager/App/ON_SUBMIT'; export const ON_SUBMIT = 'contentManager/App/ON_SUBMIT';
export const SUBMIT_SUCCEEDED = 'contentManager/App/SUBMIT_SUCCEEDED'; export const SUBMIT_SUCCEEDED = 'contentManager/App/SUBMIT_SUCCEEDED';

View File

@ -5,7 +5,7 @@
*/ */
import { fromJS, List } from 'immutable'; import { fromJS, List } from 'immutable';
import { findIndex, get } from 'lodash'; import { findIndex, get, upperFirst } from 'lodash';
import Manager from 'utils/Manager'; import Manager from 'utils/Manager';
import { import {
EMPTY_STORE, EMPTY_STORE,
@ -19,7 +19,7 @@ import {
ON_CHANGE_SETTINGS, ON_CHANGE_SETTINGS,
ON_CLICK_ADD_ATTR, ON_CLICK_ADD_ATTR,
ON_REMOVE, ON_REMOVE,
ON_REMOVE_EDIT_VIEW_ATTR, ON_REMOVE_EDIT_VIEW_RELATION_ATTR,
ON_REMOVE_EDIT_VIEW_FIELD_ATTR, ON_REMOVE_EDIT_VIEW_FIELD_ATTR,
ON_RESET, ON_RESET,
SUBMIT_SUCCEEDED, SUBMIT_SUCCEEDED,
@ -118,10 +118,6 @@ function appReducer(state = initialState, action) {
.push(attrToAdd.get('0')); .push(attrToAdd.get('0'));
} }
return list.delete(action.index);
});
case ON_REMOVE_EDIT_VIEW_ATTR:
return state.updateIn(['modifiedSchema', 'models'].concat(action.keys.split('.')), list => {
return list.delete(action.index); return list.delete(action.index);
}); });
case ON_REMOVE_EDIT_VIEW_FIELD_ATTR: case ON_REMOVE_EDIT_VIEW_FIELD_ATTR:
@ -184,6 +180,19 @@ function appReducer(state = initialState, action) {
.insert(rightBoundIndex, `col-md-${attrToRemoveInfos.bootstrapCol}`); .insert(rightBoundIndex, `col-md-${attrToRemoveInfos.bootstrapCol}`);
} }
}); });
case ON_REMOVE_EDIT_VIEW_RELATION_ATTR: {
const relationName = state.getIn(['modifiedSchema', 'models', ...action.keys.split('.'), action.index]);
return state
.updateIn(['modifiedSchema', 'models', action.keys.split('.')[0], 'relations', relationName], relation => {
return relation
.update('description', () => '')
.update('label', () => upperFirst(relation.get('alias')));
})
.updateIn(['modifiedSchema', 'models'].concat(action.keys.split('.')), list => {
return list.delete(action.index);
});
}
case ON_RESET: case ON_RESET:
return state return state
.update('modifiedSchema', () => state.get('schema')); .update('modifiedSchema', () => state.get('schema'));

View File

@ -23,8 +23,8 @@ import {
onChangeSettings, onChangeSettings,
onClickAddAttr, onClickAddAttr,
onRemove, onRemove,
onRemoveEditViewAttr,
onRemoveEditViewFieldAttr, onRemoveEditViewFieldAttr,
onRemoveEditViewRelationAttr,
onReset, onReset,
onSubmit, onSubmit,
} from 'containers/App/actions'; } from 'containers/App/actions';
@ -140,6 +140,12 @@ class SettingPage extends React.PureComponent {
.join('.'); .join('.');
} }
getRelationLabel = (attrName) => {
const attrLabel = get(this.props.schema, ['models', ...this.getPath().split('.'), 'relations', attrName, 'label'], 'iii');
return attrLabel;
}
getRelations = () => { getRelations = () => {
const relations = get(this.props.schema, 'models.'.concat(this.getPath()).concat('.relations'), {}); const relations = get(this.props.schema, 'models.'.concat(this.getPath()).concat('.relations'), {});
@ -258,6 +264,23 @@ class SettingPage extends React.PureComponent {
this.props.onRemove(index, keys); this.props.onRemove(index, keys);
} }
handleRemoveRelation = (index, keys) => {
const { settingPage: { relationToEdit } } = this.props;
const relationToRemoveName = get(this.props.schema, ['models', ...keys.split('.'), index]);
const isRemovingSelectedItem = relationToRemoveName === relationToEdit.alias;
const displayedRelations = this.getEditPageDisplayedRelations();
const displayedFields = this.getEditPageDisplayedFields();
this.props.onRemoveEditViewRelationAttr(index, keys);
if (isRemovingSelectedItem && displayedRelations.length > 1) {
const nextIndex = index - 1 > -1 ? index - 1 : index + 1;
this.handleClickEditRelation(nextIndex);
} else if (displayedFields.length > 0) {
this.handleClickEditField(0);
} // Else add a field in the other drag and auto select it
}
handleReset = (e) => { handleReset = (e) => {
e.preventDefault(); e.preventDefault();
this.setState({ showWarningCancel: true }); this.setState({ showWarningCancel: true });
@ -348,10 +371,10 @@ class SettingPage extends React.PureComponent {
key={attr} key={attr}
keys={`${this.getPath()}.editDisplay.relations`} keys={`${this.getPath()}.editDisplay.relations`}
name={attr} name={attr}
label={attr} label={this.getRelationLabel(attr)}
moveAttr={this.props.moveAttrEditView} moveAttr={this.props.moveAttrEditView}
onClickEdit={this.handleClickEditRelation} onClickEdit={this.handleClickEditRelation}
onRemove={this.props.onRemoveEditViewAttr} onRemove={this.handleRemoveRelation}
updateSiblingHoverState={() => {}} updateSiblingHoverState={() => {}}
/> />
); );
@ -661,8 +684,8 @@ SettingPage.propTypes = {
onClickEditListItem: PropTypes.func.isRequired, onClickEditListItem: PropTypes.func.isRequired,
onClickEditRelation: PropTypes.func.isRequired, onClickEditRelation: PropTypes.func.isRequired,
onRemove: PropTypes.func.isRequired, onRemove: PropTypes.func.isRequired,
onRemoveEditViewAttr: PropTypes.func.isRequired,
onRemoveEditViewFieldAttr: PropTypes.func.isRequired, onRemoveEditViewFieldAttr: PropTypes.func.isRequired,
onRemoveEditViewRelationAttr: PropTypes.func.isRequired,
onReset: PropTypes.func.isRequired, onReset: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired,
schema: PropTypes.object.isRequired, schema: PropTypes.object.isRequired,
@ -682,8 +705,8 @@ const mapDispatchToProps = (dispatch) => (
onClickEditListItem, onClickEditListItem,
onClickEditRelation, onClickEditRelation,
onRemove, onRemove,
onRemoveEditViewAttr,
onRemoveEditViewFieldAttr, onRemoveEditViewFieldAttr,
onRemoveEditViewRelationAttr,
onReset, onReset,
onSubmit, onSubmit,
}, },

View File

@ -38,6 +38,10 @@
"via": "users", "via": "users",
"plugin": "users-permissions", "plugin": "users-permissions",
"configurable": false "configurable": false
},
"products": {
"collection": "product",
"via": "user"
} }
} }
} }