slate/scenes/SceneEditAccount.js

427 lines
12 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as System from "~/components/system";
import * as Actions from "~/common/actions";
import * as Constants from "~/common/constants";
import * as Strings from "~/common/strings";
import * as Validations from "~/common/validations";
2020-08-20 08:29:33 +03:00
import * as FileUtilities from "~/common/file-utilities";
import { css } from "@emotion/react";
2020-09-12 01:25:33 +03:00
import { dispatchCustomEvent } from "~/common/custom-events";
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: "",
2020-08-22 08:45:50 +03:00
body: this.props.viewer.data.body,
photo: this.props.viewer.data.photo,
name: this.props.viewer.data.name,
deleting: false,
2020-09-29 00:30:12 +03:00
allow_filecoin_directory_listing: this.props.viewer
.allow_filecoin_directory_listing,
allow_automatic_data_storage: this.props.viewer
.allow_automatic_data_storage,
allow_encrypted_data_storage: this.props.viewer
.allow_encrypted_data_storage,
changingPassword: false,
changingUsername: false,
changingAvatar: false,
changingFilecoin: false,
};
_handleUpload = async (e) => {
this.setState({ changingAvatar: true });
e.persist();
let file = e.target.files[0];
if (!file) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
message: "Something went wrong with the upload. Please try again",
},
},
});
return;
}
2020-09-08 08:08:10 +03:00
// NOTE(jim): Only allow images for account avatar.
if (!file.type.startsWith("image/")) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message: "Upload failed. Only images and gifs are allowed" },
},
});
return;
}
2020-09-23 23:54:43 +03:00
const response = await FileUtilities.upload({ file });
2020-09-23 23:54:43 +03:00
if (!response) {
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message: "We're having trouble connecting right now" },
},
});
this.setState({ changingAvatar: false });
return;
}
if (response.error) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: json.decorator } },
});
this.setState({ changingAvatar: false });
return;
}
2020-09-23 23:54:43 +03:00
const { json } = response;
const cid = json.data.ipfs.replace("/ipfs/", "");
const url = Strings.getCIDGatewayURL(cid);
await Actions.updateViewer({
2020-08-22 08:45:50 +03:00
data: {
photo: Strings.getCIDGatewayURL(cid),
2020-08-22 08:45:50 +03:00
body: this.state.body,
name: this.state.name,
2020-08-22 08:45:50 +03:00
},
});
await this.props.onRehydrate();
this.setState({ changingAvatar: false, photo: url });
};
2020-08-22 08:45:50 +03:00
_handleSaveBio = async (e) => {
this.setState({ changingBio: true });
await Actions.updateViewer({
data: {
photo: this.state.photo,
body: this.state.body,
name: this.state.name,
2020-08-22 08:45:50 +03:00
},
});
await this.props.onRehydrate();
this.setState({ changingBio: false });
};
_handleSaveFilecoin = async (e) => {
this.setState({ changingFilecoin: true });
await Actions.updateViewer({
data: {
photo: this.state.photo,
body: this.state.body,
name: this.state.name,
2020-09-29 00:30:12 +03:00
allow_filecoin_directory_listing: this.state
.allow_filecoin_directory_listing,
allow_automatic_data_storage: this.state.allow_automatic_data_storage,
allow_encrypted_data_storage: this.state.allow_encrypted_data_storage,
},
});
await this.props.onRehydrate();
this.setState({ changingFilecoin: false });
};
_handleSave = async (e) => {
this.setState({ changingUsername: true });
if (!Validations.username(this.state.username)) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
message: "Please include only letters and numbers in your username",
},
},
});
this.setState({ changingUsername: false });
return;
}
await Actions.updateViewer({
username: this.state.username,
2020-08-22 08:45:50 +03:00
data: {
photo: this.state.photo,
body: this.state.body,
name: this.state.name,
2020-08-22 08:45:50 +03:00
},
});
await this.props.onRehydrate();
this.setState({ changingUsername: false });
};
_handleUsernameChange = (e) => {
e.persist();
this.setState({ [e.target.name]: e.target.value.toLowerCase() });
};
_handleChangePassword = async (e) => {
this.setState({ changingPassword: true });
if (this.state.password !== this.state.confirm) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: { alert: { message: "Passwords did not match" } },
});
this.setState({ changingPassword: false });
return;
}
if (!Validations.password(this.state.password)) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message: "Password length must be more than 8 characters" },
},
});
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();
this.setState({ deleting: false });
};
_handleChange = (e) => {
e.persist();
this.setState({ [e.target.name]: e.target.value });
};
_handleCheckboxChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
};
render() {
const profileURL = `https://slate.host/${this.state.username}`;
console.log(this.props.viewer);
2020-07-23 04:25:19 +03:00
return (
<ScenePage>
<System.H1>Account settings</System.H1>
<System.DescriptionGroup
style={{ marginTop: 48 }}
label="Avatar image"
description="This image will appear in various lists."
/>
2020-08-22 08:45:50 +03:00
<Avatar
style={{ marginTop: 24 }}
size={256}
url={this.props.viewer.data.photo}
/>
<div style={{ marginTop: 24 }}>
2020-08-22 08:45:50 +03:00
<input
css={STYLES_FILE_HIDDEN}
type="file"
id="file"
onChange={this._handleUpload}
/>
<System.ButtonPrimary
style={{ margin: "0 16px 16px 0" }}
type="label"
htmlFor="file"
2020-08-22 08:45:50 +03:00
loading={this.state.changingAvatar}
>
Pick avatar
</System.ButtonPrimary>
</div>
<System.DescriptionGroup
style={{ marginTop: 48 }}
label="Allow Slate to make Filecoin archive storage deals on your behalf"
description="If this box is checked, then we will make Filecoin archive storage deals on your behalf. By default these storage deals are not encrypted and anyone can retrieve them from the Filecoin Network."
/>
<System.CheckBox
style={{ marginTop: 48 }}
2020-09-29 00:30:12 +03:00
name="allow_filecoin_directory_listing"
value={this.state.allow_filecoin_directory_listing}
onChange={this._handleCheckboxChange}
>
Show your successful deals on a directory page where others can
retrieve them.
</System.CheckBox>
<System.CheckBox
style={{ marginTop: 24 }}
name="allow_automatic_data_storage"
value={this.state.allow_automatic_data_storage}
onChange={this._handleCheckboxChange}
>
Allow Slate to make archive storage deals on your behalf to the
Filecoin Network. You will get a receipt in the Filecoin section.
</System.CheckBox>
<System.CheckBox
style={{ marginTop: 24 }}
name="allow_encrypted_data_storage"
value={this.state.allow_encrypted_data_storage}
onChange={this._handleCheckboxChange}
>
Force encryption on archive storage deals (only you can see retrieved
data from the Filecoin network).
</System.CheckBox>
<div style={{ marginTop: 24 }}>
<System.ButtonPrimary
onClick={this._handleSaveFilecoin}
loading={this.state.changingFilecoin}
>
Save archiving settings
</System.ButtonPrimary>
</div>
<System.Input
containerStyle={{ marginTop: 64 }}
label="Username"
2020-07-23 04:25:19 +03:00
description={
<React.Fragment>
This is your username on Slate. Your username is unique and used
for your profile URL{" "}
2020-07-23 04:25:19 +03:00
<a href={profileURL} target="_blank">
{profileURL}
</a>
</React.Fragment>
}
name="username"
value={this.state.username}
placeholder="Username"
onChange={this._handleUsernameChange}
/>
<div style={{ marginTop: 24 }}>
2020-08-22 08:45:50 +03:00
<System.ButtonPrimary
onClick={this._handleSave}
loading={this.state.changingUsername}
>
Change username
</System.ButtonPrimary>
</div>
<System.Input
containerStyle={{ marginTop: 48 }}
label="Name"
description={`This is how your name will be publicly shown.`}
name="name"
value={this.state.name}
placeholder="Your name"
onChange={this._handleChange}
/>
<System.DescriptionGroup label="Bio" style={{ marginTop: 24 }} />
2020-08-22 08:45:50 +03:00
<System.Textarea
style={{ marginTop: 24 }}
label="Bio"
name="body"
value={this.state.body}
placeholder="A user on Slate."
onChange={this._handleChange}
/>
<div style={{ marginTop: 24 }}>
<System.ButtonPrimary
onClick={this._handleSaveBio}
loading={this.state.changingBio}
>
Update information
2020-08-22 08:45:50 +03:00
</System.ButtonPrimary>
</div>
<System.DescriptionGroup
style={{ marginTop: 48 }}
label="Reset password"
description="Your new password must be a minimum of eight characters."
/>
<System.Input
containerStyle={{ marginTop: 24 }}
label="New password"
name="password"
type="password"
value={this.state.password}
placeholder="Your new password"
onChange={this._handleChange}
/>
<System.Input
containerStyle={{ marginTop: 24 }}
label="Confirm password"
name="confirm"
type="password"
value={this.state.confirm}
placeholder="Confirm it!"
onChange={this._handleChange}
/>
<div style={{ marginTop: 24 }}>
2020-08-22 08:45:50 +03:00
<System.ButtonPrimary
onClick={this._handleChangePassword}
loading={this.state.changingPassword}
>
Change password
</System.ButtonPrimary>
</div>
<System.DescriptionGroup
style={{ marginTop: 48 }}
label="Delete your account"
description="If you choose to delete your account you will lose your Textile Hub and Powergate key. Make sure you back those up before deleting your account."
/>
<div style={{ marginTop: 24 }}>
2020-08-22 08:45:50 +03:00
<System.ButtonPrimary
onClick={this._handleDelete}
loading={this.state.deleting}
>
Delete my account
</System.ButtonPrimary>
</div>
</ScenePage>
);
}
}