slate/components/sidebars/SidebarCreateSlate.js

218 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-07-27 04:51:51 +03:00
import * as React from "react";
import * as Strings from "~/common/strings";
import * as Constants from "~/common/constants";
import * as System from "~/components/system";
2020-09-06 05:42:30 +03:00
import * as Validations from "~/common/validations";
2020-09-23 23:52:00 +03:00
import * as Actions from "~/common/actions";
2020-07-27 04:51:51 +03:00
2020-09-12 01:25:33 +03:00
import { dispatchCustomEvent } from "~/common/custom-events";
import { css } from "@emotion/core";
2020-09-12 01:25:33 +03:00
2020-09-12 02:50:29 +03:00
const SLATE_LIMIT = 50;
2020-09-09 13:01:49 +03:00
const STYLES_GROUP = css`
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
overflow-wrap: break-word;
white-space: pre-wrap;
margin-top: 8px;
`;
const STYLES_HEADER = css`
font-family: ${Constants.font.semiBold};
2020-09-16 23:39:35 +03:00
margin-top: 32px;
`;
2020-07-27 04:51:51 +03:00
export default class SidebarCreateSlate extends React.Component {
state = {
name: "",
public: true,
body: "",
2020-07-27 04:51:51 +03:00
loading: false,
};
_handleSubmit = async () => {
2020-09-09 13:01:49 +03:00
if (this.props.viewer.slates.length >= SLATE_LIMIT) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: {
2020-09-19 23:25:03 +03:00
alert: {
message: `You have reached the limit of ${SLATE_LIMIT} Slates!`,
},
2020-09-12 01:25:33 +03:00
},
});
2020-09-09 13:01:49 +03:00
return;
}
2020-07-27 04:51:51 +03:00
this.setState({ loading: true });
2020-09-06 05:42:30 +03:00
if (!Validations.slatename(this.state.name)) {
2020-09-12 01:25:33 +03:00
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message: "Please provide a name between 1-48 characters." },
2020-09-12 01:25:33 +03:00
},
});
2020-07-27 12:50:25 +03:00
this.setState({ loading: false });
2020-07-27 04:51:51 +03:00
return;
}
2020-11-20 07:03:30 +03:00
const response = await Actions.createSlate({
2020-07-27 04:51:51 +03:00
name: this.state.name,
public: this.state.public,
body: this.state.body,
2020-07-27 04:51:51 +03:00
});
2020-09-12 01:25:33 +03:00
if (!response) {
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
message: "We're having trouble connecting right now. Please try again later",
2020-09-12 01:25:33 +03:00
},
},
});
return;
}
if (response.error) {
dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
return;
2020-07-27 04:51:51 +03:00
}
2020-10-05 00:30:28 +03:00
if (this.props.sidebarData && this.props.sidebarData.files) {
2020-09-23 23:52:00 +03:00
let data = this.props.sidebarData.files.map((file) => {
return { title: file.title || file.name, ...file };
2020-09-23 23:52:00 +03:00
});
const addResponse = await Actions.addFileToSlate({
slate: response.slate,
data,
fromSlate: this.props.sidebarData.fromSlate,
2020-09-23 23:52:00 +03:00
});
if (!addResponse) {
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
message: "We're having trouble connecting right now. Please try again later",
2020-09-23 23:52:00 +03:00
},
},
});
return;
}
if (addResponse.error) {
dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
return;
}
const { added, skipped } = addResponse;
2020-10-31 02:12:20 +03:00
let message = Strings.formatAsUploadMessage(added, skipped, true);
if (message) {
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message, status: !added ? null : "INFO" },
},
});
}
2020-09-23 23:52:00 +03:00
}
2020-11-13 01:36:20 +03:00
await this.setState({ loading: false });
window.setTimeout(
() =>
this.props.onAction({
type: "NAVIGATE",
value: response.slate.id,
}),
200
2020-11-11 04:44:21 +03:00
);
2020-07-27 04:51:51 +03:00
};
_handleCancel = () => {
this.props.onCancel();
};
_handleChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
};
render() {
return (
<div>
2020-09-08 02:28:50 +03:00
<System.P
style={{
fontFamily: Constants.font.semiBold,
fontSize: Constants.typescale.lvl3,
2020-09-10 06:28:48 +03:00
marginBottom: "64px",
}}
>
Create slate
2020-07-27 04:51:51 +03:00
</System.P>
2020-09-16 23:39:35 +03:00
<System.P css={STYLES_HEADER}>Name</System.P>
2020-07-27 04:51:51 +03:00
<System.Input
name="name"
2020-09-10 06:28:48 +03:00
style={{ marginTop: 12 }}
placeholder="Slate name..."
2020-07-27 04:51:51 +03:00
value={this.state.name}
onChange={this._handleChange}
onSubmit={this._handleSubmit}
2020-07-27 04:51:51 +03:00
/>
2020-09-10 06:28:48 +03:00
<System.P style={{ marginTop: 12 }}>
Your slate URL will be:{" "}
<a
href={`${this.props.viewer.username}/${Strings.createSlug(this.state.name)}`}
target="_blank"
>
2020-09-10 06:28:48 +03:00
https://slate.host/
{this.props.viewer.username}/{Strings.createSlug(this.state.name)}
</a>
2020-07-27 04:51:51 +03:00
</System.P>
<System.P css={STYLES_HEADER}>Description</System.P>
<System.Textarea
style={{ marginTop: 12 }}
name="body"
value={this.state.body}
2020-10-05 00:30:28 +03:00
placeholder="Slate description..."
onChange={this._handleChange}
onSubmit={this._handleSubmit}
/>
<System.P css={STYLES_HEADER} style={{ marginTop: 48 }}>
Privacy
</System.P>
<div css={STYLES_GROUP}>
<System.P style={{ marginRight: 16 }}>
{this.state.public
? "Public. Anyone can search for and view this slate."
: "Private. Only you can view this slate."}
</System.P>
<System.Toggle name="public" onChange={this._handleChange} active={this.state.public} />
</div>
2020-08-02 22:17:13 +03:00
<System.ButtonPrimary
full
2020-07-27 04:51:51 +03:00
style={{ marginTop: 48 }}
onClick={this._handleSubmit}
loading={this.state.loading}
>
2020-07-27 04:51:51 +03:00
Create {this.state.name}
2020-08-02 22:17:13 +03:00
</System.ButtonPrimary>
2020-07-27 04:51:51 +03:00
</div>
);
}
}