slate/scenes/ScenePublicProfile.js

95 lines
2.3 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-11-28 06:35:48 +03:00
import * as SVG from "~/common/svg";
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
2020-11-28 06:35:48 +03:00
import EmptyState from "~/components/core/EmptyState";
2020-09-04 01:42:08 +03:00
import SceneProfile from "~/scenes/SceneProfile";
2020-11-28 06:35:48 +03:00
import ScenePage from "~/components/core/ScenePage";
2020-09-04 01:42:08 +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 ScenePublicProfile extends React.Component {
state = {
profile: null,
2020-11-28 06:35:48 +03:00
notFound: false,
2020-09-04 01:42:08 +03:00
};
componentDidMount = async () => {
await this.fetchProfile();
2020-09-04 01:42:08 +03:00
};
componentDidUpdate = async (prevProps) => {
if (
this.props.data &&
prevProps.data &&
this.props.data.id &&
prevProps.data.id &&
this.props.data.id !== prevProps.data.id
) {
await this.fetchProfile();
2020-09-04 01:42:08 +03:00
}
};
fetchProfile = async () => {
const username = Window.getQueryParameterByName("user");
let query;
if (username) {
query = { username: username };
} else if (this.props.data && this.props.data.id) {
if (this.props.data.id === this.props.viewer.id) {
this.setState({ profile: this.props.viewer });
return;
}
query = { id: this.props.data.id };
} else {
query = { id: this.props.viewer.id };
}
let response;
if (query) {
response = await Actions.getSerializedProfile(query);
}
if (!response || response.error) {
2020-11-28 06:35:48 +03:00
this.setState({ notFound: true });
2020-09-04 01:42:08 +03:00
return;
}
this.props.onUpdateData({ data: response.data });
this.setState({ profile: response.data });
2020-09-04 01:42:08 +03:00
};
render() {
2020-11-28 06:35:48 +03:00
if (this.state.notFound) {
return (
<ScenePage>
<EmptyState>
<SVG.Users height="24px" style={{ marginBottom: 24 }} />
<div>We were unable to locate that user profile</div>
</EmptyState>
</ScenePage>
);
}
2020-09-04 01:42:08 +03:00
if (!this.state.profile) {
return (
<div css={STYLES_LOADER}>
2020-09-04 01:42:08 +03:00
<LoaderSpinner />
</div>
2020-09-04 01:42:08 +03:00
);
}
2020-09-04 01:42:08 +03:00
return <SceneProfile {...this.props} data={this.state.profile} />;
}
}