Repository URL to install this package:
|
Version:
0.9.5 ▾
|
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const mobx_1 = require("xmobx/mobx");
const deps_1 = require("../deps");
const isValidCreditCard_1 = require("../deps/isValidCreditCard");
const isValidExpiryDate_1 = require("../deps/isValidExpiryDate");
const deps_2 = require("./deps");
// const curriedDecorate = curry('_', decorate)
function curryDecorate(types) {
return function (Klass) {
mobx_1.decorate(Klass, types);
return Klass;
};
}
const decorations = {
setInputsList: mobx_1.action.bound,
setFormReference: mobx_1.action.bound,
setProps: mobx_1.action.bound,
};
// @todo + finish at the end removing validation duplication
// @curryDecorate(decorations)
class FormState {
constructor(stateData = {}) {
// super(stateData)
// extendObservable(this, {
// // I think this has an issue - name
// // name: '',
// hasAllValidInputs: false,
// inputsList: [],
// })
this.hasAllValidInputs = false;
this.inputsList = [];
/**
* @note added identifier check too
* @param {String} name name of the input for this form
* @return {InputState}
*/
this.get = (name) => {
/**
* @todo convert this to a .find
*/
let result;
const isSameName = (inputState) => {
console.info('[forms] input state get - recursing: ', inputState);
if (inputState.type === 'groupElements') {
inputState.elementList.forEach(isSameName);
}
else if (inputState.name === name || inputState.identifier === name) {
result = inputState;
}
};
this.inputsList.forEach(isSameName);
return result;
};
// ========= @todo - remove this duplication =========
this.formValidation = () => {
const isValidInput = item => {
console.log('itemDetails', item);
const { type, isHidden, isEnabled, validationType } = item;
if (isEnabled && type !== 'label' && validationType !== 'none') {
if (type === 'groupElements') {
return item.elementList.every(isValidInput);
}
else {
return this.isValidElement(item);
}
}
else {
return true;
}
};
console.log('current input list', this.inputsList);
const hasAllValidInputs = this.inputsList.every(isValidInput);
console.log('hasAllValidInputs', hasAllValidInputs);
return hasAllValidInputs;
};
/**
* @todo @gnanaprabhu why is this duplicated
*/
this.isValidElement = item => {
const { validationType, value } = item;
if (validationType === 'creditcard' || validationType === 'securitycode') {
return this.validateCreditCard(item);
}
if (validationType === 'month' || validationType === 'year') {
return this.validateExpiryDate(item);
}
if (validationType === 'confirmPassword') {
if (this.validateConfirmPassword(item) === false) {
return false;
}
}
else {
const validationResult = deps_1.isValid(value, validationType);
if (!validationResult) {
return false;
}
}
return true;
};
this.validateCreditCard = item => {
const creditCard = this.get('cardNumber');
const securityCode = this.get('SecurityCode');
if (creditCard && creditCard.getValue()) {
if (securityCode && securityCode.getValue()) {
const isamexCard = isValidCreditCard_1.isAmexCard(creditCard.getValue());
if (isamexCard && securityCode.getValue().length !== 4) {
return false;
}
else if (!isamexCard && securityCode.getValue().length === 4) {
return false;
}
}
return isValidCreditCard_1.isValidCreditCard(creditCard.getValue());
}
return false;
};
this.validateExpiryDate = item => {
const expiryMonth = this.get('expirationyear');
const expiryYear = this.get('expirationyear');
if (expiryMonth.getValue() && expiryYear.getValue()) {
if (isValidExpiryDate_1.isValidYear(expiryYear.getValue(), expiryMonth.getValue())) {
return true;
}
else {
return false;
}
}
return false;
};
/**
* @todo when setting inputs list, copy...
* ORIGINAL_INPUTS_LIST
*/
this.inputsList = mobx_1.observable([]);
// no need to add another method, is an alias for specific usage
this.toSerialized = this.toJSON.bind(this);
}
static init(state) {
return new FormState(state);
}
setInputsList(list) {
this.inputsList = list;
return this;
}
setFormReference(dom) {
this.form = dom;
}
setProps(props) {
this.props = props;
}
// ========= protected read =========
/**
* @type {Computed}
*/
toJSON() {
const formData = {};
const getInputValue = input => {
// ignore labels
if (input.type === 'label') {
return;
}
// title says it all
const { key, value } = deps_2.fromInputToSerializedKeyValue(input, getInputValue);
// special place for radios
if (input.type === 'radio') {
if (value) {
formData['selectedItem'] = key;
}
}
else {
// otherwise, default
formData[key] = value;
/**
* @todo @fixme bad serialization - fixed in ui-component-library
*/
if (formData['0'] === value) {
delete formData['0'];
}
}
};
// this is using `getInputValue` as a longhand `.reduce`
this.inputsList.forEach(getInputValue);
return formData;
}
validateConfirmPassword(item) {
// if (isObj(item) === false) {
// console.error('@ganesh - it is being passed a string')
// return false
// }
const password = this.get('password');
const passwordValue = password.getValue();
item.isValidInput = true;
if (passwordValue === '' || passwordValue !== item.value) {
item.isValidInput = false;
item.errorMessage = deps_1.errorMessage(item.errorMessageFor);
return false;
}
return true;
}
}
/**
* @example [].map(FormState.toInputState)
*/
FormState.toInputState = deps_2.toInputState;
__decorate([
mobx_1.observable
], FormState.prototype, "hasAllValidInputs", void 0);
__decorate([
mobx_1.observable
], FormState.prototype, "inputsList", void 0);
__decorate([
mobx_1.action
], FormState.prototype, "setInputsList", null);
__decorate([
mobx_1.action.bound
], FormState.prototype, "setFormReference", null);
__decorate([
mobx_1.action.bound
], FormState.prototype, "setProps", null);
__decorate([
mobx_1.action.bound
], FormState.prototype, "validateConfirmPassword", null);
exports.FormState = FormState;
exports.default = FormState;
//# sourceMappingURL=FormState.js.map