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(edition): Allow multiple publishers for a single edition #848

Merged
merged 5 commits into from Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -130,6 +130,10 @@ class EntitySearchFieldOption extends React.Component {
return option.text;
}

getOptionValue(option) {
return option.id;
}

render() {
const labelElement = <ValidationLabel empty={this.props.empty} error={this.props.error}>{this.props.label}</ValidationLabel>;
const helpIconElement = this.props.tooltipText && (
Expand All @@ -147,7 +151,7 @@ class EntitySearchFieldOption extends React.Component {
components={{Option: LinkedEntitySelect, SingleValue: EntitySelect}}
filterOptions={false}
getOptionLabel={this.getOptionLabel}
labelKey="text"
getOptionValue={this.getOptionValue}
loadOptions={this.fetchOptions}
onBlurResetsInput={false}
{...this.props}
Expand Down
2 changes: 1 addition & 1 deletion src/client/entity-editor/edition-section/actions.ts
Expand Up @@ -164,7 +164,7 @@ export function toggleShowEditionGroup(showEGSection: boolean): Action {
* the edition.
* @returns {Action} The resulting UPDATE_PUBLISHER action.
*/
export function updatePublisher(newPublisher: Publisher): Action {
export function updatePublisher(newPublisher: Record<string, Publisher>): Action {
return {
payload: newPublisher,
type: UPDATE_PUBLISHER
Expand Down
13 changes: 7 additions & 6 deletions src/client/entity-editor/edition-section/edition-section.tsx
Expand Up @@ -37,7 +37,7 @@ import {
} from './actions';

import {Alert, Button, Col, Form, ListGroup, OverlayTrigger, Row, Tooltip} from 'react-bootstrap';
import {DateObject, isNullDate} from '../../helpers/utils';
import {DateObject, convertMapToObject, isNullDate} from '../../helpers/utils';
import type {List, Map} from 'immutable';
import {faClone, faExternalLinkAlt, faQuestionCircle, faSearch} from '@fortawesome/free-solid-svg-icons';
import {
Expand Down Expand Up @@ -123,7 +123,7 @@ type DispatchProps = {
onHeightChange: (arg: React.ChangeEvent<HTMLInputElement>) => unknown,
onLanguagesChange: (arg: Array<LanguageOption>) => unknown,
onPagesChange: (arg: React.ChangeEvent<HTMLInputElement>) => unknown,
onPublisherChange: (arg: Publisher) => unknown,
onPublisherChange: (arg: Publisher[]) => unknown,
onToggleShowEditionGroupSection: (showEGSection: boolean) => unknown,
onEditionGroupChange: (arg: EditionGroup) => unknown,
onReleaseDateChange: (arg: string) => unknown,
Expand Down Expand Up @@ -180,7 +180,7 @@ function EditionSection({
editionGroupValue,
editionGroupVisible,
matchingNameEditionGroups,
publisherValue,
publisherValue: publishers,
releaseDateValue,
statusValue,
weightValue,
Expand All @@ -191,7 +191,8 @@ function EditionSection({
label: language.name,
value: language.id
}));

let publisherValue = publishers ?? {};
publisherValue = Object.values(convertMapToObject(publisherValue));
const editionFormatsForDisplay = editionFormats.map((format) => ({
label: format.label,
value: format.id
Expand Down Expand Up @@ -319,6 +320,7 @@ function EditionSection({
<Row>
<Col lg={{offset: 3, span: 6}}>
<EntitySearchFieldOption
isMulti
instanceId="publisher"
label="Publisher"
type="publisher"
Expand Down Expand Up @@ -460,7 +462,6 @@ EditionSection.displayName = 'EditionSection';
type RootState = Map<string, Map<string, any>>;
function mapStateToProps(rootState: RootState): StateProps {
const state: Map<string, any> = rootState.get('editionSection');

return {
depthValue: state.get('depth'),
editionGroupRequired: state.get('editionGroupRequired'),
Expand Down Expand Up @@ -503,7 +504,7 @@ function mapDispatchToProps(dispatch: Dispatch<Action>): DispatchProps {
onPagesChange: (event) => dispatch(debouncedUpdatePages(
event.target.value ? parseInt(event.target.value, 10) : null
)),
onPublisherChange: (value) => dispatch(updatePublisher(value)),
onPublisherChange: (value) => dispatch(updatePublisher(Object.fromEntries(value.map((pub, index) => [index, pub])))),
onReleaseDateChange: (releaseDate) =>
dispatch(debouncedUpdateReleaseDate(releaseDate)),
onStatusChange: (value: {value: number} | null) =>
Expand Down
2 changes: 1 addition & 1 deletion src/client/entity-editor/edition-section/reducer.ts
Expand Up @@ -47,7 +47,7 @@ function reducer(
languages: Immutable.List([]),
matchingNameEditionGroups: [],
physicalEnable: true,
publisher: null,
publisher: Immutable.fromJS({}),
releaseDate: '',
status: null
}),
Expand Down
15 changes: 13 additions & 2 deletions src/client/entity-editor/validators/edition.ts
Expand Up @@ -28,6 +28,7 @@ import {
import {Iterable} from 'immutable';
import _ from 'lodash';
import type {_IdentifierType} from '../../../types';
import {convertMapToObject} from '../../helpers/utils';


export function validateEditionSectionDepth(value: any): boolean {
Expand Down Expand Up @@ -74,8 +75,18 @@ export function validateEditionSectionPublisher(value: any): boolean {
if (!value) {
return true;
}

return validateUUID(get(value, 'id', null), true);
const publishers = convertMapToObject(value);
let flag = false;
for (const pubId in publishers) {
if (Object.prototype.hasOwnProperty.call(publishers, pubId)) {
const publisher = publishers[pubId];
if (!validateUUID(get(publisher, 'id', null), true)) {
return false;
}
flag = true;
}
}
return flag;
}

export function validateEditionSectionReleaseDate(value: any) {
Expand Down
13 changes: 8 additions & 5 deletions src/client/helpers/entity.tsx
Expand Up @@ -203,11 +203,14 @@ export function getEditionPublishers(edition) {

if (hasPublishers) {
return edition.publisherSet.publishers.map(
(publisher) => (
<a href={`/publisher/${publisher.bbid}`} key={publisher.bbid}>
{_get(publisher, 'defaultAlias.name', publisher.bbid)}
</a>
)
(publisher) =>
(
<span key={publisher.bbid}>
<a href={`/publisher/${publisher.bbid}`}>
{_get(publisher, 'defaultAlias.name', publisher.bbid)}
</a> &nbsp;
MonkeyDo marked this conversation as resolved.
Show resolved Hide resolved
</span>
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/entity/edition.js
Expand Up @@ -88,7 +88,7 @@ function transformNewForm(data) {
pages: data.editionSection.pages &&
parseInt(data.editionSection.pages, 10),
publishers: data.editionSection.publisher &&
[data.editionSection.publisher.id],
Object.values(data.editionSection.publisher).map((pub) => pub.id),
relationships,
releaseEvents,
statusId: data.editionSection.status &&
Expand Down Expand Up @@ -402,7 +402,7 @@ function editionToFormState(edition) {

const publisher = edition.publisherSet && (
_.isEmpty(edition.publisherSet.publishers) ?
null : utils.entityToOption(edition.publisherSet.publishers[0])
null : Object.fromEntries(edition.publisherSet.publishers.map(utils.entityToOption).map((op, index) => [index, op]))
);

const editionGroup = utils.entityToOption(edition.editionGroup);
Expand Down
4 changes: 2 additions & 2 deletions test/src/client/entity-editor/validators/test-edition.js
Expand Up @@ -184,9 +184,9 @@ function describeValidateEditionSectionLanguages() {
});
}

const VALID_ENTITY = {
MonkeyDo marked this conversation as resolved.
Show resolved Hide resolved
const VALID_ENTITY = {0: {
id: '21675f5b-e9f8-4a6b-9aac-d3c965aa7d83'
};
}};

const INVALID_ENTITY = {
id: '2'
Expand Down