2017-06-18 17:23:58 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* EditFormRelations
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-26 16:36:28 +02:00
|
|
|
import React from 'react';
|
2017-09-18 09:34:29 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2017-10-31 13:14:38 +01:00
|
|
|
import { get, map, size } from 'lodash';
|
2017-06-18 17:23:58 +02:00
|
|
|
|
2017-11-27 17:27:16 +01:00
|
|
|
// Components.
|
2017-09-07 17:16:31 +02:00
|
|
|
import SelectOne from 'components/SelectOne';
|
|
|
|
import SelectMany from 'components/SelectMany';
|
2017-11-27 17:27:16 +01:00
|
|
|
|
|
|
|
// Utils.
|
|
|
|
import getQueryParameters from 'utils/getQueryParameters';
|
|
|
|
|
|
|
|
// Style.
|
2017-06-18 17:23:58 +02:00
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
|
|
class EditFormRelations extends React.Component { // eslint-disable-line react/prefer-stateless-function
|
2017-10-31 13:14:38 +01:00
|
|
|
componentDidMount() {
|
|
|
|
if (size(get(this.props.schema, [this.props.currentModelName, 'relations'])) === 0 && !this.props.isNull) {
|
|
|
|
this.props.toggleNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-18 17:23:58 +02:00
|
|
|
render() {
|
2017-11-27 17:27:16 +01:00
|
|
|
const source = getQueryParameters(this.props.location.search, 'source');
|
|
|
|
const currentSchema = get(this.props.schema, [this.props.currentModelName]) || get(this.props.schema, ['plugins', source, this.props.currentModelName]);
|
|
|
|
|
|
|
|
const relations = map(currentSchema.relations, (relation, i) => {
|
2017-09-22 16:56:09 +02:00
|
|
|
|
2017-09-07 17:16:31 +02:00
|
|
|
switch (relation.nature) {
|
|
|
|
case 'oneToOne':
|
2017-10-23 17:07:57 +02:00
|
|
|
case 'manyToOne':
|
2017-09-12 14:31:39 +02:00
|
|
|
if (relation.dominant) {
|
|
|
|
return (
|
|
|
|
<SelectOne
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
key={i}
|
|
|
|
record={this.props.record}
|
|
|
|
relation={relation}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2017-10-23 17:07:57 +02:00
|
|
|
case 'oneToMany':
|
2017-09-07 17:16:31 +02:00
|
|
|
case 'manyToMany':
|
2017-10-23 17:35:00 +02:00
|
|
|
return (
|
|
|
|
<SelectMany
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
key={i}
|
|
|
|
record={this.props.record}
|
|
|
|
relation={relation}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
|
|
|
/>
|
|
|
|
);
|
2017-09-07 17:16:31 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!relations.length) {
|
|
|
|
return (null);
|
|
|
|
}
|
2017-06-18 17:23:58 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.editFormRelations}>
|
2017-09-07 17:16:31 +02:00
|
|
|
<h3>Relational data</h3>
|
2017-06-18 17:23:58 +02:00
|
|
|
{relations}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EditFormRelations.propTypes = {
|
2017-09-18 09:34:29 +02:00
|
|
|
currentModelName: PropTypes.oneOfType([
|
|
|
|
PropTypes.bool,
|
|
|
|
PropTypes.string,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
isNull: PropTypes.bool.isRequired,
|
2017-11-27 17:27:16 +01:00
|
|
|
location: PropTypes.shape({
|
|
|
|
search: PropTypes.string,
|
|
|
|
}).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
record: PropTypes.oneOfType([
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
schema: PropTypes.oneOfType([
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.bool,
|
2017-08-18 17:02:33 +02:00
|
|
|
]).isRequired,
|
2017-09-18 09:34:29 +02:00
|
|
|
setRecordAttribute: PropTypes.func.isRequired,
|
|
|
|
toggleNull: PropTypes.func.isRequired,
|
2017-06-18 17:23:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default EditFormRelations;
|