slate/scenes/SceneSignIn.js

172 lines
4.4 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as Actions from "~/common/actions";
import * as System from "~/components/system";
import * as Constants from "~/common/constants";
import * as Window from "~/common/window";
import * as Validations from "~/common/validations";
import * as Strings from "~/common/strings";
import * as Events from "~/common/custom-events";
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
import { SignIn } from "~/components/core/SignIn";
2020-08-03 07:06:44 +03:00
import WebsitePrototypeHeader from "~/components/core/WebsitePrototypeHeader";
import WebsitePrototypeFooter from "~/components/core/WebsitePrototypeFooter";
const STYLES_ROOT = css`
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
text-align: center;
font-size: 1rem;
2020-09-15 05:58:23 +03:00
min-height: 100vh;
width: 100vw;
position: absolute;
overflow: hidden;
background-size: cover;
background-position: 50% 50%:
2020-08-03 07:06:44 +03:00
`;
const STYLES_MIDDLE = css`
position: relative;
min-height: 10%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
2020-07-23 04:25:19 +03:00
flex-direction: column;
2020-08-03 07:06:44 +03:00
text-align: left;
padding: 24px;
`;
export default class SceneSignIn extends React.Component {
state = {
scene: "WELCOME",
username: "",
password: "",
loading: false,
2020-09-15 05:58:23 +03:00
usernameTaken: false,
};
_handleChange = (e) => {
if (e.target.name === "accepted" && e.target.value) {
const hash = Strings.generateRandomString();
const confirm = window.prompt(`Please type ${hash} to continue.`);
if (confirm !== hash) {
window.alert("Please try again.");
return;
}
}
this.setState({ [e.target.name]: e.target.value });
};
_handleUsernameChange = (e) => {
2020-09-12 08:14:18 +03:00
const value = Strings.createSlug(e.target.value, "");
2020-09-15 05:58:23 +03:00
this.setState({ [e.target.name]: value, usernameTaken: false });
};
_handleSubmit = async () => {
this.setState({ loading: true });
await Window.delay(100);
if (!this.state.accepted && this.state.scene === "CREATE_ACCOUNT") {
Events.dispatchMessage({
message: "You must accept the terms of service to create an account",
});
this.setState({ loading: false });
return;
}
if (!Validations.username(this.state.username)) {
Events.dispatchMessage({
message:
this.state.scene === "CREATE_ACCOUNT"
? "Usernames must between 1-48 characters and consist of only characters and numbers"
: "Invalid username",
2020-09-12 01:25:33 +03:00
});
this.setState({ loading: false });
return;
}
if (!Validations.password(this.state.password)) {
Events.dispatchMessage({
message:
this.state.scene === "CREATE_ACCOUNT"
? "Your password must be at least 8 characters"
: "Incorrect password",
2020-09-12 01:25:33 +03:00
});
this.setState({ loading: false });
2020-08-03 07:06:44 +03:00
return;
}
2020-09-15 05:58:23 +03:00
let response = null;
if (this.state.scene === "CREATE_ACCOUNT") {
response = await this.props.onCreateUser({
username: this.state.username.toLowerCase(),
password: this.state.password,
accepted: this.state.accepted,
2020-09-15 05:58:23 +03:00
});
} else {
response = await this.props.onAuthenticate({
username: this.state.username.toLowerCase(),
password: this.state.password,
});
}
if (Events.hasError(response)) {
2020-09-12 01:25:33 +03:00
this.setState({ loading: false });
}
};
_handleCheckUsername = async () => {
2020-09-15 05:58:23 +03:00
if (!this.state.username || !this.state.username.length) {
return;
}
if (!Validations.username(this.state.username)) {
Events.dispatchMessage({
message:
"Usernames must between 1-48 characters and consist of only characters and numbers",
});
return;
}
const response = await Actions.checkUsername({
username: this.state.username.toLowerCase(),
});
if (Events.hasError(response)) {
2020-09-13 05:53:20 +03:00
return;
2020-09-13 03:54:26 +03:00
}
if (response.data) {
2020-09-15 05:58:23 +03:00
//NOTE(martina): username taken
this.setState({ usernameTaken: true });
Events.dispatchMessage({ message: "That username is taken" });
2020-09-15 05:58:23 +03:00
return;
}
2020-09-15 05:58:23 +03:00
//NOTE(martina): username not taken
return this.setState({
2020-09-15 05:58:23 +03:00
usernameTaken: false,
});
};
render() {
2020-07-23 04:25:19 +03:00
return (
<div css={STYLES_ROOT}>
2020-11-01 21:40:03 +03:00
<WebsitePrototypeHeader style={{ background: `none` }} />
<div css={STYLES_MIDDLE}>
<SignIn {...this.props} />
</div>
2020-08-03 07:06:44 +03:00
<WebsitePrototypeFooter />
2020-07-23 04:25:19 +03:00
</div>
);
}
}