Skip to content

Commit

Permalink
Merge 964b9c0 into fd718f5
Browse files Browse the repository at this point in the history
  • Loading branch information
akashgp09 committed Aug 4, 2021
2 parents fd718f5 + 964b9c0 commit b5e6b3e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ import type {
Relationship as _Relationship
} from './types';
import {faExternalLinkAlt, faPlus, faTimes} from '@fortawesome/free-solid-svg-icons';
import {getInitAttribute, setAttribute} from './helper';

import EntitySearchFieldOption from '../common/entity-search-field-option';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import {NumberAttribute} from './attributes';
import ReactSelect from 'react-select';
import Relationship from './relationship';
import _ from 'lodash';
Expand Down Expand Up @@ -158,6 +160,8 @@ type RelationshipModalState = {
relationshipType?: RelationshipType | null | undefined,
relationship?: _Relationship | null | undefined,
targetEntity?: EntitySearchResult | null | undefined,
attributePosition?: _Attribute,
attributeNumber?: _Attribute
attributes?: _Attribute[]
};

Expand All @@ -166,6 +170,8 @@ function getInitState(
): RelationshipModalState {
if (_.isNull(initRelationship)) {
return {
attributeNumber: {attributeType: 2, value: {textValue: null}},
attributePosition: {attributeType: 1, value: {textValue: null}},
attributeSetId: null,
attributes: [],
relationship: null,
Expand Down Expand Up @@ -194,6 +200,8 @@ function getInitState(
}
}
const attributes = _.get(initRelationship, ['attributes']);
const attributePosition = getInitAttribute(attributes, 1);
const attributeNumber = getInitAttribute(attributes, 2);

const searchFormatOtherEntity = otherEntity && {
id: _.get(otherEntity, ['bbid']),
Expand All @@ -205,6 +213,8 @@ function getInitState(
};

return {
attributeNumber,
attributePosition,
attributeSetId: _.get(initRelationship, ['attributeSetId']),
attributes,
relationship: initRelationship,
Expand Down Expand Up @@ -267,11 +277,31 @@ class RelationshipModal
});
};

handleNumberAttributeChange = ({target}) => {
const value = target.value === '' ? null : target.value;
const attributeNumber = {
attributeType: 2,
value: {textValue: value}
};
const attributePosition = {
attributeType: 1,
value: {textValue: null}
};
this.setState({
attributeNumber,
attributePosition
});
};

handleAdd = () => {
const {onAdd} = this.props;
if (onAdd) {
if (this.state.relationship) {
onAdd(this.state.relationship);
const {relationship} = this.state;
// Before adding a relationship, set all the attributes state value
// (ex: number, position etc) to attributes property of the relationship object.
relationship.attributes = setAttribute(this.state, this.state.relationshipType.attributeTypes);
onAdd(relationship);
}
}
};
Expand Down Expand Up @@ -356,6 +386,13 @@ class RelationshipModal
relationshipTypes, baseEntity, otherEntity
);

// The attribute types belonging to the relationship type
const attributeTypes = this.state.relationshipType ? this.state.relationshipType.attributeTypes : null;
let attributes = [];
if (attributeTypes) {
// Name of the attribute type belonging to the relationship type. EX: ['position', 'number]
attributes = attributeTypes.map(attribute => attribute.name);
}
return (
<FormGroup>
<ControlLabel>Relationship</ControlLabel>
Expand All @@ -372,6 +409,14 @@ class RelationshipModal
{this.state.relationshipType &&
<HelpBlock>{this.state.relationshipType.description}</HelpBlock>
}
{
attributes.includes('number') ?
<NumberAttribute
value={this.state.attributeNumber.value.textValue}
onHandleChange={this.handleNumberAttributeChange}
/> :
null
}
</FormGroup>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ export function RelationshipList(
/* eslint-disable react/jsx-no-bind */
const renderedRelationships = _.map(
relationships,
({relationshipType, sourceEntity, targetEntity}, rowID) => (
({relationshipType, sourceEntity, targetEntity, attributes}, rowID) => (
<Row className="margin-top-d5" key={rowID}>
<Col md={onEdit || onRemove ? 8 : 12}>
<Relationship
link
showAttributes
attributes={attributes}
contextEntity={contextEntity}
relationshipType={relationshipType}
sourceEntity={sourceEntity}
Expand Down

0 comments on commit b5e6b3e

Please sign in to comment.