Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/packages / features / Authentication / ResetFlow / Forms / CreateNewPasswordForm.js
Size: Mime:
import * as tslib_1 from "tslib";
import React from 'react';
import { observer } from 'xmobx/mobx-react';
import { isUndefinedLike } from 'exotic';
import { FormState, ObserverForm } from '@skava/packages/ui/forms';
import { ContinueButton } from '@skava/packages/ui';
import { sessionContainer } from '@skava/packages/core/auth/session/container';
import { oneRouter } from '@skava/router';
import { errorContainer } from '@skava/packages/core/notifications';
import { wording } from '../fixture';
import { securityQuestionContainer } from './FormState';
import { resetPasswordFormContainer } from './ResetPasswordForm';
import { isErrorLikeResponseForUser, fromResponseToToastMessage, } from '../state/deps';
class ResetPasswordForm extends FormState {
    constructor() {
        super(...arguments);
        this.inputsList = [
            {
                identity: 'resetcode',
                type: 'text',
                value: '',
                className: 'reset-code-wrapper',
                name: 'resetcode',
                isHidden: false,
                labelText: wording.resetCode,
                maxLength: 50,
                wrapperClassName: 'reset-code-wrapper',
                validationType: 'required',
                errorMessageFor: 'required',
                ariaLabel: wording.resetCode,
            },
            {
                type: 'label',
                name: 'recieve-code',
                labelText: wording.recievecode,
                classes: 'recieve-code-label',
                wrapperClassName: 'recieve-code-label',
                qa: 'recieve-code-label',
            },
            {
                identity: 'password',
                type: 'password',
                value: '',
                className: 'register-password',
                name: 'password',
                labelText: wording.password,
                maxLength: 90,
                wrapperClassName: 'register-password-container',
                // doesMatchesUserName: this.doesMatchesUserName,
                validationType: 'password',
                errorMessageFor: 'password',
                ariaLabel: wording.password,
            },
            {
                identity: 'confirmPassword',
                type: 'password',
                value: '',
                className: 'register-confirm-password',
                name: 'confirmPassword',
                labelText: wording.reenterPassword,
                maxLength: 90,
                wrapperClassName: 'register-confirm-password-container',
                validationType: 'confirmPassword',
                onFormValidation: this.validateConfirmPassword,
                errorMessageFor: 'confirmPassword',
                ariaLabel: wording.reenterPassword,
            },
        ];
    }
}
const resetPasswordForm = new ResetPasswordForm();
let RenderResetPasswordForm = class RenderResetPasswordForm extends ObserverForm {
    constructor() {
        super(...arguments);
        this.updateToastWith = async (response, isUserActivation) => {
            // 1
            const errorMessage = fromResponseToToastMessage(response);
            const error = {
                errorMessage,
            };
            errorContainer.setError(error);
            if (isErrorLikeResponseForUser(response) === false) {
                oneRouter.update('/signin');
            }
        };
        this.resetUsingEmail = async () => {
            // @todo why are we double encoding this?!!?!?!?!?
            const resetParam = encodeURIComponent(encodeURIComponent(oneRouter.get('resetParam')));
            const isUserActivation = encodeURIComponent(encodeURIComponent(oneRouter.get('userActivation')));
            const { password, confirmPassword } = this.state.toSerialized();
            // @todo dangerous, should not do response like this, spli to smaller fns
            let response = undefined;
            if (!isUndefinedLike(isUserActivation)) {
                const userActivation = {
                    password,
                    activationParam: resetParam,
                };
                response = await sessionContainer.userActivation(userActivation);
            }
            else {
                const resetByEmailParams = {
                    resetParam,
                    password,
                    confirmPassword,
                };
                response = await sessionContainer.validateByEmail(resetByEmailParams);
            }
            this.updateToastWith(response);
        };
        this.resetUsingSecurityQuestions = async () => {
            console.log('resetPasswordFormContainer', resetPasswordFormContainer.toSerialized(), 'securityQuestionContainer', securityQuestionContainer.toSerialized());
            const { email } = resetPasswordFormContainer.toSerialized();
            const { securityanswer1, securityquestion1 } = securityQuestionContainer.toSerialized();
            const { password } = this.state.toSerialized();
            const securityQuestionsParams = {
                emailId: email,
                challengeAnswer1: securityanswer1,
                challengeQuestion1: securityquestion1,
                password,
            };
            const response = await sessionContainer.resetPasswordThroughSecurityQuestions(securityQuestionsParams);
            this.updateToastWith(response);
        };
        /**
         * @see https://bitbucket.org/skava-admin/reference-store/pull-requests/2170/tsc-issue-fixes-for-resetflow/diff?w=1#comment-88954781
         */
        this.handleSubmit = (event) => {
            event.preventDefault();
            if (this.validateForm()) {
                const serialized = this.state.toSerialized();
                if (this.props.isFromSecurityCode) {
                    if (oneRouter.get('step') === 'resetPasswordLink') {
                        this.resetUsingEmail();
                    }
                    else {
                        this.resetUsingSecurityQuestions();
                    }
                }
                console.log(serialized);
            }
            else {
                console.error('Form has invalid inputs!');
            }
        };
        /**
         * @todo using binding here should not be done...
         */
        this.componentWillMount = () => {
            const checkIsFromSecurityQuestion = (inputState) => {
                if ((inputState.name === 'resetcode' || inputState.name === 'recieve-code') &&
                    this.props.isFromSecurityCode) {
                    inputState.isHidden = true;
                }
                return inputState;
            };
            this.state.inputsList = this.state.inputsList.map(checkIsFromSecurityQuestion);
        };
    }
    render() {
        const { setPasswordText, resetPasswordText } = wording;
        const textLabel = !isUndefinedLike(this.props.isUserActivation)
            ? setPasswordText
            : resetPasswordText;
        return (React.createElement(this.Form, { setRef: this.state.setFormReference },
            this.renderInputList(),
            React.createElement(ContinueButton, { className: "reset-continue-button", onClick: this.handleSubmit, text: textLabel, qa: "qa-reset-password-button" })));
    }
};
// @todo @@typings put as typings, @@forms4.0
// static propTypes = {
//   state: obj,
//   onClose: func.isRequired,
//   onCancel: func.isRequired,
//   isFromSecurityCode: bool,
// }
RenderResetPasswordForm.defaultProps = {
    state: resetPasswordForm,
};
RenderResetPasswordForm = tslib_1.__decorate([
    observer
], RenderResetPasswordForm);
export { RenderResetPasswordForm };
export default RenderResetPasswordForm;
//# sourceMappingURL=CreateNewPasswordForm.js.map