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 SceneProfile from "~/scenes/SceneProfile";
|
|
|
|
|
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 ScenePublicProfile extends React.Component {
|
|
|
|
state = {
|
|
|
|
profile: null,
|
|
|
|
};
|
|
|
|
|
2020-09-12 10:36:32 +03:00
|
|
|
componentDidMount = async () => {
|
2020-11-15 05:12:04 +03:00
|
|
|
await this.fetchProfile();
|
2020-09-04 01:42:08 +03:00
|
|
|
};
|
|
|
|
|
2020-09-12 10:36:32 +03:00
|
|
|
componentDidUpdate = async (prevProps) => {
|
2020-11-17 05:10:43 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-15 05:12:04 +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 };
|
2020-09-12 10:36:32 +03:00
|
|
|
}
|
2020-11-17 05:10:43 +03:00
|
|
|
let response;
|
2020-11-15 05:12:04 +03:00
|
|
|
if (query) {
|
2020-11-17 05:10:43 +03:00
|
|
|
response = await Actions.getSerializedProfile(query);
|
2020-09-12 10:36:32 +03:00
|
|
|
}
|
|
|
|
|
2020-11-17 05:10:43 +03:00
|
|
|
if (!response || response.error) {
|
2020-11-15 05:12:04 +03:00
|
|
|
dispatchCustomEvent({
|
|
|
|
name: "create-alert",
|
|
|
|
detail: { alert: { message: "We're having trouble fetching that user right now." } },
|
|
|
|
});
|
|
|
|
this.props.onBack();
|
2020-09-04 01:42:08 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-09-12 10:36:32 +03:00
|
|
|
|
2020-11-17 05:10:43 +03:00
|
|
|
this.props.onUpdateData({ data: response.data });
|
|
|
|
this.setState({ profile: response.data });
|
2020-09-04 01:42:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.state.profile) {
|
|
|
|
return (
|
2020-11-15 05:12:04 +03:00
|
|
|
//TODO(martina): replace with ghost UI while loading
|
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
|
|
|
);
|
|
|
|
}
|
2020-09-12 10:36:32 +03:00
|
|
|
|
2020-09-04 01:42:08 +03:00
|
|
|
return <SceneProfile {...this.props} data={this.state.profile} />;
|
|
|
|
}
|
|
|
|
}
|