2020-08-31 21:19:46 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Actions from "~/common/actions";
|
2020-12-20 05:19:32 +03:00
|
|
|
import * as SVG from "~/common/svg";
|
2020-08-31 21:19:46 +03:00
|
|
|
|
2020-11-30 08:24:22 +03:00
|
|
|
import { css } from "@emotion/react";
|
2020-12-20 05:19:32 +03:00
|
|
|
import { LoaderSpinner } from "~/components/system/components/Loaders";
|
2020-08-31 21:19:46 +03:00
|
|
|
|
|
|
|
import ScenePage from "~/components/core/ScenePage";
|
|
|
|
import Profile from "~/components/core/Profile";
|
2020-12-20 05:19:32 +03:00
|
|
|
import EmptyState from "~/components/core/EmptyState";
|
2020-08-31 21:19:46 +03:00
|
|
|
|
2020-12-20 05:19:32 +03:00
|
|
|
const STYLES_LOADER = css`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
height: 90vh;
|
|
|
|
width: 100%;
|
|
|
|
`;
|
|
|
|
|
2020-08-31 21:19:46 +03:00
|
|
|
export default class SceneProfile extends React.Component {
|
2020-12-20 05:19:32 +03:00
|
|
|
state = {
|
|
|
|
profile: null,
|
|
|
|
notFound: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount = async () => {
|
2021-01-29 03:57:02 +03:00
|
|
|
console.log("component did mount called");
|
2020-12-20 05:19:32 +03:00
|
|
|
await this.fetchProfile();
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate = async (prevProps) => {
|
2021-01-15 23:20:30 +03:00
|
|
|
if (this.props.data?.id && prevProps.data?.id && this.props.data.id !== prevProps.data.id) {
|
2020-12-20 05:19:32 +03:00
|
|
|
await this.fetchProfile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchProfile = async () => {
|
2021-01-29 03:57:02 +03:00
|
|
|
const username = this.props.page.user || this.props.page.data?.username;
|
2020-12-20 05:19:32 +03:00
|
|
|
let query;
|
|
|
|
let targetUser;
|
|
|
|
if (username) {
|
|
|
|
if (username === this.props.viewer.username) {
|
|
|
|
targetUser = this.props.viewer;
|
|
|
|
} else {
|
|
|
|
query = { username: username };
|
|
|
|
}
|
|
|
|
} else if (this.props.data && this.props.data.id) {
|
|
|
|
if (this.props.data.id === this.props.viewer.id) {
|
|
|
|
targetUser = this.props.viewer;
|
|
|
|
} else {
|
|
|
|
query = { id: this.props.data.id };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!targetUser) {
|
|
|
|
let response;
|
|
|
|
if (query) {
|
|
|
|
response = await Actions.getSerializedProfile(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response || response.error) {
|
|
|
|
this.setState({ notFound: true });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
targetUser = response.data;
|
|
|
|
}
|
|
|
|
|
2021-01-29 03:57:02 +03:00
|
|
|
window.history.replaceState(
|
|
|
|
{ ...window.history.state, data: targetUser },
|
|
|
|
"A slate user",
|
|
|
|
`/${targetUser.username}`
|
|
|
|
);
|
2020-12-20 05:19:32 +03:00
|
|
|
|
|
|
|
this.props.onUpdateData({ data: targetUser });
|
2021-01-19 00:21:39 +03:00
|
|
|
this.setState({ profile: targetUser });
|
2020-12-20 05:19:32 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.profile) {
|
|
|
|
return (
|
|
|
|
<div css={STYLES_LOADER}>
|
|
|
|
<LoaderSpinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-31 21:19:46 +03:00
|
|
|
return (
|
2021-01-17 04:53:02 +03:00
|
|
|
<Profile
|
|
|
|
{...this.props}
|
|
|
|
creator={
|
|
|
|
this.state.profile.id === this.props.viewer.id ? this.props.viewer : this.state.profile
|
|
|
|
}
|
2021-01-21 00:50:29 +03:00
|
|
|
isOwner={this.state.profile.id === this.props.viewer.id}
|
2021-02-05 15:19:57 +03:00
|
|
|
isAuthenticated={Boolean(this.props.viewer)}
|
2021-01-21 00:50:29 +03:00
|
|
|
key={this.state.profile.id}
|
2021-01-17 04:53:02 +03:00
|
|
|
/>
|
2020-08-31 21:19:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|