1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 16:48:04 +02:00

Fix ESLint errors with form utils

This commit is contained in:
Brett Sun 2016-06-13 15:48:57 +02:00
parent 085d0aaa26
commit 574a06311f

View File

@ -1,15 +1,12 @@
'use strict';
import { getLangText } from './lang'; import { getLangText } from './lang';
import GlobalNotificationActions from '../actions/global_notification_actions'; import GlobalNotificationActions from '../actions/global_notification_actions';
import GlobalNotificationModel from '../models/global_notification_model'; import GlobalNotificationModel from '../models/global_notification_model';
import AppConstants from '../constants/application_constants';
/** /**
* Validates a given list of forms * Validates a given list of forms
* @param {Form} forms List of forms, each of which should have a `validate` method available * @param {Form[]} forms List of forms, each of which should have a `validate`
* method available
* @param {boolean} showFailureNotification Show global notification if there are validation failures * @param {boolean} showFailureNotification Show global notification if there are validation failures
* @return {boolean} True if validation did *NOT* catch any errors * @return {boolean} True if validation did *NOT* catch any errors
*/ */
@ -23,7 +20,10 @@ export function validateForms(forms, showFailureNotification) {
}, true); }, true);
if (!validationSuccessful && showFailureNotification) { if (!validationSuccessful && showFailureNotification) {
const notification = new GlobalNotificationModel(getLangText('Oops, there may be missing or invalid fields. Please check your inputs again.'), 'danger'); const notification = new GlobalNotificationModel(
getLangText('Oops, there may be missing or invalid fields. Please check your inputs again.'),
'danger'
);
GlobalNotificationActions.appendGlobalNotification(notification); GlobalNotificationActions.appendGlobalNotification(notification);
} }
@ -32,17 +32,18 @@ export function validateForms(forms, showFailureNotification) {
/** /**
* Get the data ids of the given piece or editions. * Get the data ids of the given piece or editions.
* @param {boolean} isPiece Is the given entities parameter a piece? (False: array of editions) * @param {boolean} isPiece Is the given entities parameter a piece?
* (False: array of editions)
* @param {(object|object[])} pieceOrEditions Piece or array of editions * @param {(object|object[])} pieceOrEditions Piece or array of editions
* @return {(object|object[])} Data IDs of the pieceOrEditions for the form * @return {(object|object[])} Data IDs of the pieceOrEditions for the form
*/ */
export function getAclFormDataId(isPiece, pieceOrEditions) { export function getAclFormDataId(isPiece, pieceOrEditions) {
if (isPiece) { if (isPiece) {
return {piece_id: pieceOrEditions.id}; return { piece_id: pieceOrEditions.id };
} else { } else {
return {bitcoin_id: pieceOrEditions.map(function(edition){ return {
return edition.bitcoin_id; bitcoin_id: pieceOrEditions.map((edition) => edition.bitcoin_id).join()
}).join()}; };
} }
} }
@ -51,73 +52,68 @@ export function getAclFormDataId(isPiece, pieceOrEditions) {
* @param {object} options Options object for creating the message: * @param {object} options Options object for creating the message:
* @param {string} options.aclName Enum name of an acl * @param {string} options.aclName Enum name of an acl
* @param {(object|object[])} options.entities Piece or array of Editions * @param {(object|object[])} options.entities Piece or array of Editions
* @param {boolean} options.isPiece Is the given entities parameter a piece? (False: array of editions) * @param {boolean} options.isPiece Is the given entities parameter a piece?
* (False: array of editions)
* @param {string} [options.senderName] Name of the sender * @param {string} [options.senderName] Name of the sender
* @return {string} Completed message * @return {string} Completed message
*/ */
export function getAclFormMessage(options) { export function getAclFormMessage({ aclName, additionalMessage, entities, isPiece, senderName }) {
if (!options || options.aclName === undefined || options.isPiece === undefined || if (aclName === undefined || isPiece === undefined ||
!(typeof options.entities === 'object' || options.entities.constructor === Array)) { !(typeof entities === 'object' || Array.isArray(entities))) {
throw new Error('You must specify an acl class, entities in the correct format, and entity type'); throw new Error('You must specify an acl class, entities in the correct format, and ' +
'entity type');
} }
let aclName = options.aclName; const entityTitles = isPiece ? getTitlesStringOfPiece(entities)
let entityTitles = options.isPiece ? getTitlesStringOfPiece(options.entities) : getTitlesStringOfEditions(entities);
: getTitlesStringOfEditions(options.entities);
let message = '';
message += getLangText('Hi'); let message = `${getLangText('Hi')}\n\n`;
message += ',\n\n';
if(aclName === 'acl_transfer') { if (aclName === 'acl_transfer') {
message += getLangText('I transfer ownership of'); message += getLangText('I transfer ownership of');
} else if(aclName === 'acl_consign') { } else if (aclName === 'acl_consign') {
message += getLangText('I consign'); message += getLangText('I consign');
} else if(aclName === 'acl_unconsign') { } else if (aclName === 'acl_unconsign') {
message += getLangText('I un-consign'); message += getLangText('I un-consign');
} else if(aclName === 'acl_loan') { } else if (aclName === 'acl_loan') {
message += getLangText('I loan'); message += getLangText('I loan');
} else if(aclName === 'acl_loan_request') { } else if (aclName === 'acl_loan_request') {
message += getLangText('I request to loan'); message += getLangText('I request to loan');
} else if(aclName === 'acl_share') { } else if (aclName === 'acl_share') {
message += getLangText('I share'); message += getLangText('I share');
} else { } else {
throw new Error('Your specified aclName did not match a an acl class.'); throw new Error('Your specified aclName did not match a an acl class.');
} }
message += ':\n'; message += `:\n${entityTitles}`;
message += entityTitles;
if(aclName === 'acl_transfer' || aclName === 'acl_loan' || aclName === 'acl_consign') { if (aclName === 'acl_transfer' || aclName === 'acl_loan' || aclName === 'acl_consign') {
message += getLangText('to you'); message += getLangText('to you');
} else if(aclName === 'acl_unconsign' || aclName === 'acl_loan_request') { } else if (aclName === 'acl_unconsign' || aclName === 'acl_loan_request') {
message += getLangText('from you'); message += getLangText('from you');
} else if(aclName === 'acl_share') { } else if (aclName === 'acl_share') {
message += getLangText('with you'); message += getLangText('with you');
} else { } else {
throw new Error('Your specified aclName did not match a an acl class.'); throw new Error('Your specified aclName did not match a an acl class.');
} }
if (options.additionalMessage) { if (additionalMessage) {
message += '\n\n' + options.additionalMessage; message += `\n\n${additionalMessage}`;
} }
if (options.senderName) { if (senderName) {
message += '\n\n'; message += `\n\n${getLangText('Truly yours')},\n${senderName}`;
message += getLangText('Truly yours,');
message += '\n';
message += options.senderName;
} }
return message; return message;
} }
function getTitlesStringOfPiece(piece){ function getTitlesStringOfPiece(piece) {
return '\"' + piece.title + '\"'; return `"${piece.title}"`;
} }
function getTitlesStringOfEditions(editions) { function getTitlesStringOfEditions(editions) {
return editions.map(function(edition) { return editions.map((edition) => (
return '- \"' + edition.title + ', ' + getLangText('edition') + ' ' + edition.edition_number + '\"\n'; `- "${edition.title}, ${getLangText('edition')} ${edition.edition_number}"\n`)
}).join(''); ).join();
} }