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(EG): Copy Edition name to Edition Group #842

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/client/entity-editor/name-section/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const UPDATE_NAME_FIELD = 'UPDATE_NAME_FIELD';
export const UPDATE_SORT_NAME_FIELD = 'UPDATE_SORT_NAME_FIELD';
export const UPDATE_WARN_IF_EXISTS = 'UPDATE_WARN_IF_EXISTS';
export const UPDATE_SEARCH_RESULTS = 'UPDATE_SEARCH_RESULTS';

export const UPDATE_COPY_NAME_TO_EDITION_GROUP = 'UPDATE_COPY_NAME_TO_EDITION_GROUP';
export type Action = {
type: string,
payload?: unknown,
Expand Down Expand Up @@ -203,3 +203,17 @@ export function searchName(
});
};
}

/**
* Produces an action indicating that the checkbox
* should be updated with the provided value.
*
* @param {boolean} isCheck - The new value to be used for the checkbox.
* @returns {Action} The resulting UPDATE_COPY_NAME_TO_EDITION_GROUP action.
*/
export function updateCopyNameCheckbox(isCheck:boolean): Action {
return {
payload: isCheck,
type: UPDATE_COPY_NAME_TO_EDITION_GROUP
};
}
19 changes: 18 additions & 1 deletion src/client/entity-editor/name-section/name-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import {Alert, Col, ListGroup, Row} from 'react-bootstrap';
import {Alert, Col, Form, ListGroup, Row} from 'react-bootstrap';
import {
checkIfNameExists,
debouncedUpdateDisambiguationField,
debouncedUpdateNameField,
debouncedUpdateSortNameField,
searchName,
updateCopyNameCheckbox,
updateLanguageField
} from './actions';
import {isAliasEmpty, isRequiredDisambiguationEmpty} from '../helpers';
Expand Down Expand Up @@ -63,6 +64,7 @@ import {getEntityDisambiguation} from '../../helpers/entity';
* @param {string} props.nameValue - The name currently set for this entity.
* @param {string} props.sortNameValue - The sort name currently set for this
* entity.
* @param {Function} props.onCheckboxToggle - A function to be called when toggle copy name to EG checkbox
* @param {Function} props.onLanguageChange - A function to be called when a
* different language type is selected.
* @param {Function} props.onNameChange - A function to be called when the name
Expand Down Expand Up @@ -129,13 +131,15 @@ class NameSection extends React.Component {

render() {
const {
action,
disambiguationDefaultValue,
entityType,
exactMatches,
languageOptions,
languageValue,
nameValue,
sortNameValue,
onCheckboxToggle,
onLanguageChange,
onSortNameChange,
onDisambiguationChange,
Expand Down Expand Up @@ -257,6 +261,17 @@ class NameSection extends React.Component {
onChange={onDisambiguationChange}
/>
</Col>
{action === 'edit' && entityType === 'edition' &&
<Col className="md-2" lg={{offset: 3, span: 6}}>
<Form.Check
className="margin-bottom-d8"
id="name"
label="Copy the Edition name to the Edition Group"
type="checkbox"
onChange={onCheckboxToggle}
/>
</Col>
}
</Row>
</div>
);
Expand All @@ -271,6 +286,7 @@ NameSection.propTypes = {
languageOptions: PropTypes.array.isRequired,
languageValue: PropTypes.number,
nameValue: PropTypes.string.isRequired,
onCheckboxToggle: PropTypes.func.isRequired,
onDisambiguationChange: PropTypes.func.isRequired,
onLanguageChange: PropTypes.func.isRequired,
onNameChange: PropTypes.func.isRequired,
Expand Down Expand Up @@ -314,6 +330,7 @@ function mapStateToProps(rootState) {
function mapDispatchToProps(dispatch, {entity, entityType}) {
const entityBBID = entity && entity.bbid;
return {
onCheckboxToggle: (event) => dispatch(updateCopyNameCheckbox(event.target.checked)),
onDisambiguationChange: (event) =>
dispatch(debouncedUpdateDisambiguationField(event.target.value)),
onLanguageChange: (value) =>
Expand Down
7 changes: 5 additions & 2 deletions src/client/entity-editor/name-section/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
*/

import {
UPDATE_DISAMBIGUATION_FIELD, UPDATE_LANGUAGE_FIELD, UPDATE_NAME_FIELD,
UPDATE_SEARCH_RESULTS, UPDATE_SORT_NAME_FIELD, UPDATE_WARN_IF_EXISTS
UPDATE_COPY_NAME_TO_EDITION_GROUP, UPDATE_DISAMBIGUATION_FIELD, UPDATE_LANGUAGE_FIELD,
UPDATE_NAME_FIELD, UPDATE_SEARCH_RESULTS, UPDATE_SORT_NAME_FIELD, UPDATE_WARN_IF_EXISTS
} from './actions';
import Immutable from 'immutable';


function reducer(
state = Immutable.Map({
copyNameToEditionGroup: false,
disambiguation: '',
exactMatches: [],
language: null,
Expand All @@ -48,6 +49,8 @@ function reducer(
return state.set('searchResults', payload);
case UPDATE_WARN_IF_EXISTS:
return state.set('exactMatches', payload);
case UPDATE_COPY_NAME_TO_EDITION_GROUP:
return state.set('copyNameToEditionGroup', payload);
// no default
}
return state;
Expand Down
3 changes: 2 additions & 1 deletion src/server/routes/entity/edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function transformNewForm(data) {
return {
aliases,
annotation: data.annotationSection.content,
copyNameToEditionGroup: data.nameSection.copyNameToEditionGroup ?? false,
depth: data.editionSection.depth &&
parseInt(data.editionSection.depth, 10),
disambiguation: data.nameSection.disambiguation,
Expand Down Expand Up @@ -296,7 +297,7 @@ router.param(
middleware.makeEntityLoader(
'Edition',
[
'editionGroup.defaultAlias',
'editionGroup.aliasSet.aliases',
'languageSet.languages',
'editionFormat',
'editionStatus',
Expand Down
31 changes: 31 additions & 0 deletions src/server/routes/entity/entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,31 @@ function sanitizeBody(body:any) {
return body;
}

/**
* @param {Object} body - form body
* @param {Object} currentEditionGroup - current Edition Group
* @param {Object} orm - orm
* @param {Object} transacting - bookshelf transaction
* @returns {Promise} - resolve to edition group modal
*/
async function copyNameToEditionGroup(body:Record<string, any>, currentEditionGroup:Record<string, any>, orm, transacting):Promise<any> {
const entityDefaultAlias = body.aliases.find((alias) => alias.default);
const tempAliases = currentEditionGroup.aliasSet.aliases.map((alias) => {
const mAlias = _.pick(alias, ['id', 'name', 'sortName', 'languageId', 'primary', 'default']);
if (alias.id === currentEditionGroup.aliasSet.defaultAliasId) {
return {...mAlias, default: true, name: entityDefaultAlias.name};
}
return mAlias;
});
const aliasSet = await getNextAliasSet(orm, transacting, currentEditionGroup, {aliases: tempAliases});
const EGEntity = await fetchOrCreateMainEntity(
orm, transacting, false, currentEditionGroup.bbid, 'EditionGroup'
);
EGEntity.set('aliasSetId', aliasSet.id);
EGEntity.shouldInsert = false;
return EGEntity;
}

export function handleCreateOrEditEntity(
req: PassportRequest,
res: $Response,
Expand All @@ -1020,6 +1045,7 @@ export function handleCreateOrEditEntity(
aliasSet: {id: number} | null | undefined,
annotation: {id: number} | null | undefined,
bbid: string,
editionGroup:any | undefined,
disambiguation: {id: number} | null | undefined,
identifierSet: {id: number} | null | undefined,
type: EntityTypeString
Expand Down Expand Up @@ -1085,6 +1111,11 @@ export function handleCreateOrEditEntity(
let allEntities = [...otherEntities, mainEntity]
.filter(entity => entity.get('dataId') !== null);

// copy name to the edition group
if (!isNew && entityType === 'Edition' && body.copyNameToEditionGroup) {
const EGEntity = await copyNameToEditionGroup(body, currentEntity.editionGroup, orm, transacting);
allEntities.push(EGEntity);
}
if (isMergeOperation) {
allEntities = await processMergeOperation(orm, transacting, req.session,
mainEntity, allEntities, relationshipSets);
Expand Down