2020-07-27 06:20:34 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as System from "~/components/system";
|
2020-08-03 02:29:11 +03:00
|
|
|
import * as Actions from "~/common/actions";
|
2020-08-07 09:33:44 +03:00
|
|
|
import * as Constants from "~/common/constants";
|
2020-08-09 11:08:46 +03:00
|
|
|
import * as SVG from "~/components/system/svg";
|
2020-07-27 06:20:34 +03:00
|
|
|
|
|
|
|
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";
|
2020-08-20 23:33:08 +03:00
|
|
|
import SlateMediaObject from "~/components/core/SlateMediaObject";
|
2020-08-27 07:27:50 +03:00
|
|
|
import CircleButtonGray from "~/components/core/CircleButtonGray";
|
2020-08-07 09:33:44 +03:00
|
|
|
|
2020-08-31 21:19:46 +03:00
|
|
|
const STYLES_USERNAME = css`
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
:hover {
|
|
|
|
color: ${Constants.system.brand};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-09-02 21:54:43 +03:00
|
|
|
// TODO(jim + martina):
|
|
|
|
// Sub state for scenes and sidebars is getting out of hand.
|
|
|
|
// I don't want some crazy global solution, so lets think of a rudimentry
|
|
|
|
// way to keep things sane
|
2020-09-03 00:04:47 +03:00
|
|
|
const setStateData = (source) => {
|
2020-09-02 21:54:43 +03:00
|
|
|
return {
|
|
|
|
name: source.data.name,
|
|
|
|
username: source.owner ? source.owner.username : null,
|
|
|
|
slatename: source.slatename,
|
|
|
|
public: source.data.public,
|
|
|
|
objects: source.data.objects,
|
|
|
|
body: source.data.body,
|
|
|
|
layouts: source.data.layouts
|
|
|
|
? source.data.layouts
|
|
|
|
: { lg: generateLayout(source.data.objects) },
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-27 06:20:34 +03:00
|
|
|
export default class SceneSlate extends React.Component {
|
2020-08-03 02:29:11 +03:00
|
|
|
state = {
|
2020-09-02 21:54:43 +03:00
|
|
|
...setStateData(this.props.data, this.props.viewer),
|
2020-08-03 02:29:11 +03:00
|
|
|
loading: false,
|
2020-09-03 00:04:47 +03:00
|
|
|
saving: "IDLE",
|
|
|
|
editing: this.props.data.data.ownerId === this.props.viewer.id,
|
2020-08-03 02:29:11 +03:00
|
|
|
};
|
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
componentDidMount() {
|
|
|
|
this._handleUpdateCarousel(this.state);
|
|
|
|
}
|
|
|
|
|
2020-08-03 02:29:11 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
2020-09-02 21:54:43 +03:00
|
|
|
const updated =
|
|
|
|
this.props.current.updated_at !== prevProps.current.updated_at;
|
2020-08-22 08:19:11 +03:00
|
|
|
|
2020-09-03 00:04:47 +03:00
|
|
|
if (!updated) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-09 11:08:46 +03:00
|
|
|
|
2020-09-03 00:04:47 +03:00
|
|
|
let editing;
|
2020-08-23 23:09:38 +03:00
|
|
|
|
2020-09-03 00:04:47 +03:00
|
|
|
this.props.viewer.slates.forEach((slate) => {
|
|
|
|
if (slate.id === this.props.current.slateId) {
|
|
|
|
editing = true;
|
|
|
|
}
|
|
|
|
});
|
2020-08-20 23:33:08 +03:00
|
|
|
|
2020-09-03 00:04:47 +03:00
|
|
|
this.setState({
|
|
|
|
...setStateData(this.props.current, this.props.viewer),
|
|
|
|
loading: false,
|
|
|
|
saving: "SAVED",
|
|
|
|
editing,
|
|
|
|
});
|
|
|
|
|
|
|
|
this._handleUpdateCarousel({
|
|
|
|
objects: this.props.data.data.objects,
|
|
|
|
});
|
2020-08-03 02:29:11 +03:00
|
|
|
}
|
|
|
|
|
2020-08-22 20:30:03 +03:00
|
|
|
_handleChangeLayout = async (layout, layouts) => {
|
2020-09-03 00:04:47 +03:00
|
|
|
this.setState({ layouts, saving: "SAVING" });
|
|
|
|
await this._handleSave(null, null, layouts);
|
2020-08-22 20:30:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_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) => {
|
2020-08-03 02:29:11 +03:00
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
const response = await Actions.updateSlate({
|
2020-08-31 21:19:46 +03:00
|
|
|
id: this.props.data.id,
|
2020-08-03 02:29:11 +03:00
|
|
|
data: {
|
2020-08-21 10:07:39 +03:00
|
|
|
objects: objects ? objects : this.state.objects,
|
2020-08-22 20:30:03 +03:00
|
|
|
layouts: layouts ? layouts : this.state.layouts,
|
2020-08-03 02:29:11 +03:00
|
|
|
public: this.state.public,
|
2020-08-22 08:19:11 +03:00
|
|
|
body: this.state.body,
|
2020-08-26 07:41:05 +03:00
|
|
|
name: this.state.name,
|
2020-08-03 02:29:11 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response) {
|
2020-09-03 00:04:47 +03:00
|
|
|
this.setState({ loading: false, saving: "ERROR" });
|
2020-08-03 02:29:11 +03:00
|
|
|
alert("TODO: Server Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.error) {
|
2020-09-03 00:04:47 +03:00
|
|
|
this.setState({ loading: false, saving: "ERROR" });
|
2020-08-03 02:29:11 +03:00
|
|
|
alert(`TODO: ${response.decorator}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.props.onRehydrate();
|
2020-08-09 11:08:46 +03:00
|
|
|
|
|
|
|
this._handleUpdateCarousel(this.state);
|
2020-08-03 02:29:11 +03:00
|
|
|
};
|
|
|
|
|
2020-08-21 10:07:39 +03:00
|
|
|
_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 },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
_handleUpdateCarousel = (state) => {
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "slate-global-create-carousel",
|
|
|
|
detail: {
|
|
|
|
slides: state.objects.map((each) => {
|
2020-08-25 21:39:14 +03:00
|
|
|
// NOTE(jim):
|
|
|
|
// This is a hack to catch this undefined case I don't want to track down yet.
|
|
|
|
const url = each.url.replace("https://undefined", "https://");
|
|
|
|
|
2020-08-25 20:46:41 +03:00
|
|
|
// NOTE
|
2020-08-25 20:50:11 +03:00
|
|
|
// regex here performs https://{cid}.ipfs.slate.textile.io => [https://{cid}, {cid}]
|
2020-08-25 21:39:14 +03:00
|
|
|
let cid = url.match(
|
|
|
|
/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i
|
|
|
|
)[1];
|
|
|
|
const data = { ...each, cid, url };
|
|
|
|
|
2020-08-09 11:38:16 +03:00
|
|
|
return {
|
|
|
|
onDelete: this._handleDelete,
|
2020-08-21 10:07:39 +03:00
|
|
|
onObjectSave: this._handleObjectSave,
|
2020-08-25 21:39:14 +03:00
|
|
|
id: data.id,
|
2020-08-21 10:07:39 +03:00
|
|
|
cid,
|
2020-08-25 21:39:14 +03:00
|
|
|
data,
|
2020-08-31 21:19:46 +03:00
|
|
|
editing: this.state.editing,
|
2020-08-25 21:39:14 +03:00
|
|
|
component: (
|
|
|
|
<SlateMediaObject key={each.id} useImageFallback data={data} />
|
|
|
|
),
|
2020-08-09 11:38:16 +03:00
|
|
|
};
|
2020-08-09 11:08:46 +03:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2020-08-07 05:17:49 +03:00
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
_handleDelete = async (id) => {
|
2020-08-09 11:38:16 +03:00
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: true },
|
|
|
|
});
|
2020-08-07 05:17:49 +03:00
|
|
|
|
2020-08-23 23:20:16 +03:00
|
|
|
let index;
|
|
|
|
const objects = this.state.objects.filter((o, i) => {
|
|
|
|
if (o.id === id) {
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.id !== id;
|
|
|
|
});
|
|
|
|
|
2020-08-24 02:53:16 +03:00
|
|
|
// NOTE(jim): Every time we remove an object from a slate.
|
|
|
|
// We will want to remove the object from the layouts too.
|
2020-08-23 23:20:16 +03:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
const response = await Actions.updateSlate({
|
2020-08-31 21:19:46 +03:00
|
|
|
id: this.props.data.slateId,
|
2020-08-09 11:08:46 +03:00
|
|
|
data: {
|
2020-08-23 23:20:16 +03:00
|
|
|
objects,
|
|
|
|
layouts,
|
2020-08-09 11:08:46 +03:00
|
|
|
public: this.state.public,
|
2020-08-22 20:30:03 +03:00
|
|
|
body: this.state.body,
|
2020-08-26 07:41:05 +03:00
|
|
|
name: this.state.name,
|
2020-08-09 11:08:46 +03:00
|
|
|
},
|
2020-08-07 05:17:49 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!response) {
|
2020-08-09 11:38:16 +03:00
|
|
|
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) {
|
2020-08-09 11:38:16 +03:00
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: false },
|
|
|
|
});
|
2020-08-07 05:17:49 +03:00
|
|
|
alert(`TODO: ${response.decorator}`);
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
await this.props.onRehydrate();
|
2020-08-07 05:17:49 +03:00
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
this._handleUpdateCarousel(this.state);
|
2020-08-03 02:29:11 +03:00
|
|
|
|
2020-08-09 11:38:16 +03:00
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "state-global-carousel-loading",
|
|
|
|
detail: { loading: false },
|
|
|
|
});
|
2020-08-03 02:29:11 +03:00
|
|
|
};
|
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
_handleSelect = (index) =>
|
|
|
|
System.dispatchCustomEvent({
|
|
|
|
name: "slate-global-open-carousel",
|
|
|
|
detail: { index },
|
|
|
|
});
|
2020-07-27 11:33:39 +03:00
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
_handleAdd = () => {
|
|
|
|
return this.props.onAction({
|
|
|
|
type: "SIDEBAR",
|
|
|
|
value: "SIDEBAR_ADD_FILE_TO_BUCKET",
|
2020-08-31 21:19:46 +03:00
|
|
|
data: this.props.data,
|
2020-08-09 11:08:46 +03:00
|
|
|
});
|
|
|
|
};
|
2020-07-27 11:33:39 +03:00
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
_handleShowSettings = () => {
|
|
|
|
return this.props.onAction({
|
|
|
|
type: "SIDEBAR",
|
|
|
|
value: "SIDEBAR_SINGLE_SLATE_SETTINGS",
|
2020-08-31 21:19:46 +03:00
|
|
|
data: this.props.data,
|
2020-08-09 11:08:46 +03:00
|
|
|
});
|
|
|
|
};
|
2020-07-27 11:33:39 +03:00
|
|
|
|
2020-08-09 11:08:46 +03:00
|
|
|
render() {
|
2020-08-31 21:19:46 +03:00
|
|
|
const {
|
|
|
|
username,
|
|
|
|
slatename,
|
|
|
|
objects,
|
|
|
|
body = "A slate.",
|
|
|
|
name,
|
|
|
|
} = this.state;
|
2020-08-03 02:29:11 +03:00
|
|
|
|
2020-07-27 11:33:39 +03:00
|
|
|
return (
|
2020-08-09 11:08:46 +03:00
|
|
|
<ScenePage style={{ padding: `88px 24px 128px 24px` }}>
|
2020-08-22 07:25:34 +03:00
|
|
|
<ScenePageHeader
|
|
|
|
style={{ padding: `0 24px 0 24px` }}
|
2020-08-31 21:19:46 +03:00
|
|
|
title={
|
|
|
|
username ? (
|
|
|
|
<span>
|
|
|
|
<span
|
|
|
|
onClick={() =>
|
|
|
|
this.props.onAction({
|
|
|
|
type: "NAVIGATE",
|
|
|
|
value: this.props.sceneId,
|
|
|
|
scene: "PROFILE",
|
|
|
|
data: this.props.data.owner,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
css={STYLES_USERNAME}
|
|
|
|
>
|
|
|
|
{username}
|
|
|
|
</span>{" "}
|
|
|
|
/ {slatename}
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
slatename
|
|
|
|
)
|
|
|
|
}
|
2020-08-22 07:25:34 +03:00
|
|
|
actions={
|
2020-08-31 21:19:46 +03:00
|
|
|
this.state.editing ? (
|
|
|
|
<React.Fragment>
|
|
|
|
<CircleButtonGray
|
|
|
|
onMouseUp={this._handleAdd}
|
|
|
|
onTouchEnd={this._handleAdd}
|
|
|
|
style={{ marginLeft: 12, marginRight: 12 }}
|
|
|
|
>
|
|
|
|
<SVG.Plus height="16px" />
|
|
|
|
</CircleButtonGray>
|
|
|
|
<CircleButtonGray
|
|
|
|
onMouseUp={this._handleShowSettings}
|
|
|
|
onTouchEnd={this._handleShowSettings}
|
|
|
|
>
|
|
|
|
<SVG.Settings height="16px" />
|
|
|
|
</CircleButtonGray>
|
|
|
|
</React.Fragment>
|
|
|
|
) : null
|
2020-08-25 21:39:14 +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-31 21:19:46 +03:00
|
|
|
editing={this.state.editing}
|
2020-09-03 00:04:47 +03:00
|
|
|
saving={this.state.saving}
|
2020-08-22 12:32:40 +03:00
|
|
|
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>
|
|
|
|
);
|
2020-07-27 06:20:34 +03:00
|
|
|
}
|
|
|
|
}
|