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

feat(relationships): add initial relationship when entity created via another entity. #BB-266 #247

Merged
merged 3 commits into from Jan 31, 2019
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
12 changes: 10 additions & 2 deletions src/client/components/pages/entities/creator.js
Expand Up @@ -23,15 +23,15 @@ import EntityFooter from './footer';
import EntityImage from './image';
import EntityLinks from './links';
import EntityTitle from './title';
import Icon from 'react-fontawesome';
import PropTypes from 'prop-types';
import React from 'react';
import {labelsForCreator} from '../../../helpers/utils';


const {extractAttribute, getTypeAttribute, getEntityUrl,
ENTITY_TYPE_ICONS, getSortNameOfDefaultAlias} = entityHelper;
const {Col, Row} = bootstrap;

const {Button, Col, Row} = bootstrap;

function CreatorAttributes({creator}) {
const type = getTypeAttribute(creator.creatorType).data;
Expand Down Expand Up @@ -121,6 +121,14 @@ function CreatorDisplayPage({entity, identifierTypes}) {
identifierTypes={identifierTypes}
urlPrefix={urlPrefix}
/>
<Button
bsStyle="success"
className="margin-top-d15"
href={`/work/create?${
entity.type.toLowerCase()}=${entity.bbid}`}
>
<Icon className="margin-right-0-5" name="plus"/>Add Work
</Button>
<hr className="margin-top-d40"/>
<EntityFooter
entityUrl={urlPrefix}
Expand Down
46 changes: 46 additions & 0 deletions src/server/helpers/entityRouteUtils.js
Expand Up @@ -185,3 +185,49 @@ export function makeEntityCreateOrEditHandler(
);
};
}

/**
* add an initial relationship to entity from another enitty
* when one entity created from other.
* @param {object} props - props related to new entity
* @param {number} relationshipTypeId - relationshipId number for initaial relationship
* @param {object} targetEntity - details about target entitiy like publication, publisher and creator
* @param {number} relationshipIndex - initial relationship index number
*/

export function addInitialRelationship(props, relationshipTypeId, relationshipIndex, targetEntity) {
// Prepend 'i' here to indicate initail relationship row identifier
const rowId = `i${relationshipIndex || 0}`;
const relationship = props.relationshipTypes.find(
relationshipType => relationshipType.id === relationshipTypeId
);
const targetEntityDetail = {
bbid: targetEntity.id,
defaultAlias: {name: targetEntity.text},
type: targetEntity.type
};

const sourceEntityDetail = {
defaultAlias: {name: ''},
type: _.upperFirst(props.entityType)
};

const initialRelationship = {
label: relationship.linkPhrase,
relationshipType: relationship,
rowID: rowId,
sourceEntity: targetEntity.type === 'Publication' ? sourceEntityDetail : targetEntityDetail,
targetEntity: targetEntity.type === 'Publication' ? targetEntityDetail : sourceEntityDetail
};

if (!props.initialState.relationshipSection) {
props.initialState.relationshipSection = {};
props.initialState.relationshipSection.lastRelationships = null;
props.initialState.relationshipSection.relationshipEditorProps = null;
props.initialState.relationshipSection.relationshipEditorVisible = false;
}
props.initialState.relationshipSection.relationships =
{...props.initialState.relationshipSection.relationships, [rowId]: initialRelationship};

return props;
}
12 changes: 11 additions & 1 deletion src/server/routes/entity/edition.js
Expand Up @@ -23,6 +23,7 @@ import * as middleware from '../../helpers/middleware';
import * as utils from '../../helpers/utils';

import {
addInitialRelationship,
entityEditorMarkup,
generateEntityProps,
makeEntityCreateOrEditHandler
Expand Down Expand Up @@ -128,17 +129,26 @@ router.get(

function render(props) {
const {initialState} = props;

let relationshipTypeId;
let initialRelationshipIndex;
if (props.publisher || props.publication) {
initialState.editionSection = {};
}

if (props.publisher) {
initialState.editionSection.publisher = props.publisher;
// add initial relationship with relationshipTypeId = 4 (<Publisher> published < New Edition>)
relationshipTypeId = 4;
initialRelationshipIndex = 0;
addInitialRelationship(props, relationshipTypeId, initialRelationshipIndex, props.publisher);
}

if (props.publication) {
initialState.editionSection.publication = props.publication;
// add initial raltionship with relationshipTypeId = 3 (<New Edition> is an edition of <Publication>)
relationshipTypeId = 3;
initialRelationshipIndex = 1;
addInitialRelationship(props, relationshipTypeId, initialRelationshipIndex, props.publication);
}

const editorMarkup = entityEditorMarkup(props);
Expand Down
58 changes: 49 additions & 9 deletions src/server/routes/entity/work.js
Expand Up @@ -22,10 +22,13 @@ import * as entityRoutes from './entity';
import * as middleware from '../../helpers/middleware';
import * as utils from '../../helpers/utils';
import {
addInitialRelationship,
entityEditorMarkup,
generateEntityProps,
makeEntityCreateOrEditHandler
} from '../../helpers/entityRouteUtils';

import Promise from 'bluebird';
import _ from 'lodash';
import {escapeProps} from '../../helpers/props';
import express from 'express';
Expand Down Expand Up @@ -79,23 +82,60 @@ router.get('/:bbid/revisions', (req, res, next) => {
entityRoutes.displayRevisions(req, res, next, WorkRevision);
});

function entityToOption(entity) {
return {
disambiguation: entity.disambiguation ?
entity.disambiguation.comment : null,
id: entity.bbid,
text: entity.defaultAlias ?
entity.defaultAlias.name : '(unnamed)',
type: entity.type
};
}

// Creation

router.get(
'/create', auth.isAuthenticated, middleware.loadIdentifierTypes,
middleware.loadLanguages, middleware.loadWorkTypes,
middleware.loadRelationshipTypes,
(req, res) => {
const {markup, props} = entityEditorMarkup(generateEntityProps(
(req, res, next) => {
const {Creator} = req.app.locals.orm;
let relationshipTypeId;
let initialRelationshipIndex;
const propsPromise = generateEntityProps(
'work', req, res, {}
));
);

return res.send(target({
markup,
props: escapeProps(props),
script: '/js/entity-editor.js',
title: 'Add Work'
}));
if (req.query.creator) {
propsPromise.creator =
Creator.forge({bbid: req.query.creator})
.fetch({withRelated: 'defaultAlias'})
.then((data) => entityToOption(data.toJSON()));
}

function render(props) {
if (props.creator) {
// add initial ralationship with relationshipTypeId = 8 (<Author> wrote <Work>)
relationshipTypeId = 8;
initialRelationshipIndex = 0;
addInitialRelationship(props, relationshipTypeId, initialRelationshipIndex, props.creator);
}

const editorMarkup = entityEditorMarkup(props);
const {markup} = editorMarkup;
const updatedProps = editorMarkup.props;

return res.send(target({
markup,
props: escapeProps(updatedProps),
script: '/js/entity-editor.js',
title: 'Add Work'
}));
}
Promise.props(propsPromise)
.then(render)
.catch(next);
}
);

Expand Down