Repository URL to install this package:
|
Version:
0.0.4 ▾
|
/**
* @todo convert to class container
*/
import { observable } from 'xmobx/mobx';
import { isSafe, isTrue } from 'exotic';
import { ObservableContainer } from '@skava/packages/libraries/observable-container';
const isTypeDropDown = (x) => x.type === 'select';
const toValue = (x) => x.value;
const setCustomSecurityQuestionState = (input) => {
// To reset the answer field when a new option is selected
// input.value = ''
if (state.shouldShowNext) {
input.isHidden = false;
state.shouldShowNext = false;
}
if (state.shouldHideNext) {
input.isHidden = true;
state.shouldHideNext = false;
}
};
const showHideCustomSecurityQuestion = (option, value, inputsList) => {
option.disabled = false;
if (option.value === value) {
if (isTrue(option.isCustom)) {
state.shouldShowNext = true;
}
else {
state.shouldHideNext = true;
emptySecurityAnswer(inputsList, option);
state.previousSelected = option.value;
}
}
};
const state = observable({
shouldShowNext: false,
shouldHideNext: false,
previousSelected: '',
setCustomSecurityQuestionState,
showHideCustomSecurityQuestion,
});
const emptySecurityAnswer = (inputsList, option) => {
const findSecurityAnswer = (input) => {
return input.name === 'securityanswer1' || input.name === 'securityAnswer';
};
if (state.previousSelected !== option.value) {
const securityAnswerState = inputsList.find(findSecurityAnswer);
if (securityAnswerState && securityAnswerState.value) {
securityAnswerState.value = '';
}
}
};
class SecurityQuestionsContainer extends ObservableContainer {
constructor() {
super(...arguments);
// @action @mutates input.disabled
this.disableSelectedValues = (value, elementState, inputsList) => {
// 0. define
const key = elementState.name;
const selectedInputsOptions = inputsList.filter(isTypeDropDown);
const selectedOptions = selectedInputsOptions.map(toValue);
// const isDropDown = isDropDownForKey(key)
// 1. each input
const mutateInput = (input) => {
state.setCustomSecurityQuestionState(input);
if (input.type === 'select') {
if (input.name !== key) {
const options = input.options;
// 4. default
// @todo - seems redundant.......
//
// @todo - isDisabled?
// reseting to original state
const disableInput = (option) => (option.isDisabled = false);
options.forEach(disableInput);
// ===
// 7. disabling the selected options
const disableSelectedValues = (selectedValue) => {
// check every selected value
// against every option...
// @todo for a simpler implementation see the radiogroup
const checkSelectedAgainstOptions = (option) => {
/**
* If isCustom is true, then don't disable the option
* value of isCustom can be undefined or true
*/
const isCustom = option.value === selectedValue.value &&
input.value !== option.value &&
// what logic is this...
// !option.isCustom === true
// option.isCustom === false?????
isTrue(!option.isCustom);
if (isCustom && isSafe(option.value)) {
option.isDisabled = true;
}
};
// 5.
options.forEach(checkSelectedAgainstOptions);
};
// 5. disable all selected
selectedOptions.forEach(disableSelectedValues);
}
else {
const options = input.options;
options.forEach((option) => state.showHideCustomSecurityQuestion(option, value, inputsList));
}
}
};
// 3. iterate
inputsList.forEach(mutateInput);
};
}
}
const securityQuestionsContainer = new SecurityQuestionsContainer();
export default securityQuestionsContainer;
export { securityQuestionsContainer };
export { securityQuestionsContainer as container };
export { SecurityQuestionsContainer };
//# sourceMappingURL=container.js.map