import * as React from "react"; import * as System from "~/components/system"; import * as Actions from "~/common/actions"; import * as Validations from "~/common/validations"; import * as FileUtilities from "~/common/file-utilities"; import { css } from "@emotion/react"; import ScenePage from "~/components/core/ScenePage"; import Avatar from "~/components/core/Avatar"; const STYLES_FILE_HIDDEN = css` height: 1px; width: 1px; opacity: 0; visibility: hidden; position: fixed; top: -1px; left: -1px; `; const delay = (time) => new Promise((resolve) => setTimeout(() => { resolve(); }, time) ); export default class SceneEditAccount extends React.Component { state = { username: this.props.viewer.username, password: "", confirm: "", body: this.props.viewer.data.body, photo: this.props.viewer.data.photo, deleting: false, changingPassword: false, changingUsername: false, changingAvatar: false, }; _handleUpload = async (e) => { this.setState({ changingAvatar: true }); e.persist(); let file = e.target.files[0]; if (!file) { alert("TODO: Something went wrong"); return; } if (!file.type.startsWith("image/")) { alert("TODO: Error message for not an image."); return; } const json = await FileUtilities.upload({ file }); if (json.error) { alert("TODO: Image already exists in bucket error message"); this.setState({ changingAvatar: false }); return; } await Actions.updateViewer({ data: { photo: `https://hub.textile.io${json.data.ipfs}`, body: this.state.body, }, }); await this.props.onRehydrate(); this.setState({ changingAvatar: false }); }; _handleSaveBio = async (e) => { this.setState({ changingBio: true }); await Actions.updateViewer({ data: { photo: this.state.photo, body: this.state.body, }, }); await this.props.onRehydrate(); this.setState({ changingBio: false }); }; _handleSave = async (e) => { this.setState({ changingUsername: true }); if (!Validations.username(this.state.username)) { alert("TODO: Not a valid username"); this.setState({ changingUsername: false }); return; } await Actions.updateViewer({ username: this.state.username, data: { photo: this.state.photo, body: this.state.body, }, }); await this.props.onRehydrate(); this.setState({ changingUsername: false }); }; _handleChangePassword = async (e) => { this.setState({ changingPassword: true }); if (this.state.password !== this.state.confirm) { alert("TODO: Error message for non-matching passwords"); this.setState({ changingPassword: false }); return; } if (!Validations.password(this.state.password)) { alert("TODO: Not a valid password"); this.setState({ changingPassword: false }); return; } await Actions.updateViewer({ type: "CHANGE_PASSWORD", password: this.state.password, }); this.setState({ changingPassword: false, password: "", confirm: "" }); }; _handleDelete = async (e) => { this.setState({ deleting: true }); await delay(100); const response = await this.props.onDeleteYourself(); if (!response) { this.setState({ deleting: false }); } }; _handleChange = (e) => { e.persist(); this.setState({ [e.target.name]: e.target.value }); }; render() { const profileURL = `https://slate.host/${this.state.username}`; return ( Profile & account settings
Pick avatar
This is your username on Slate. Your username is used for your profile URL{" "} {profileURL} } name="username" value={this.state.username} placeholder="Name" onChange={this._handleChange} />
Change username
Change bio
Change password
Delete my account
); } }