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