"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var Yup = require("yup");
var react_router_dom_1 = require("react-router-dom");
var formik_1 = require("formik");
var routes_1 = require("../../../../apiClient/routes");
var httpService_1 = require("../../../../services/httpService");
var react_toastify_1 = require("react-toastify");
var Signup = /** @class */ (function (_super) {
    __extends(Signup, _super);
    function Signup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            displayErrorMessage: false
        };
        _this.handleSubmit = function (values) {
            httpService_1.default.post(routes_1.AccountController_RegisterUser, values, { validateStatus: function (status) { return status <= 400; } }).then(function (response) {
                if (response.status == 200) {
                    react_toastify_1.toast.success('👍 Registrace proběhla úspěšně!', {
                        position: "top-right",
                        autoClose: 3000,
                        hideProgressBar: true,
                        closeOnClick: true,
                        pauseOnHover: false,
                        draggable: true,
                        progress: undefined,
                    });
                    _this.props.history.push("/login");
                }
                else if (response.status == 400) {
                    react_toastify_1.toast.error('Něco se nepovedlo :(', {
                        position: "top-right",
                        autoClose: 3000,
                        hideProgressBar: true,
                        closeOnClick: true,
                        pauseOnHover: false,
                        draggable: true,
                        progress: undefined,
                    });
                    _this.setState({ displayErrorMessage: true });
                }
            }).catch(function (e) {
                console.log(e);
                _this.setState(__assign({}, _this.state));
            });
        };
        return _this;
    }
    Signup.prototype.componentDidMount = function () {
        document.title = "Registrace";
    };
    Signup.prototype.render = function () {
        var _this = this;
        var signupValidationSchema = Yup.object().shape({
            email: Yup.string().required("Email je vyžadován!").email("Nevalidní emailová adresa!"),
            password: Yup.string().required("Heslo je vyžadováno!").min(8, "Minimální délka hesla je 8 znaků!"),
            confirmPassword: Yup.string().required("Potvrzení hesla je vyžadováno!").oneOf([Yup.ref('password'), null], "Hesla musí být stejná!")
        });
        var signupInitValues = {
            email: '',
            password: '',
            confirmPassword: ''
        };
        return (React.createElement("div", { className: "container" },
            React.createElement("div", { className: "row" },
                React.createElement("div", { className: "col-lg-3" }),
                React.createElement("div", { className: "col-lg-6" },
                    React.createElement(formik_1.Formik, { initialValues: signupInitValues, validationSchema: signupValidationSchema, onSubmit: function (values) { _this.handleSubmit(values); } }, function (_a) {
                        var errors = _a.errors, handleReset = _a.handleReset, handleSubmit = _a.handleSubmit, touched = _a.touched;
                        return (React.createElement(formik_1.Form, { onReset: handleReset, onSubmit: handleSubmit },
                            React.createElement("div", { className: "checkout__input" },
                                React.createElement("p", null,
                                    "Email",
                                    React.createElement("span", null, "*")),
                                _this.state.displayErrorMessage
                                    ? React.createElement("label", { className: "error-message" }, "Zadan\u00E1 emailov\u00E1 adresa je ji\u017E registrov\u00E1na!")
                                    : '',
                                React.createElement(formik_1.Field, { name: "email", type: "email", placeholder: "Emailov\u00E1 adresa", className: (touched.email && errors.email)
                                        ? 'input__error'
                                        : (touched.email && !errors.email ? 'input__success' : '') }),
                                errors && touched.email && errors.email && React.createElement(React.Fragment, null,
                                    React.createElement("label", { className: "error-message" }, errors.email))),
                            React.createElement("div", { className: "checkout__input" },
                                React.createElement("p", null,
                                    "Heslo",
                                    React.createElement("span", null, "*")),
                                React.createElement(formik_1.Field, { name: "password", type: "password", placeholder: "Heslo", className: (touched.password && errors.password)
                                        ? 'input__error'
                                        : (touched.password && !errors.password ? 'input__success' : '') }),
                                errors && touched.password && errors.password && React.createElement(React.Fragment, null,
                                    React.createElement("label", { className: "error-message" }, errors.password))),
                            React.createElement("div", { className: "checkout__input" },
                                React.createElement("p", null,
                                    "Potvrzen\u00ED hesla",
                                    React.createElement("span", null, "*")),
                                React.createElement(formik_1.Field, { name: "confirmPassword", type: "password", placeholder: "Potvrzen\u00ED hesla", className: (touched.confirmPassword && errors.confirmPassword)
                                        ? 'input__error'
                                        : (touched.confirmPassword && !errors.confirmPassword ? 'input__success' : '') }),
                                errors && touched.confirmPassword && errors.confirmPassword && React.createElement(React.Fragment, null,
                                    React.createElement("label", { className: "error-message" }, errors.confirmPassword))),
                            React.createElement("button", { type: "submit", className: "site-btn" }, "Registrovat se"),
                            React.createElement("div", { className: "d-flex justify-content-center mt-2" },
                                React.createElement("p", null,
                                    React.createElement(react_router_dom_1.Link, { className: "login__link", to: "/login" }, "-> Zpět na přihlášení <-")))));
                    })),
                React.createElement("div", { className: "col-lg-3" }))));
    };
    return Signup;
}(React.Component));
exports.default = Signup;
//# sourceMappingURL=Signup.js.map