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-09-12 14:31:39 +02:00
|
|
|
import { map } from 'lodash';
|
2017-06-18 17:23:58 +02:00
|
|
|
|
2017-09-07 17:16:31 +02:00
|
|
|
import SelectOne from 'components/SelectOne';
|
|
|
|
import SelectMany from 'components/SelectMany';
|
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
|
|
|
|
render() {
|
2017-09-12 14:31:39 +02:00
|
|
|
const relations = map(this.props.schema[this.props.currentModelName].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-09-12 14:31:39 +02:00
|
|
|
if (relation.dominant === true || relation.nature === 'manyToMany') {
|
|
|
|
return (
|
|
|
|
<SelectMany
|
|
|
|
currentModelName={this.props.currentModelName}
|
|
|
|
key={i}
|
|
|
|
record={this.props.record}
|
|
|
|
relation={relation}
|
|
|
|
schema={this.props.schema}
|
|
|
|
setRecordAttribute={this.props.setRecordAttribute}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2017-09-07 17:16:31 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!relations.length) {
|
|
|
|
if (this.props.isNull === false) {
|
|
|
|
this.props.toggleNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
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;
|