Handle add relation in dropdown

This commit is contained in:
soupette 2018-07-16 16:11:16 +02:00
parent 2a753fbb92
commit 8c31a060f7
5 changed files with 83 additions and 27 deletions

View File

@ -17,6 +17,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_RESET, ON_RESET,
ON_SUBMIT, ON_SUBMIT,
SUBMIT_SUCCEEDED, SUBMIT_SUCCEEDED,
@ -110,6 +111,14 @@ export function onRemove(index, keys) {
}; };
} }
export function onRemoveEditViewAttr(index, keys) {
return {
type: ON_REMOVE_EDIT_VIEW_ATTR,
index,
keys,
};
}
export function onReset() { export function onReset() {
return { return {
type: ON_RESET, type: ON_RESET,

View File

@ -15,6 +15,7 @@ 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_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

@ -16,6 +16,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_RESET, ON_RESET,
SUBMIT_SUCCEEDED, SUBMIT_SUCCEEDED,
} from './constants'; } from './constants';
@ -87,7 +88,7 @@ function appReducer(state = initialState, action) {
.updateIn(['modifiedSchema', 'models'].concat(action.keys), () => action.value); .updateIn(['modifiedSchema', 'models'].concat(action.keys), () => action.value);
case ON_CLICK_ADD_ATTR: case ON_CLICK_ADD_ATTR:
return state return state
.updateIn(['modifiedSchema', 'models'].concat(action.keys.split('.')).concat(['listDisplay']), list => list.push(fromJS(action.data))); .updateIn(['modifiedSchema', 'models'].concat(action.keys.split('.')), list => list.push(fromJS(action.data)));
case ON_REMOVE: case ON_REMOVE:
return state.updateIn(['modifiedSchema', 'models'].concat(action.keys.split('.')).concat(['listDisplay']), list => { return state.updateIn(['modifiedSchema', 'models'].concat(action.keys.split('.')).concat(['listDisplay']), list => {
@ -105,6 +106,10 @@ 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_RESET: case ON_RESET:

View File

@ -22,6 +22,7 @@ import {
onChangeSettings, onChangeSettings,
onClickAddAttr, onClickAddAttr,
onRemove, onRemove,
onRemoveEditViewAttr,
onReset, onReset,
onSubmit, onSubmit,
} from 'containers/App/actions'; } from 'containers/App/actions';
@ -50,6 +51,7 @@ class SettingPage extends React.PureComponent {
state = { state = {
isDraggingSibling: false, isDraggingSibling: false,
isOpen: false, isOpen: false,
isOpenRelation: false,
showWarning: false, showWarning: false,
showWarningCancel: false, showWarningCancel: false,
}; };
@ -89,13 +91,22 @@ class SettingPage extends React.PureComponent {
}); });
} }
getDropDownRelationsItems = () => {
const currentDisplayedRelations = this.getEditPageDisplayedRelations();
return this.getRelations()
.filter(relation => {
return currentDisplayedRelations.indexOf(relation) === -1;
});
}
getEditPageDisplaySettings = () => { getEditPageDisplaySettings = () => {
return get(this.props.schema, 'models.'.concat(this.getPath().concat('.editDisplay')), { fields: [], relations: [] }); return get(this.props.schema, 'models.'.concat(this.getPath().concat('.editDisplay')), { fields: [], relations: [] });
} }
getEditPageFields = () => get(this.getEditPageDisplaySettings(), ['fields'], []); getEditPageFields = () => get(this.getEditPageDisplaySettings(), ['fields'], []);
getEditPageRelations = () => get(this.getEditPageDisplaySettings(), ['relations'], []); getEditPageDisplayedRelations = () => get(this.getEditPageDisplaySettings(), ['relations'], []);
getListDisplay = () => ( getListDisplay = () => (
get(this.props.schema, `models.${this.getPath()}.listDisplay`, []) get(this.props.schema, `models.${this.getPath()}.listDisplay`, [])
@ -115,6 +126,18 @@ class SettingPage extends React.PureComponent {
.join('.'); .join('.');
} }
getRelations = () => {
const relations = get(this.props.schema, 'models.'.concat(this.getPath()).concat('.relations'), {});
return Object.keys(relations)
.filter(relation => {
const isUploadRelation = get(relations, [relation, 'plugin'], '') === 'upload';
const isMorphSide = get(relations, [relation, 'nature'], '').toLowerCase().includes('morph') && get(relations, [relation, relation]) !== undefined;
return !isUploadRelation && !isMorphSide;
});
}
getSelectOptions = (input) => { getSelectOptions = (input) => {
const selectOptions = this.getListDisplay().reduce((acc, curr) => { const selectOptions = this.getListDisplay().reduce((acc, curr) => {
@ -217,16 +240,7 @@ class SettingPage extends React.PureComponent {
} }
hasRelations = () => { hasRelations = () => {
const relations = get(this.props.schema, 'models.'.concat(this.getPath()).concat('.relations'), {}); return this.getRelations().length > 0;
return Object.keys(relations)
.filter(relation => {
const isUploadRelation = get(relations, [relation, 'plugin'], '') === 'upload';
const isMorphSide = get(relations, [relation, 'nature'], '').toLowerCase().includes('morph') && get(relations, [relation, relation]) !== undefined;
return !isUploadRelation && !isMorphSide;
})
.length > 0;
} }
// We need to remove the Over state on the DraggableAttr component // We need to remove the Over state on the DraggableAttr component
@ -244,13 +258,20 @@ class SettingPage extends React.PureComponent {
} }
} }
toggleDropdownRelations = () => {
if (this.getDropDownRelationsItems().length > 0) {
this.setState(prevState => ({ isOpenRelation: !prevState.isOpenRelation }));
}
}
render() { render() {
const { isDraggingSibling, isOpen, showWarning, showWarningCancel } = this.state; const { isDraggingSibling, isOpen, isOpenRelation, showWarning, showWarningCancel } = this.state;
const { const {
moveAttr, moveAttr,
moveAttrEditView, moveAttrEditView,
onChangeSettings, onChangeSettings,
onClickAddAttr, onClickAddAttr,
onRemoveEditViewAttr,
onReset, onReset,
onSubmit, onSubmit,
} = this.props; } = this.props;
@ -365,22 +386,12 @@ class SettingPage extends React.PureComponent {
</FormattedMessage> </FormattedMessage>
</DropdownToggle> </DropdownToggle>
<DropdownMenu> <DropdownMenu>
{this.getDropDownItems().map((item, i) => { {this.getDropDownItems().map((item) => {
if (i !== this.getDropDownItems().length - 1 && this.getDropDownItems().length > 0) {
return (
<React.Fragment key={item.name}>
<DropdownItem onClick={() => onClickAddAttr(item, namePath)}>
{item.label}
</DropdownItem>
<DropdownItem divider />
</React.Fragment>
);
}
return ( return (
<DropdownItem <DropdownItem
key={item.name} key={item.name}
onClick={() => onClickAddAttr(item, namePath)} onClick={() => onClickAddAttr(item, `${namePath}.listDisplay`)}
> >
{item.label} {item.label}
</DropdownItem> </DropdownItem>
@ -448,7 +459,7 @@ class SettingPage extends React.PureComponent {
<FormattedMessage id="content-manager.containers.SettingPage.relations" /> <FormattedMessage id="content-manager.containers.SettingPage.relations" />
<div className={cn(styles.sort_wrapper, 'col-md-12')}> <div className={cn(styles.sort_wrapper, 'col-md-12')}>
<div className="row"> <div className="row">
{this.getEditPageRelations().map((attr, index) => { {this.getEditPageDisplayedRelations().map((attr, index) => {
return ( return (
<DraggableAttr <DraggableAttr
index={index} index={index}
@ -460,11 +471,33 @@ class SettingPage extends React.PureComponent {
label={attr} label={attr}
moveAttr={moveAttrEditView} moveAttr={moveAttrEditView}
onClickEditListItem={() => {}} onClickEditListItem={() => {}}
onRemove={() => {}} onRemove={onRemoveEditViewAttr}
updateSiblingHoverState={() => {}} updateSiblingHoverState={() => {}}
/> />
); );
})} })}
<div className={cn(styles.dropdownRelations, styles.dropdownWrapper)}>
<ButtonDropdown isOpen={isOpenRelation} toggle={this.toggleDropdownRelations}>
<DropdownToggle>
<FormattedMessage id="content-manager.containers.SettingPage.addField">
{msg => <p>{msg}</p>}
</FormattedMessage>
</DropdownToggle>
<DropdownMenu>
{this.getDropDownRelationsItems().map((item) => {
return (
<DropdownItem
key={item}
onClick={() => onClickAddAttr(item, `${namePath}.editDisplay.relations`)}
>
{item}
</DropdownItem>
);
})}
</DropdownMenu>
</ButtonDropdown>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -490,6 +523,7 @@ SettingPage.propTypes = {
onClickAddAttr: PropTypes.func.isRequired, onClickAddAttr: PropTypes.func.isRequired,
onClickEditListItem: PropTypes.func.isRequired, onClickEditListItem: PropTypes.func.isRequired,
onRemove: PropTypes.func.isRequired, onRemove: PropTypes.func.isRequired,
onRemoveEditViewAttr: 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,
@ -506,6 +540,7 @@ const mapDispatchToProps = (dispatch) => (
onClickAddAttr, onClickAddAttr,
onClickEditListItem, onClickEditListItem,
onRemove, onRemove,
onRemoveEditViewAttr,
onReset, onReset,
onSubmit, onSubmit,
}, },

View File

@ -138,4 +138,10 @@
> div { > div {
padding: 0 10px; padding: 0 10px;
} }
}
.dropdownRelations {
width: 100%;
margin-left: 0 !important;
margin-bottom: 10px;
} }