93 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-06-18 17:23:58 +02:00
/**
*
* EditFormRelations
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { map } from 'lodash';
2017-06-18 17:23:58 +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() {
const relations = map(this.props.schema[this.props.currentModelName].relations, (relation, i) => {
switch (relation.nature) {
case 'oneToOne':
case 'manyToOne':
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;
case 'oneToMany':
case 'manyToMany':
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;
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}>
<h3>Relational data</h3>
2017-06-18 17:23:58 +02:00
{relations}
</div>
);
}
}
EditFormRelations.propTypes = {
currentModelName: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
2017-08-18 17:02:33 +02:00
]).isRequired,
isNull: PropTypes.bool.isRequired,
record: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool,
2017-08-18 17:02:33 +02:00
]).isRequired,
schema: PropTypes.oneOfType([
PropTypes.object,
PropTypes.bool,
2017-08-18 17:02:33 +02:00
]).isRequired,
setRecordAttribute: PropTypes.func.isRequired,
toggleNull: PropTypes.func.isRequired,
2017-06-18 17:23:58 +02:00
};
export default EditFormRelations;