2020-07-21 08:45:15 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Actions from "~/common/actions";
|
|
|
|
import * as System from "~/components/system";
|
|
|
|
import * as Constants from "~/common/constants";
|
2021-01-11 06:57:40 +03:00
|
|
|
import * as Window from "~/common/window";
|
2020-07-23 11:57:44 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-09-02 11:33:39 +03:00
|
|
|
import * as Strings from "~/common/strings";
|
2020-11-28 07:39:01 +03:00
|
|
|
import * as Events from "~/common/custom-events";
|
2020-07-21 08:45:15 +03:00
|
|
|
|
2020-11-30 08:24:22 +03:00
|
|
|
import { css } from "@emotion/react";
|
2020-11-17 05:10:43 +03:00
|
|
|
import { SignIn } from "~/components/core/SignIn";
|
2020-07-21 08:45:15 +03:00
|
|
|
|
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%;
|
2020-07-21 08:45:15 +03:00
|
|
|
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;
|
2020-07-21 08:45:15 +03:00
|
|
|
padding: 24px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
export default class SceneSignIn extends React.Component {
|
|
|
|
state = {
|
2020-09-02 11:33:39 +03:00
|
|
|
scene: "WELCOME",
|
2020-07-21 08:45:15 +03:00
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
loading: false,
|
2020-09-15 05:58:23 +03:00
|
|
|
usernameTaken: false,
|
2020-07-21 08:45:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_handleChange = (e) => {
|
2020-10-29 22:48:45 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 08:45:15 +03:00
|
|
|
this.setState({ [e.target.name]: e.target.value });
|
|
|
|
};
|
|
|
|
|
2020-09-02 19:28:49 +03:00
|
|
|
_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 });
|
2020-09-02 19:28:49 +03:00
|
|
|
};
|
|
|
|
|
2020-07-21 08:45:15 +03:00
|
|
|
_handleSubmit = async () => {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
2021-01-11 06:57:40 +03:00
|
|
|
await Window.delay(100);
|
2020-07-22 08:53:29 +03:00
|
|
|
|
2020-10-29 22:48:45 +03:00
|
|
|
if (!this.state.accepted && this.state.scene === "CREATE_ACCOUNT") {
|
2020-11-28 07:39:01 +03:00
|
|
|
Events.dispatchMessage({
|
|
|
|
message: "You must accept the terms of service to create an account",
|
2020-10-29 22:48:45 +03:00
|
|
|
});
|
|
|
|
this.setState({ loading: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
if (!Validations.username(this.state.username)) {
|
2020-11-28 07:39:01 +03:00
|
|
|
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
|
|
|
});
|
2020-07-22 22:17:08 +03:00
|
|
|
this.setState({ loading: false });
|
2020-07-22 08:53:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
if (!Validations.password(this.state.password)) {
|
2020-11-28 07:39:01 +03:00
|
|
|
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
|
|
|
});
|
2020-09-02 11:33:39 +03:00
|
|
|
this.setState({ loading: false });
|
2020-08-03 07:06:44 +03:00
|
|
|
return;
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
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,
|
2020-10-29 22:48:45 +03:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2020-07-22 08:53:29 +03:00
|
|
|
|
2020-11-28 07:39:01 +03:00
|
|
|
if (Events.hasError(response)) {
|
2020-09-12 01:25:33 +03:00
|
|
|
this.setState({ loading: false });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
2020-07-21 08:45:15 +03:00
|
|
|
};
|
|
|
|
|
2020-09-02 11:33:39 +03:00
|
|
|
_handleCheckUsername = async () => {
|
2020-09-15 05:58:23 +03:00
|
|
|
if (!this.state.username || !this.state.username.length) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-02 11:33:39 +03:00
|
|
|
if (!Validations.username(this.state.username)) {
|
2020-11-28 07:39:01 +03:00
|
|
|
Events.dispatchMessage({
|
|
|
|
message:
|
|
|
|
"Usernames must between 1-48 characters and consist of only characters and numbers",
|
2020-09-13 01:08:36 +03:00
|
|
|
});
|
|
|
|
return;
|
2020-09-02 11:33:39 +03:00
|
|
|
}
|
2020-07-23 11:57:44 +03:00
|
|
|
|
2020-09-02 11:33:39 +03:00
|
|
|
const response = await Actions.checkUsername({
|
2020-09-02 19:28:49 +03:00
|
|
|
username: this.state.username.toLowerCase(),
|
2020-09-02 11:33:39 +03:00
|
|
|
});
|
|
|
|
|
2020-11-28 07:39:01 +03:00
|
|
|
if (Events.hasError(response)) {
|
2020-09-13 05:53:20 +03:00
|
|
|
return;
|
2020-09-13 03:54:26 +03:00
|
|
|
}
|
|
|
|
|
2020-09-02 11:33:39 +03:00
|
|
|
if (response.data) {
|
2020-09-15 05:58:23 +03:00
|
|
|
//NOTE(martina): username taken
|
|
|
|
this.setState({ usernameTaken: true });
|
2020-11-28 07:39:01 +03:00
|
|
|
Events.dispatchMessage({ message: "That username is taken" });
|
2020-09-15 05:58:23 +03:00
|
|
|
return;
|
2020-09-02 11:33:39 +03:00
|
|
|
}
|
|
|
|
|
2020-09-15 05:58:23 +03:00
|
|
|
//NOTE(martina): username not taken
|
2020-09-02 11:33:39 +03:00
|
|
|
return this.setState({
|
2020-09-15 05:58:23 +03:00
|
|
|
usernameTaken: false,
|
2020-09-02 11:33:39 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-07-23 04:25:19 +03:00
|
|
|
return (
|
2020-11-17 05:10:43 +03:00
|
|
|
<div css={STYLES_ROOT}>
|
2020-11-01 21:40:03 +03:00
|
|
|
<WebsitePrototypeHeader style={{ background: `none` }} />
|
2020-11-17 05:10:43 +03:00
|
|
|
<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>
|
|
|
|
);
|
2020-07-21 08:45:15 +03:00
|
|
|
}
|
|
|
|
}
|