slate/scenes/SceneProfile.js

212 lines
5.5 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as System from "~/components/system";
import * as Actions from "~/common/actions";
import * as Constants from "~/common/constants";
import * as SVG from "~/common/svg";
import { css } from "@emotion/react";
2020-09-03 00:08:32 +03:00
import {
ButtonPrimary,
ButtonSecondary,
} from "~/components/system/components/Buttons";
2020-09-12 01:25:33 +03:00
import { dispatchCustomEvent } from "~/common/custom-events";
import ScenePage from "~/components/core/ScenePage";
import Profile from "~/components/core/Profile";
2020-09-15 00:29:41 +03:00
const STYLES_BUTTONS = css`
display: inline-flex;
flex-direction: row;
align-items: center;
`;
const STATUS_BUTTON_MAP = {
trusted: "Remove peer",
untrusted: "Add peer",
sent: "Cancel request",
received: "Accept request",
};
export default class SceneProfile extends React.Component {
state = {
loading: false,
trustStatus: "untrusted",
followStatus: false,
2020-09-15 00:29:41 +03:00
followLoading: false,
trustLoading: false,
};
componentDidMount = () => {
this.setStatus(this.props.viewer);
};
componentDidUpdate(prevProps) {
2020-09-04 01:42:08 +03:00
if (prevProps.data.username !== this.props.data.username) {
this.setStatus(this.props.viewer);
}
}
setStatus = (viewer) => {
2020-09-15 00:29:41 +03:00
let newState = {
trustStatus: "untrusted",
followStatus: false,
followLoading: false,
trustLoading: false,
};
let trust = viewer.trusted.filter((entry) => {
return entry.target_user_id === this.props.data.id;
});
if (trust.length) {
let relation = trust[0];
newState.trustId = relation.id;
if (relation.data.verified) {
newState.trustStatus = "trusted";
} else {
newState.trustStatus = "sent";
}
}
let pendingTrust = viewer.pendingTrusted.filter((entry) => {
return entry.owner_user_id === this.props.data.id;
});
if (pendingTrust.length) {
let relation = pendingTrust[0];
newState.trustId = relation.id;
if (pendingTrust[0].data.verified) {
newState.trustStatus = "trusted";
} else {
newState.trustStatus = "received";
}
}
if (
viewer.subscriptions.filter((entry) => {
return entry.target_user_id === this.props.data.id;
}).length
) {
newState.followStatus = true;
}
this.setState(newState);
};
_handleUpdate = async (e) => {
let response = await this.props.onRehydrate();
2020-09-12 01:25:33 +03:00
if (!response) {
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
message:
"We're having trouble connecting right now. Please try again later",
},
},
});
return null;
}
if (response.error) {
dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
return null;
}
let viewer = response.data;
this.setStatus(viewer);
};
2020-09-15 00:29:41 +03:00
_handleTrust = () => {
this.setState({ trustLoading: true }, async () => {
let response;
if (
this.state.trustStatus === "untrusted" ||
this.state.trustStatus === "sent"
) {
response = await Actions.createTrustRelationship({
userId: this.props.data.id,
});
console.log(response);
} else if (this.state.trustStatus === "received") {
response = await Actions.updateTrustRelationship({
userId: this.props.data.id,
});
console.log(response);
} else {
response = await Actions.deleteTrustRelationship({
id: this.state.trustId,
});
console.log(response);
}
await this._handleUpdate();
});
};
_handleFollow = () => {
this.setState({ followLoading: true }, async () => {
let response = await Actions.createSubscription({
userId: this.props.data.id,
});
console.log(response);
2020-09-15 00:29:41 +03:00
await this._handleUpdate();
});
};
render() {
let buttons = (
2020-09-15 00:29:41 +03:00
<div css={STYLES_BUTTONS}>
2020-09-03 00:08:32 +03:00
{this.state.followStatus ? (
<ButtonSecondary
2020-09-15 00:29:41 +03:00
loading={this.state.followLoading}
style={{ margin: "0px 8px", minWidth: 152 }}
2020-09-03 00:08:32 +03:00
onClick={this._handleFollow}
>
Unfollow
</ButtonSecondary>
) : (
<ButtonPrimary
2020-09-15 00:29:41 +03:00
loading={this.state.followLoading}
style={{ margin: "0px 8px", minWidth: 152 }}
2020-09-03 00:08:32 +03:00
onClick={this._handleFollow}
>
Follow
</ButtonPrimary>
)}
{this.state.trustStatus === "untrusted" ||
this.state.trustStatus === "received" ? (
<ButtonPrimary
2020-09-15 00:29:41 +03:00
loading={this.state.trustLoading}
style={{ margin: "0px 8px", minWidth: 152 }}
2020-09-03 00:08:32 +03:00
onClick={this._handleTrust}
>
{STATUS_BUTTON_MAP[this.state.trustStatus]}
</ButtonPrimary>
) : (
<ButtonSecondary
2020-09-15 00:29:41 +03:00
loading={this.state.trustLoading}
style={{ margin: "0px 8px", minWidth: 152 }}
2020-09-03 00:08:32 +03:00
onClick={this._handleTrust}
>
{STATUS_BUTTON_MAP[this.state.trustStatus]}
</ButtonSecondary>
)}
</div>
);
return (
2020-09-03 00:08:32 +03:00
<ScenePage>
<Profile
2020-09-03 02:17:31 +03:00
{...this.props}
onAction={this.props.onAction}
creator={this.props.data}
sceneId={this.props.sceneId}
buttons={
this.props.viewer.username === this.props.data.username
? null
: buttons
}
2020-09-01 21:46:36 +03:00
editing={this.props.viewer.username === this.props.data.username}
/>
</ScenePage>
);
}
}