104 lines
2.6 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 { get, map, size } from 'lodash';
2017-06-18 17:23:58 +02:00
2017-11-27 17:27:16 +01:00
// Components.
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
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) => {
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':
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}
/>
);
default:
break;
}
});
if (!relations.length) {
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,
2017-11-27 17:27:16 +01:00
location: PropTypes.shape({
search: PropTypes.string,
}).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;