UNPKG

opds-web-client

Version:
63 lines (62 loc) 2.97 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var React = require("react"); require("../stylesheets/basic_auth_form.scss"); var BasicAuthForm = (function (_super) { __extends(BasicAuthForm, _super); function BasicAuthForm(props) { _super.call(this, props); this.state = { error: this.props.error }; this.submit = this.submit.bind(this); } BasicAuthForm.prototype.render = function () { return (React.createElement("div", {className: "auth-form"}, React.createElement("h3", null, this.props.title + " " || "", "Login"), this.state.error && React.createElement("div", {className: "error"}, this.state.error), React.createElement("form", {onSubmit: this.submit}, React.createElement("input", {className: "form-control", ref: "login", type: "text", placeholder: this.loginLabel()}), React.createElement("br", null), React.createElement("input", {className: "form-control", ref: "password", type: "password", placeholder: this.passwordLabel()}), React.createElement("br", null), React.createElement("input", {type: "submit", className: "btn btn-default", value: "Submit"})))); }; BasicAuthForm.prototype.componentWillReceiveProps = function (nextProps) { this.setState({ error: nextProps.error }); }; BasicAuthForm.prototype.loginLabel = function () { return this.props.loginLabel || "username"; }; BasicAuthForm.prototype.passwordLabel = function () { return this.props.passwordLabel || "password"; }; BasicAuthForm.prototype.validate = function () { var login = this.refs["login"].value; var password = this.refs["password"].value; if (!login || !password) { this.setState({ error: this.loginLabel() + " and " + this.passwordLabel() + " are required" }); return false; } else { this.setState({ error: null }); } return true; }; BasicAuthForm.prototype.submit = function (event) { event.preventDefault(); if (this.validate()) { var login = this.refs["login"].value; var password = this.refs["password"].value; var credentials = this.generateCredentials(login, password); this.props.saveCredentials(credentials); this.props.hide(); if (this.props.callback) { this.props.callback(credentials); } } }; BasicAuthForm.prototype.generateCredentials = function (login, password) { return btoa(login + ":" + password); }; return BasicAuthForm; }(React.Component)); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = BasicAuthForm;