Skip to content

Commit

Permalink
refactor(entityRoutes): Add generateEnityProps
Browse files Browse the repository at this point in the history
generateEntityProps gives sensible defaults to most `props` values.
  • Loading branch information
naiveai committed Dec 28, 2017
1 parent 845b92c commit 3e4cad9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 108 deletions.
55 changes: 55 additions & 0 deletions src/server/helpers/entityRouteUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2017 Eshan Singh
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

// @flow

import * as utils from './utils';
import _ from 'lodash';
import express from 'express';
import {generateProps} from './props';


type EntityAction = 'create' | 'edit';

/**
* Returns a props object with reasonable defaults for entity creation/editing.
* @param {string} entityType - entity type
* @param {string} action - 'create' or 'edit'
* @param {request} req - request object
* @param {response} res - response object
* @param {object} additionalProps - additional props
* @returns {object} - props
*/
export function generateEntityProps(
entityType: string, action: EntityAction,
req: express.request, res: express.response,
additionalProps: Object): Object {
let entityName = _.capitalize(entityType);
let props = Object.assign({
entityType,
heading: `${_.capitalize(action)} ${entityName}`,
initialState: {},
languageOptions: res.locals.languages,
requiresJS: true,
subheading: action === 'create' ?
`Add a new ${entityName} to BookBrainz` :
`Edit an existing ${entityName} on BookBrainz`
}, additionalProps);

return generateProps(req, res, props);
}
37 changes: 13 additions & 24 deletions src/server/routes/entity/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as error from '../../helpers/error';
import * as middleware from '../../helpers/middleware';
import * as propHelpers from '../../../client/helpers/props';
import * as utils from '../../helpers/utils';
import {escapeProps, generateProps} from '../../helpers/props';
import EntityEditor from '../../../client/entity-editor/entity-editor';
import Immutable from 'immutable';
import Layout from '../../../client/containers/layout';
Expand All @@ -33,7 +32,9 @@ import React from 'react';
import ReactDOMServer from 'react-dom/server';
import _ from 'lodash';
import {createStore} from 'redux';
import {escapeProps} from '../../helpers/props';
import express from 'express';
import {generateEntityProps} from '../../helpers/entityRouteUtils';


const {createRootReducer, getEntitySection, getValidator} = entityEditorHelpers;
Expand Down Expand Up @@ -95,17 +96,11 @@ router.get(
'Creator'
);

const props = generateProps(req, res, {
entityType: 'creator',
genderOptions: res.locals.genders,
heading: 'Create Creator',
identifierTypes: filteredIdentifierTypes,
initialState: {},
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Add a new Creator to BookBrainz',
submissionUrl: '/creator/create/handler'
});
const props = generateEntityProps(
'creator', 'create', req, res, {
identifierTypes: filteredIdentifierTypes
}
);

const {initialState, ...rest} = props;

Expand Down Expand Up @@ -232,18 +227,12 @@ router.get(
creator
);

const props = generateProps(req, res, {
creator,
entityType: 'creator',
genderOptions: res.locals.genders,
heading: 'Edit Creator',
identifierTypes: filteredIdentifierTypes,
initialState: creatorToFormState(creator),
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Edit an existing Creator in BookBrainz',
submissionUrl: `/creator/${creator.bbid}/edit/handler`
});
const props = generateEntityProps(
'creator', 'edit', req, res, {
identifierTypes: filteredIdentifierTypes,
initialState: creatorToFormState(creator)
}
);

const {initialState, ...rest} = props;

Expand Down
34 changes: 13 additions & 21 deletions src/server/routes/entity/edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as error from '../../helpers/error';
import * as middleware from '../../helpers/middleware';
import * as propHelpers from '../../../client/helpers/props';
import * as utils from '../../helpers/utils';
import {escapeProps, generateProps} from '../../helpers/props';
import EntityEditor from '../../../client/entity-editor/entity-editor';
import Immutable from 'immutable';
import Layout from '../../../client/containers/layout';
Expand All @@ -34,7 +33,9 @@ import React from 'react';
import ReactDOMServer from 'react-dom/server';
import _ from 'lodash';
import {createStore} from 'redux';
import {escapeProps} from '../../helpers/props';
import express from 'express';
import {generateEntityProps} from '../../helpers/entityRouteUtils';


const {createRootReducer, getEntitySection, getValidator} = entityEditorHelpers;
Expand Down Expand Up @@ -118,16 +119,11 @@ router.get(
);

const {Publication, Publisher} = req.app.locals.orm;
const propsPromise = generateProps(req, res, {
entityType: 'edition',
heading: 'Create Edition',
identifierTypes: filteredIdentifierTypes,
initialState: {},
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Add a new Edition to BookBrainz',
submissionUrl: '/edition/create/handler'
});
const propsPromise = generateEntityProps(
'edition', 'create', req, res, {
identifierTypes: filteredIdentifierTypes
}
);

if (req.query.publication) {
propsPromise.publication =
Expand Down Expand Up @@ -296,16 +292,12 @@ router.get(
edition
);

const props = generateProps(req, res, {
entityType: 'edition',
heading: 'Edit Edition',
identifierTypes: filteredIdentifierTypes,
initialState: editionToFormState(edition),
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Edit an existing Edition in BookBrainz',
submissionUrl: `/edition/${edition.bbid}/edit/handler`
});
const props = generateEntityProps(
'edition', 'edit', req, res, {
initialState: editionToFormState(edition),
submissionUrl: `edition/${edition.bbid}/edit/handler`
}
);

const {initialState, ...rest} = props;

Expand Down
34 changes: 13 additions & 21 deletions src/server/routes/entity/publication.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as error from '../../helpers/error';
import * as middleware from '../../helpers/middleware';
import * as propHelpers from '../../../client/helpers/props';
import * as utils from '../../helpers/utils';
import {escapeProps, generateProps} from '../../helpers/props';
import EntityEditor from '../../../client/entity-editor/entity-editor';
import Immutable from 'immutable';
import Layout from '../../../client/containers/layout';
Expand All @@ -33,7 +32,9 @@ import React from 'react';
import ReactDOMServer from 'react-dom/server';
import _ from 'lodash';
import {createStore} from 'redux';
import {escapeProps} from '../../helpers/props';
import express from 'express';
import {generateEntityProps} from '../../helpers/entityRouteUtils';


const {createRootReducer, getEntitySection, getValidator} = entityEditorHelpers;
Expand Down Expand Up @@ -100,16 +101,11 @@ router.get(
'Publication'
);

const props = generateProps(req, res, {
entityType: 'publication',
heading: 'Create Publication',
identifierTypes: filteredIdentifierTypes,
initialState: {},
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Add a new Publication to BookBrainz',
submissionUrl: '/publication/create/handler'
});
const props = generateEntityProps(
'publication', 'create', req, res, {
identifierTypes: filteredIdentifierTypes
}
);

const {initialState, ...rest} = props;

Expand Down Expand Up @@ -214,16 +210,12 @@ router.get(
publication
);

const props = generateProps(req, res, {
entityType: 'publication',
heading: 'Edit Publication',
identifierTypes: filteredIdentifierTypes,
initialState: publicationToFormState(publication),
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Edit an existing Publication in BookBrainz',
submissionUrl: `/publication/${publication.bbid}/edit/handler`
});
const props = generateEntityProps(
'publication', 'edit', req, res, {
identifierTypes: filteredIdentifierTypes,
initialState: publicationToFormState(publication)
}
);

const {initialState, ...rest} = props;

Expand Down
34 changes: 13 additions & 21 deletions src/server/routes/entity/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as error from '../../helpers/error';
import * as middleware from '../../helpers/middleware';
import * as propHelpers from '../../../client/helpers/props';
import * as utils from '../../helpers/utils';
import {escapeProps, generateProps} from '../../helpers/props';
import EntityEditor from '../../../client/entity-editor/entity-editor';
import Immutable from 'immutable';
import Layout from '../../../client/containers/layout';
Expand All @@ -33,7 +32,9 @@ import React from 'react';
import ReactDOMServer from 'react-dom/server';
import _ from 'lodash';
import {createStore} from 'redux';
import {escapeProps} from '../../helpers/props';
import express from 'express';
import {generateEntityProps} from '../../helpers/entityRouteUtils';


const {createRootReducer, getEntitySection, getValidator} = entityEditorHelpers;
Expand Down Expand Up @@ -110,16 +111,11 @@ router.get(
'Publisher'
);

const props = generateProps(req, res, {
entityType: 'publisher',
heading: 'Create Publisher',
identifierTypes: filteredIdentifierTypes,
initialState: {},
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Add a new Publisher to BookBrainz',
submissionUrl: '/publisher/create/handler'
});
const props = generateEntityProps(
'publisher', 'create', req, res, {
identifierTypes: filteredIdentifierTypes
}
);

const {initialState, ...rest} = props;

Expand Down Expand Up @@ -243,16 +239,12 @@ router.get(
publisher
);

const props = generateProps(req, res, {
entityType: 'publisher',
heading: 'Edit Publisher',
identifierTypes: filteredIdentifierTypes,
initialState: publisherToFormState(publisher),
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Edit an existing Publisher in BookBrainz',
submissionUrl: `/publisher/${publisher.bbid}/edit/handler`
});
const props = generateEntityProps(
'publisher', 'edit', req, res, {
identifierTypes: filteredIdentifierTypes,
initialState: publisherToFormState(publisher)
}
);

const {initialState, ...rest} = props;

Expand Down
34 changes: 13 additions & 21 deletions src/server/routes/entity/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as error from '../../helpers/error';
import * as middleware from '../../helpers/middleware';
import * as propHelpers from '../../../client/helpers/props';
import * as utils from '../../helpers/utils';
import {escapeProps, generateProps} from '../../helpers/props';
import EntityEditor from '../../../client/entity-editor/entity-editor';
import Immutable from 'immutable';
import Layout from '../../../client/containers/layout';
Expand All @@ -33,7 +32,9 @@ import React from 'react';
import ReactDOMServer from 'react-dom/server';
import _ from 'lodash';
import {createStore} from 'redux';
import {escapeProps} from '../../helpers/props';
import express from 'express';
import {generateEntityProps} from '../../helpers/entityRouteUtils';


const {createRootReducer, getEntitySection, getValidator} = entityEditorHelpers;
Expand Down Expand Up @@ -96,16 +97,11 @@ router.get(
'Work'
);

const props = generateProps(req, res, {
entityType: 'work',
heading: 'Create Work',
identifierTypes: filteredIdentifierTypes,
initialState: {},
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Add a new Work to BookBrainz',
submissionUrl: '/work/create/handler'
});
const props = generateEntityProps(
'work', 'create', req, res, {
identifierTypes: filteredIdentifierTypes
}
);

const {initialState, ...rest} = props;

Expand Down Expand Up @@ -214,16 +210,12 @@ router.get(

workToFormState(work);

const props = generateProps(req, res, {
entityType: 'work',
heading: 'Edit Work',
identifierTypes: filteredIdentifierTypes,
initialState: workToFormState(work),
languageOptions: res.locals.languages,
requiresJS: true,
subheading: 'Edit an existing Work in BookBrainz',
submissionUrl: `/work/${work.bbid}/edit/handler`
});
const props = generateEntityProps(
'work', 'edit', req, res, {
identifierTypes: filteredIdentifierTypes,
initialState: workToFormState(work)
}
);

const {initialState, ...rest} = props;

Expand Down

0 comments on commit 3e4cad9

Please sign in to comment.