opds-web-client
Version:
64 lines (63 loc) • 3.13 kB
JavaScript
"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");
var styles_1 = require("./styles");
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 () {
var authFormStyle = styles_1.popupStyle(300, 300);
return (React.createElement("div", {className: "authForm", style: authFormStyle}, React.createElement("h3", {style: { marginTop: "0px", marginBottom: "20px" }}, this.props.title + " " || "", "Login"), this.state.error &&
React.createElement("div", {className: "authFormError", style: { padding: "0.5em", color: "#f00" }}, 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;