slate/components/sidebars/SidebarCreateSlate.js

258 lines
7.2 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";
import * as Events from "~/common/custom-events";
2020-12-11 05:59:17 +03:00
import * as SVG from "~/common/svg";
import * as UserBehaviors from "~/common/user-behaviors";
2020-07-27 04:51:51 +03:00
2020-12-11 05:59:17 +03:00
import { RadioGroup } from "~/components/system/components/RadioGroup";
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
2021-05-06 03:08:14 +03:00
import { Link } from "~/components/core/Link";
2020-09-12 01:25:33 +03:00
2020-12-11 05:59:17 +03:00
const STYLES_TEXT = css`
2021-07-07 22:24:01 +03:00
color: ${Constants.semantic.textGray};
2020-12-11 05:59:17 +03:00
font-size: ${Constants.typescale.lvl0};
`;
const STYLES_HEADER = css`
font-family: ${Constants.font.semiBold};
2020-12-11 05:59:17 +03:00
`;
const STYLES_GROUPING = css`
width: 100%;
border: 1px solid rgba(196, 196, 196, 0.5);
background-color: ${Constants.system.white};
border-radius: 6px;
padding: 16px;
margin-bottom: 24px;
`;
2020-07-27 04:51:51 +03:00
export default class SidebarCreateSlate extends React.Component {
state = {
name: "",
isPublic: true,
body: "",
tags: [],
suggestions: this.props.viewer?.tags || [],
2020-07-27 04:51:51 +03:00
loading: false,
};
componentDidMount = () => {
this.updateSuggestions();
};
updateSuggestions = () => {
let newSuggestions = new Set([...this.state.suggestions, ...this.state.tags]);
this.setState({ suggestions: Array.from(newSuggestions) });
};
2020-07-27 04:51:51 +03:00
_handleSubmit = async () => {
this.setState({ loading: true });
2020-09-06 05:42:30 +03:00
if (!Validations.slatename(this.state.name)) {
Events.dispatchMessage({ message: "Please provide a name between 1-48 characters." });
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,
isPublic: this.state.isPublic,
body: this.state.body,
tags: this.state.tags,
2020-07-27 04:51:51 +03:00
});
console.log(response);
2020-07-27 04:51:51 +03:00
if (Events.hasError(response)) {
2021-01-14 00:00:15 +03:00
this.setState({ loading: false });
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) {
2021-06-01 02:41:00 +03:00
await UserBehaviors.saveCopy({
2020-09-23 23:52:00 +03:00
slate: response.slate,
files: this.props.sidebarData.files,
2020-09-23 23:52:00 +03:00
});
2021-06-01 02:41:00 +03:00
// if (Events.hasError(addResponse)) {
// this.setState({ loading: false });
// return;
// }
2020-09-23 23:52:00 +03:00
2021-06-01 02:41:00 +03:00
// const { added, skipped } = addResponse;
// let message = Strings.formatAsUploadMessage(added, skipped, true);
// if (message) {
// Events.dispatchMessage({ message, status: !added ? null : "INFO" });
// }
2020-09-23 23:52:00 +03:00
}
this.setState({ loading: false });
2020-11-13 01:36:20 +03:00
window.setTimeout(
() =>
this.props.onAction({
type: "NAVIGATE",
2021-05-06 03:08:14 +03:00
href: `/$/slate/${response.slate.id}`,
2020-11-13 01:36:20 +03:00
}),
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 }, () => {
if (e.target.name === "tags") {
this.updateSuggestions();
}
});
2020-07-27 04:51:51 +03:00
};
render() {
2020-12-11 05:59:17 +03:00
const slug = Strings.createSlug(this.state.name);
const url = `/${this.props.viewer.username}/${slug}`;
2020-07-27 04:51:51 +03:00
return (
<div>
2021-07-07 23:50:57 +03:00
<System.P1
2020-09-08 02:28:50 +03:00
style={{
fontFamily: Constants.font.semiBold,
fontSize: Constants.typescale.lvl3,
2020-12-11 05:59:17 +03:00
marginBottom: 36,
}}
>
Create collection
2021-07-07 23:50:57 +03:00
</System.P1>
2020-07-27 04:51:51 +03:00
2020-12-11 05:59:17 +03:00
<div css={STYLES_GROUPING}>
2021-07-07 23:50:57 +03:00
<System.P1 css={STYLES_HEADER}>Name</System.P1>
<System.P1
2020-12-11 05:59:17 +03:00
css={STYLES_TEXT}
style={{
marginTop: 12,
}}
>
Give your collection a name so you and others can find it on Slate and on the web.
2021-07-07 23:50:57 +03:00
</System.P1>
2020-12-11 05:59:17 +03:00
<System.Input
autoFocus
placeholder="Collection name..."
2020-12-11 05:59:17 +03:00
style={{ marginTop: 12 }}
name="name"
value={this.state.name}
onChange={this._handleChange}
onSubmit={this._handleSubmit}
descriptionStyle={{ fontSize: "20px !important" }}
labelStyle={{ fontSize: "20px" }}
/>
2021-07-07 23:50:57 +03:00
<System.P1
2020-12-11 05:59:17 +03:00
style={{
marginTop: 12,
2021-07-07 22:24:01 +03:00
color: Constants.semantic.textGrayLight,
2020-12-11 05:59:17 +03:00
fontSize: Constants.typescale.lvl0,
}}
>
https://slate.host{url}
2021-07-07 23:50:57 +03:00
</System.P1>
2020-12-11 05:59:17 +03:00
</div>
2020-07-27 04:51:51 +03:00
2020-12-11 05:59:17 +03:00
<div css={STYLES_GROUPING}>
2021-07-07 23:50:57 +03:00
<System.P1 css={STYLES_HEADER}>Description</System.P1>
<System.P1
2020-12-11 05:59:17 +03:00
css={STYLES_TEXT}
style={{
marginTop: 12,
}}
>
Give your collection a description, add links, and connect it to other collections.
2021-07-07 23:50:57 +03:00
</System.P1>
2020-12-11 05:59:17 +03:00
<System.Textarea
style={{ marginTop: 12 }}
placeholder="Collection description..."
2020-12-11 05:59:17 +03:00
name="body"
value={this.state.body}
onChange={this._handleChange}
onSubmit={this._handleSubmit}
/>
</div>
<div css={STYLES_GROUPING}>
2021-07-07 23:50:57 +03:00
<System.P1 css={STYLES_HEADER}>Tags</System.P1>
<System.P1
css={STYLES_TEXT}
style={{
marginTop: 12,
}}
>
Add tags to a collection to categorize it.
2021-07-07 23:50:57 +03:00
</System.P1>
<System.Tag
name="tags"
placeholder={`Edit tags for ${this.state.name ? this.state.name : "this collection"}`}
tags={this.state.tags}
suggestions={this.state.suggestions}
style={{ marginTop: 12 }}
onChange={this._handleChange}
/>
</div>
2020-12-11 05:59:17 +03:00
<div css={STYLES_GROUPING}>
2021-07-07 23:50:57 +03:00
<System.P1 css={STYLES_HEADER} style={{ marginBottom: 12 }}>
2020-12-11 05:59:17 +03:00
Privacy
2021-07-07 23:50:57 +03:00
</System.P1>
<System.P1
2020-12-11 05:59:17 +03:00
css={STYLES_TEXT}
style={{
marginTop: 12,
}}
>
Public collections can be discovered and seen by anyone on the internet. If you make it
private, only you will be able to see it.
2021-07-07 23:50:57 +03:00
</System.P1>
2020-12-11 05:59:17 +03:00
<RadioGroup
name="isPublic"
2020-12-11 05:59:17 +03:00
options={[
{
value: true,
label: (
<div style={{ display: "flex", alignItems: "center" }}>
<SVG.Globe height="16px" style={{ marginRight: 8 }} />
Public
</div>
),
},
{
value: false,
label: (
<div style={{ display: "flex", alignItems: "center" }}>
<SVG.SecurityLock height="16px" style={{ marginRight: 8 }} />
Private
</div>
),
},
]}
style={{ marginTop: 12 }}
labelStyle={{ fontFamily: Constants.font.medium }}
selected={this.state.isPublic}
2020-12-11 05:59:17 +03:00
onChange={this._handleChange}
/>
</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>
);
}
}