slate/scenes/ScenePublicSlate.js

122 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-09-04 01:42:08 +03:00
import * as React from "react";
import * as Actions from "~/common/actions";
import * as Window from "~/common/window";
2020-09-04 01:42:08 +03:00
import { LoaderSpinner } from "~/components/system/components/Loaders";
import { css } from "@emotion/core";
import { dispatchCustomEvent } from "~/common/custom-events";
2020-09-04 01:42:08 +03:00
import SceneSlate from "~/scenes/SceneSlate";
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,
};
componentDidMount = async () => {
await this.fetchSlate();
2020-09-04 01:42:08 +03:00
};
componentDidUpdate = async (prevProps) => {
if (!this.props.data) {
return null;
2020-09-04 01:42:08 +03:00
}
2020-09-12 10:46:27 +03:00
if (!prevProps.data) {
return null;
}
if (this.props.data.id === prevProps.data.id) {
return null;
}
await this.fetchSlate();
2020-09-04 01:42:08 +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;
}
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
}
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-04 01:42:08 +03:00
this.setState({ slate: slate.data });
};
render() {
if (!this.state.slate) {
return (
<div css={STYLES_LOADER}>
2020-09-04 01:42:08 +03:00
<LoaderSpinner />
</div>
2020-09-04 01:42:08 +03:00
);
}
return <SceneSlate {...this.props} current={this.state.slate} />;
}
}