slate/scenes/SceneSlate.js

277 lines
7.3 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as System from "~/components/system";
import * as Actions from "~/common/actions";
2020-08-07 09:33:44 +03:00
import * as Constants from "~/common/constants";
import * as SVG from "~/components/system/svg";
import { css } from "@emotion/react";
import ScenePage from "~/components/core/ScenePage";
2020-08-22 07:25:34 +03:00
import ScenePageHeader from "~/components/core/ScenePageHeader";
2020-08-22 20:30:03 +03:00
import Slate, { generateLayout } from "~/components/core/Slate";
import SlateMediaObject from "~/components/core/SlateMediaObject";
import CircleButtonLight from "~/components/core/CircleButtonLight";
2020-08-07 09:33:44 +03:00
2020-08-22 12:32:40 +03:00
const moveIndex = (set, fromIndex, toIndex) => {
const element = set[fromIndex];
set.splice(fromIndex, 1);
set.splice(toIndex, 0, element);
return set;
};
export default class SceneSlate extends React.Component {
state = {
slatename: this.props.current.slatename,
public: this.props.current.data.public,
2020-08-07 05:34:33 +03:00
objects: this.props.current.data.objects,
2020-08-22 08:19:11 +03:00
body: this.props.current.data.body,
2020-08-22 20:30:03 +03:00
layouts: this.props.current.data.layouts
? this.props.current.data.layouts
: { lg: generateLayout(this.props.current.data.objects) },
loading: false,
};
componentDidMount() {
this._handleUpdateCarousel(this.state);
}
componentDidUpdate(prevProps) {
2020-08-22 20:30:03 +03:00
const isNewSlateScene = prevProps.current.slatename !== this.props.current.slatename;
2020-08-22 08:19:11 +03:00
let isUpdated = false;
2020-08-22 20:30:03 +03:00
if (this.props.current.data.objects.length !== prevProps.current.data.objects.length) {
2020-08-22 08:19:11 +03:00
isUpdated = true;
}
if (this.props.current.data.body !== prevProps.current.data.body) {
isUpdated = true;
}
if (isNewSlateScene || isUpdated) {
let layouts = this.props.current.data.layouts;
if (!layouts) {
layouts = { lg: generateLayout(this.props.current.data.objects) };
}
this.setState({
slatename: this.props.current.slatename,
public: this.props.current.data.public,
2020-08-07 05:34:33 +03:00
objects: this.props.current.data.objects,
2020-08-22 08:19:11 +03:00
body: this.props.current.data.body,
layouts: layouts,
loading: false,
});
this._handleUpdateCarousel({
objects: this.props.current.data.objects,
});
}
}
2020-08-22 20:30:03 +03:00
_handleChangeLayout = async (layout, layouts) => {
this.setState({ layouts });
};
_handleSaveLayout = async () => {
await this._handleSave(null, null, this.state.layouts);
};
2020-08-22 12:32:40 +03:00
_handleMoveIndex = async (from, to) => {
const objects = moveIndex(this.state.objects, from.index, to.index);
this.setState({ objects });
await this._handleSave(null, objects);
};
2020-08-22 20:30:03 +03:00
_handleSave = async (e, objects, layouts) => {
this.setState({ loading: true });
const response = await Actions.updateSlate({
id: this.props.current.slateId,
slatename: this.state.slatename,
data: {
objects: objects ? objects : this.state.objects,
2020-08-22 20:30:03 +03:00
layouts: layouts ? layouts : this.state.layouts,
public: this.state.public,
2020-08-22 08:19:11 +03:00
body: this.state.body,
},
});
if (!response) {
this.setState({ loading: false });
alert("TODO: Server Error");
}
if (response.error) {
this.setState({ loading: false });
alert(`TODO: ${response.decorator}`);
}
await this.props.onRehydrate();
this._handleUpdateCarousel(this.state);
};
_handleObjectSave = async (object) => {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { saving: true },
});
const objects = [...this.state.objects];
for (let i = 0; i < objects.length; i++) {
if (objects[i].id === object.id) {
objects[i] = object;
break;
}
}
this.setState({ objects });
await this._handleSave(null, objects);
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { saving: false },
});
};
_handleUpdateCarousel = (state) => {
System.dispatchCustomEvent({
name: "slate-global-create-carousel",
detail: {
slides: state.objects.map((each) => {
const cid = each.url.replace("https://hub.textile.io/ipfs/", "");
return {
onDelete: this._handleDelete,
onObjectSave: this._handleObjectSave,
id: each.id,
cid,
data: each,
2020-08-22 20:30:03 +03:00
component: <SlateMediaObject key={each.id} useImageFallback data={each} />,
};
}),
},
});
};
2020-08-07 05:17:49 +03:00
_handleDelete = async (id) => {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: true },
});
2020-08-07 05:17:49 +03:00
let index;
const objects = this.state.objects.filter((o, i) => {
if (o.id === id) {
index = i;
}
return o.id !== id;
});
const keys = Object.keys(this.state.layouts);
let layouts = this.state.layouts;
for (let j = 0; j < keys.length; j++) {
layouts[keys[j]] = layouts[keys[j]].filter((each, i) => {
return i !== index;
});
}
const response = await Actions.updateSlate({
2020-08-07 05:17:49 +03:00
id: this.props.current.slateId,
slatename: this.state.slatename,
data: {
objects,
layouts,
public: this.state.public,
2020-08-22 20:30:03 +03:00
body: this.state.body,
},
2020-08-07 05:17:49 +03:00
});
if (!response) {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: false },
});
2020-08-07 05:17:49 +03:00
alert("TODO: Server Error");
}
if (response.error) {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: false },
});
2020-08-07 05:17:49 +03:00
alert(`TODO: ${response.decorator}`);
}
await this.props.onRehydrate();
2020-08-07 05:17:49 +03:00
this._handleUpdateCarousel(this.state);
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: false },
});
};
_handleSelect = (index) =>
System.dispatchCustomEvent({
name: "slate-global-open-carousel",
detail: { index },
});
2020-07-27 11:33:39 +03:00
_handleAdd = () => {
return this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_ADD_FILE_TO_BUCKET",
data: this.props.current,
});
};
2020-07-27 11:33:39 +03:00
_handleShowSettings = () => {
return this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_SINGLE_SLATE_SETTINGS",
data: this.props.current,
});
};
2020-07-27 11:33:39 +03:00
render() {
2020-08-22 08:19:11 +03:00
const { slatename, objects, body = "A slate." } = this.state;
console.log(this.state.layouts);
2020-07-27 11:33:39 +03:00
return (
<ScenePage style={{ padding: `88px 24px 128px 24px` }}>
2020-08-22 07:25:34 +03:00
<ScenePageHeader
style={{ padding: `0 24px 0 24px` }}
title={slatename}
actions={
<React.Fragment>
2020-08-22 20:30:03 +03:00
<CircleButtonLight onClick={this._handleAdd} style={{ marginLeft: 12, marginRight: 12 }}>
2020-08-22 07:25:34 +03:00
<SVG.Plus height="16px" />
</CircleButtonLight>
<CircleButtonLight onClick={this._handleShowSettings}>
<SVG.Settings height="16px" />
</CircleButtonLight>
</React.Fragment>
2020-08-22 20:30:03 +03:00
}>
2020-08-22 08:19:11 +03:00
{body}
2020-08-22 07:25:34 +03:00
</ScenePageHeader>
2020-08-22 12:32:40 +03:00
<Slate
2020-08-22 20:30:03 +03:00
key={slatename}
2020-08-22 12:32:40 +03:00
editing
items={objects}
2020-08-22 20:30:03 +03:00
layouts={this.state.layouts}
onLayoutChange={this._handleChangeLayout}
onLayoutSave={this._handleSaveLayout}
2020-08-22 12:32:40 +03:00
onMoveIndex={this._handleMoveIndex}
onSelect={this._handleSelect}
/>
2020-07-27 11:33:39 +03:00
</ScenePage>
);
}
}