slate/components/sidebars/SidebarAddFileToSlate.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-09-23 23:52:00 +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";
import * as Actions from "~/common/actions";
import * as UserBehaviors from "~/common/user-behaviors";
2020-09-23 23:52:00 +03:00
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
import { ButtonPrimary, ButtonDisabled } from "~/components/system/components/Buttons";
2020-09-27 07:43:25 +03:00
import { SlatePicker } from "~/components/core/SlatePicker";
2020-09-23 23:52:00 +03:00
const STYLES_HEADER = css`
font-family: ${Constants.font.semiBold};
margin-top: 32px;
margin-bottom: 16px;
`;
export default class SidebarAddFileToSlate extends React.Component {
state = {
selected: {},
};
componentDidMount = () => {
if (!this.props.sidebarData || !this.props.sidebarData.files) {
this.props.onCancel();
}
};
2020-09-23 23:52:00 +03:00
_handleCreateSlate = async () => {
if (
Object.values(this.state.selected).some((value) => {
return !!value;
})
) {
await this._handleSubmit();
}
await this.props.onCancel();
this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_CREATE_SLATE",
data: this.props.sidebarData,
});
};
_handleAdd = (slate) => {
if (this.state.selected[slate.id]) {
this.setState({
selected: { ...this.state.selected, [slate.id]: false },
});
} else {
this.setState({
selected: { ...this.state.selected, [slate.id]: slate },
});
}
};
_handleSubmit = async () => {
2020-12-15 04:43:16 +03:00
this.props.onCancel();
2020-09-23 23:52:00 +03:00
for (let slate of Object.values(this.state.selected)) {
if (!slate) continue;
2021-06-01 02:41:00 +03:00
await UserBehaviors.saveCopy({
2020-10-05 00:30:28 +03:00
slate,
files: this.props.sidebarData.files,
2020-10-05 00:30:28 +03:00
});
2020-09-23 23:52:00 +03:00
}
};
render() {
return (
<div>
2021-07-07 23:50:57 +03:00
<System.P1
2020-09-23 23:52:00 +03:00
style={{
fontFamily: Constants.font.semiBold,
fontSize: Constants.typescale.lvl3,
2020-12-11 05:59:17 +03:00
marginBottom: 36,
2020-09-23 23:52:00 +03:00
}}
>
Add files to collection
2021-07-07 23:50:57 +03:00
</System.P1>
2020-09-23 23:52:00 +03:00
2021-07-07 23:50:57 +03:00
<System.P1 css={STYLES_HEADER}>Slates</System.P1>
2020-09-27 07:43:25 +03:00
<SlatePicker
slates={this.props.viewer.slates}
selected={this.state.selected}
onAdd={this._handleAdd}
onCreateSlate={this._handleCreateSlate}
/>
{Object.keys(this.state.selected).length ? (
<ButtonPrimary full onClick={this._handleSubmit} style={{ marginTop: 32 }}>
Add to collections
</ButtonPrimary>
) : (
<ButtonDisabled full style={{ marginTop: 32 }}>
Add to collections
</ButtonDisabled>
)}
2020-09-23 23:52:00 +03:00
</div>
);
}
}