Apply sort for relations in edit view

This commit is contained in:
soupette 2018-07-16 17:26:51 +02:00
parent ecb9f306e3
commit 17a39af8ef
3 changed files with 27 additions and 19 deletions

View File

@ -7,7 +7,7 @@
import React from 'react'; import React from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { get, map } from 'lodash'; import { get } from 'lodash';
// Components. // Components.
import SelectOne from 'components/SelectOne'; import SelectOne from 'components/SelectOne';
@ -15,29 +15,20 @@ import SelectMany from 'components/SelectMany';
import styles from './styles.scss'; import styles from './styles.scss';
const filterRelationsUpload = (data) => Object.keys(data).reduce((acc, current) => {
if (get(data, [current, 'plugin']) !== 'upload') {
acc[current] = data[current];
}
return acc;
}, {});
function EditRelations(props) { function EditRelations(props) {
return ( return (
<div className={styles.editFormRelations}> <div className={styles.editFormRelations}>
<FormattedMessage id="content-manager.EditRelations.title"> <FormattedMessage id="content-manager.EditRelations.title">
{(message) => <h3>{message}</h3>} {(message) => <h3>{message}</h3>}
</FormattedMessage> </FormattedMessage>
{map(filterRelationsUpload(props.schema.relations), (relation, key) => { {props.displayedRelations.map(relationName => {
if (relation.nature.toLowerCase().includes('morph') && relation[key]) return ''; const relation = get(props.schema, ['relations', relationName], {});
const Select = ['oneWay', 'oneToOne', 'manyToOne', 'oneToManyMorph', 'oneToOneMorph'].includes(relation.nature) ? SelectOne : SelectMany; const Select = ['oneWay', 'oneToOne', 'manyToOne', 'oneToManyMorph', 'oneToOneMorph'].includes(relation.nature) ? SelectOne : SelectMany;
return ( return (
<Select <Select
currentModelName={props.currentModelName} currentModelName={props.currentModelName}
key={key} key={relationName}
record={props.record} record={props.record}
relation={relation} relation={relation}
schema={props.schema} schema={props.schema}
@ -51,6 +42,7 @@ function EditRelations(props) {
} }
EditRelations.defaultProps = { EditRelations.defaultProps = {
displayedRelations: [],
record: {}, record: {},
schema: {}, schema: {},
}; };
@ -58,6 +50,7 @@ EditRelations.defaultProps = {
EditRelations.propTypes = { EditRelations.propTypes = {
changeData: PropTypes.func.isRequired, changeData: PropTypes.func.isRequired,
currentModelName: PropTypes.string.isRequired, currentModelName: PropTypes.string.isRequired,
displayedRelations: PropTypes.array,
location: PropTypes.object.isRequired, location: PropTypes.object.isRequired,
record: PropTypes.object, record: PropTypes.object,
schema: PropTypes.object, schema: PropTypes.object,

View File

@ -129,6 +129,7 @@ class SelectMany extends React.Component {
); );
const value = get(this.props.record, this.props.relation.alias); const value = get(this.props.record, this.props.relation.alias);
/* eslint-disable jsx-a11y/label-has-for */ /* eslint-disable jsx-a11y/label-has-for */
return ( return (
<div className={`form-group ${styles.selectMany}`}> <div className={`form-group ${styles.selectMany}`}>
@ -147,6 +148,7 @@ class SelectMany extends React.Component {
isNull(value) || isUndefined(value) || value.size === 0 isNull(value) || isUndefined(value) || value.size === 0
? null ? null
: value.map(item => { : value.map(item => {
if (item) { if (item) {
return { return {
value: get(item, 'value') || item, value: get(item, 'value') || item,
@ -154,7 +156,7 @@ class SelectMany extends React.Component {
get(item, 'label') || get(item, 'label') ||
templateObject({ mainField: this.props.relation.displayedAttribute }, item) templateObject({ mainField: this.props.relation.displayedAttribute }, item)
.mainField || .mainField ||
item.value.id, item.id,
}; };
} }
}) })

View File

@ -108,8 +108,16 @@ export class EditPage extends React.Component {
} }
/** /**
* Retrive the model's custom layout * Retrieve the model's displayed relations
* @return {[type]} [description] * @return {Array}
*/
getDisplayedRelations = () => {
return get(this.getSchema(), ['editDisplay', 'relations'], []);
}
/**
* Retrieve the model's custom layout
*
*/ */
getLayout = () => ( getLayout = () => (
bindLayout.call(this, get(this.props.editPage, ['layout', this.getModelName()], {})) bindLayout.call(this, get(this.props.editPage, ['layout', this.getModelName()], {}))
@ -223,6 +231,10 @@ export class EditPage extends React.Component {
this.props.setFormErrors(formErrors); this.props.setFormErrors(formErrors);
} }
hasDisplayedRelations = () => {
return this.getDisplayedRelations().length > 0;
}
isCreating = () => this.props.match.params.id === 'create'; isCreating = () => this.props.match.params.id === 'create';
isRelationComponentNull = () => ( isRelationComponentNull = () => (
@ -299,7 +311,7 @@ export class EditPage extends React.Component {
}} }}
/> />
<div className="row"> <div className="row">
<div className={this.isRelationComponentNull() ? 'col-lg-12' : 'col-lg-9'}> <div className={!this.hasDisplayedRelations() ? 'col-lg-12' : 'col-lg-9'}>
<div className={styles.main_wrapper}> <div className={styles.main_wrapper}>
{this.showLoaders() ? ( {this.showLoaders() ? (
<LoadingIndicator /> <LoadingIndicator />
@ -320,12 +332,13 @@ export class EditPage extends React.Component {
)} )}
</div> </div>
</div> </div>
{!this.isRelationComponentNull() && ( {this.hasDisplayedRelations() && (
<div className={cn('col-lg-3')}> <div className={cn('col-lg-3')}>
<div className={styles.sub_wrapper}> <div className={styles.sub_wrapper}>
{!this.isRelationComponentNull() && ( {this.hasDisplayedRelations() && (
<EditRelations <EditRelations
currentModelName={this.getModelName()} currentModelName={this.getModelName()}
displayedRelations={this.getDisplayedRelations()}
location={this.props.location} location={this.props.location}
changeData={this.props.changeData} changeData={this.props.changeData}
record={editPage.record} record={editPage.record}