2020-09-04 01:42:08 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Actions from "~/common/actions";
|
2020-11-15 05:12:04 +03:00
|
|
|
import * as Window from "~/common/window";
|
2020-09-04 01:42:08 +03:00
|
|
|
|
|
|
|
import { LoaderSpinner } from "~/components/system/components/Loaders";
|
2020-11-04 20:55:48 +03:00
|
|
|
import { css } from "@emotion/core";
|
2020-11-15 05:12:04 +03:00
|
|
|
import { dispatchCustomEvent } from "~/common/custom-events";
|
2020-09-04 01:42:08 +03:00
|
|
|
|
|
|
|
import SceneSlate from "~/scenes/SceneSlate";
|
|
|
|
|
2020-09-14 08:22:19 +03:00
|
|
|
const STYLES_LOADER = css`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
height: 90vh;
|
|
|
|
width: 100%;
|
|
|
|
`;
|
|
|
|
|
2020-09-04 01:42:08 +03:00
|
|
|
export default class ScenePublicSlate extends React.Component {
|
|
|
|
state = {
|
|
|
|
slate: null,
|
|
|
|
};
|
|
|
|
|
2020-09-12 10:40:31 +03:00
|
|
|
componentDidMount = async () => {
|
2020-11-15 05:12:04 +03:00
|
|
|
await this.fetchSlate();
|
2020-09-04 01:42:08 +03:00
|
|
|
};
|
|
|
|
|
2020-09-12 10:40:31 +03:00
|
|
|
componentDidUpdate = async (prevProps) => {
|
|
|
|
if (!this.props.data) {
|
|
|
|
return null;
|
2020-09-04 01:42:08 +03:00
|
|
|
}
|
2020-09-12 10:40:31 +03:00
|
|
|
|
2020-09-12 10:46:27 +03:00
|
|
|
if (!prevProps.data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-12 10:40:31 +03:00
|
|
|
if (this.props.data.id === prevProps.data.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:12:04 +03:00
|
|
|
await this.fetchSlate();
|
2020-09-04 01:42:08 +03:00
|
|
|
};
|
|
|
|
|
2020-11-15 05:12:04 +03:00
|
|
|
fetchSlate = async () => {
|
|
|
|
const username = Window.getQueryParameterByName("user");
|
|
|
|
const slatename = Window.getQueryParameterByName("slate");
|
|
|
|
if (
|
|
|
|
!this.props.data &&
|
|
|
|
!username &&
|
|
|
|
!slatename &&
|
|
|
|
this.props.viewer.slates &&
|
|
|
|
this.props.viewer.slates.length
|
|
|
|
) {
|
|
|
|
this.props.onAction({
|
|
|
|
type: "NAVIGATE",
|
|
|
|
value: this.props.viewer.slates[0].id,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-09-12 10:40:31 +03:00
|
|
|
|
2020-11-15 05:12:04 +03:00
|
|
|
let slateIds = this.props.viewer.slates.map((slate) => slate.id);
|
|
|
|
if (this.props.data && this.props.data.id && slateIds.includes(this.props.data.id)) {
|
|
|
|
this.props.onAction({
|
|
|
|
type: "NAVIGATE",
|
|
|
|
value: this.props.data.id,
|
|
|
|
redirect: true,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (username === this.props.viewer.username) {
|
|
|
|
for (let slate of this.props.viewer.slates) {
|
|
|
|
if (slate.slatename === slatename) {
|
|
|
|
this.props.onAction({
|
|
|
|
type: "NAVIGATE",
|
|
|
|
value: slate.id,
|
|
|
|
redirect: true,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-09-04 01:42:08 +03:00
|
|
|
}
|
2020-11-15 05:12:04 +03:00
|
|
|
dispatchCustomEvent({
|
|
|
|
name: "create-alert",
|
|
|
|
detail: { alert: { message: "We're having trouble fetching that slate right now." } },
|
|
|
|
});
|
|
|
|
this.props.onBack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let query;
|
|
|
|
if (username && slatename) {
|
|
|
|
query = { username, slatename };
|
|
|
|
} else if (this.props.data && this.props.data.id) {
|
|
|
|
query = { id: this.props.data.id };
|
|
|
|
}
|
|
|
|
let slate;
|
|
|
|
if (query) {
|
|
|
|
slate = await Actions.getSerializedSlate(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slate || slate.error) {
|
|
|
|
dispatchCustomEvent({
|
|
|
|
name: "create-alert",
|
|
|
|
detail: { alert: { message: "We're having trouble fetching that slate right now." } },
|
|
|
|
});
|
|
|
|
this.props.onBack();
|
2020-09-04 01:42:08 +03:00
|
|
|
}
|
2020-09-12 10:40:31 +03:00
|
|
|
|
2020-09-04 01:42:08 +03:00
|
|
|
this.setState({ slate: slate.data });
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.state.slate) {
|
|
|
|
return (
|
2020-09-14 08:22:19 +03:00
|
|
|
<div css={STYLES_LOADER}>
|
2020-09-04 01:42:08 +03:00
|
|
|
<LoaderSpinner />
|
2020-09-14 08:22:19 +03:00
|
|
|
</div>
|
2020-09-04 01:42:08 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return <SceneSlate {...this.props} current={this.state.slate} />;
|
|
|
|
}
|
|
|
|
}
|