Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert relationship editor changes #679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 0 additions & 51 deletions src/client/entity-editor/relationship-editor/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* eslint-disable no-inline-comments */

import type {Relationship, Attribute as _Attribute} from './types';
import arrayMove from 'array-move';
import {sortRelationshipOrdinal} from '../../../common/helpers/utils';


export const SHOW_RELATIONSHIP_EDITOR = 'SHOW_RELATIONSHIP_EDITOR';
Expand All @@ -28,7 +25,6 @@ export const ADD_RELATIONSHIP = 'ADD_RELATIONSHIP';
export const EDIT_RELATIONSHIP = 'EDIT_RELATIONSHIP';
export const REMOVE_RELATIONSHIP = 'REMOVE_RELATIONSHIP';
export const UNDO_LAST_SAVE = 'UNDO_LAST_SAVE';
export const SORT_RELATIONSHIPS = 'SORT_RELATIONSHIPS';

export type Action = {
type: string,
Expand All @@ -55,53 +51,6 @@ export function addRelationship(data: Relationship): Action {
};
}

/**
* This action creator first sorts the relationship object and then pass the
* sorted object in the payload while dispatching the action.
*
* @param {number} oldIndex - Old Position of the relationship.
* @param {number} newIndex - New Position of the relationship.
* @returns {void}
*/
export function sortRelationships(oldIndex, newIndex):any {
return (dispatch, getState) => {
const state = getState();
const relationships = state.get('relationshipSection').get('relationships');
const orderTypeValue = state.get('seriesSection').get('orderType');
const relObject = relationships.toJS();
const relArray = Object.entries(relObject);
const automaticSort = [];
let automaticSortedArr: [string, Relationship][]; // stores the sorted array of relationships(sorting performed on number)

if (orderTypeValue === 1) { // OrderType 1 for Automatic Ordering
relArray.forEach((relationship:[string, Relationship]) => {
relationship[1].attributes.forEach((attribute:_Attribute) => {
if (attribute.attributeType === 2) { // Attribute Type 2 for number
automaticSort.push({number: attribute.value.textValue, relationship});
}
});
});
automaticSort.sort(sortRelationshipOrdinal('number')); // sorts the array of relationships on number attribute
automaticSortedArr = automaticSort.map(item => item.relationship);
}

// eslint-disable-next-line max-len
const sortedRelationships = orderTypeValue === 1 ? arrayMove(automaticSortedArr, oldIndex, newIndex) : arrayMove(relArray, oldIndex, newIndex);

sortedRelationships.forEach((relationship: [string, Relationship], index: number) => {
relationship[1].attributes.forEach((attribute: _Attribute) => {
if (attribute.attributeType === 1) { // Attribute type 1 for position
attribute.value.textValue = `${index}`; // assigns the position value to the sorted relationship array
}
});
});

const sortedRelationshipObject = Object.fromEntries(new Map([...sortedRelationships]));
const payload = sortedRelationshipObject;
dispatch({payload, type: SORT_RELATIONSHIPS});
};
}

export function editRelationship(rowID: number): Action {
return {
payload: rowID,
Expand Down
3 changes: 0 additions & 3 deletions src/client/entity-editor/relationship-editor/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
HIDE_RELATIONSHIP_EDITOR,
REMOVE_RELATIONSHIP,
SHOW_RELATIONSHIP_EDITOR,
SORT_RELATIONSHIPS,
UNDO_LAST_SAVE
} from './actions';

Expand All @@ -43,8 +42,6 @@ function reducer(
case SHOW_RELATIONSHIP_EDITOR:
return state.set('relationshipEditorVisible', true)
.set('relationshipEditorProps', null);
case SORT_RELATIONSHIPS:
return state.set('relationships', Immutable.fromJS(action.payload));
case HIDE_RELATIONSHIP_EDITOR:
return state.set('relationshipEditorVisible', false);
case ADD_RELATIONSHIP: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ import type {
RelationshipType,
RelationshipWithLabel,
Attribute as _Attribute,
Relationship as _Relationship,
setPosition as _setPosition
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 @@ -148,13 +145,11 @@ type EntitySearchResult = {
type RelationshipModalProps = {
relationshipTypes: Array<RelationshipType>,
baseEntity: Entity,
seriesType: string,
initRelationship: _Relationship | null | undefined,
languageOptions: Array<{label: string, value: number}>,
onCancel?: () => unknown,
onClose?: () => unknown,
onAdd?: (_Relationship) => unknown,
setPosition?: (_setPosition) => unknown
onAdd?: (_Relationship) => unknown
};


Expand All @@ -163,18 +158,14 @@ type RelationshipModalState = {
relationshipType?: RelationshipType | null | undefined,
relationship?: _Relationship | null | undefined,
targetEntity?: EntitySearchResult | null | undefined,
attributes?: _Attribute[],
attributePosition?: _Attribute,
attributeNumber?: _Attribute
attributes?: _Attribute[]
};

function getInitState(
baseEntity: Entity, initRelationship: _Relationship | null | undefined
): 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 @@ -203,8 +194,6 @@ 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 @@ -216,8 +205,6 @@ function getInitState(
};

return {
attributeNumber,
attributePosition,
attributeSetId: _.get(initRelationship, ['attributeSetId']),
attributes,
relationship: initRelationship,
Expand Down Expand Up @@ -280,33 +267,11 @@ 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, setPosition, baseEntity} = this.props;
const {onAdd} = this.props;
if (onAdd) {
if (this.state.relationship) {
const {relationship} = this.state;
relationship.attributes = setAttribute(this.state, this.state.relationshipType.attributeTypes);
onAdd(relationship);
if (baseEntity.type === 'Series') {
setPosition({newIndex: null, oldIndex: null});
}
onAdd(this.state.relationship);
}
}
};
Expand All @@ -333,15 +298,9 @@ class RelationshipModal
}

renderEntitySelect() {
const {baseEntity, relationshipTypes, seriesType} = this.props;
const {baseEntity, relationshipTypes} = this.props;
const {targetEntity} = this.state;
let types;
if (baseEntity.type === 'Series') {
types = [seriesType];
}
else {
types = getValidOtherEntityTypes(relationshipTypes, baseEntity);
}
const types = getValidOtherEntityTypes(relationshipTypes, baseEntity);
if (!types.length) {
return null;
}
Expand Down Expand Up @@ -397,11 +356,6 @@ class RelationshipModal
relationshipTypes, baseEntity, otherEntity
);

const attributeTypes = this.state.relationshipType ? this.state.relationshipType.attributeTypes : null;
let attributes = [];
if (attributeTypes) {
attributes = attributeTypes.map(attribute => attribute.name);
}
return (
<FormGroup>
<ControlLabel>Relationship</ControlLabel>
Expand All @@ -418,7 +372,6 @@ 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