slate/scenes/SceneProfile.js

120 lines
3.4 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as Actions from "~/common/actions";
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
2020-10-31 02:12:20 +03:00
import { ButtonPrimary, ButtonSecondary } from "~/components/system/components/Buttons";
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 {
2020-10-31 02:12:20 +03:00
_handleTrust = async (trustStatus, trustId) => {
if (trustStatus === "untrusted" || trustStatus === "sent") {
await Actions.createTrustRelationship({
userId: this.props.data.id,
});
} else if (trustStatus === "received") {
await Actions.updateTrustRelationship({
userId: this.props.data.id,
});
} else {
await Actions.deleteTrustRelationship({
id: trustId,
});
}
};
2020-10-31 02:12:20 +03:00
_handleFollow = async () => {
await Actions.createSubscription({
userId: this.props.data.id,
});
};
2020-10-31 02:12:20 +03:00
render() {
let trustId, followStatus, relation;
let trustStatus = "untrusted";
let viewer = this.props.viewer;
let trust = viewer.trusted.filter((entry) => {
return entry.target_user_id === this.props.data.id;
});
if (trust.length) {
2020-10-31 02:12:20 +03:00
relation = trust[0];
trustId = relation.id;
if (relation.data.verified) {
2020-10-31 02:12:20 +03:00
trustStatus = "trusted";
} else {
2020-10-31 02:12:20 +03:00
trustStatus = "sent";
}
}
let pendingTrust = viewer.pendingTrusted.filter((entry) => {
return entry.owner_user_id === this.props.data.id;
});
if (pendingTrust.length) {
2020-10-31 02:12:20 +03:00
relation = pendingTrust[0];
trustId = relation.id;
if (pendingTrust[0].data.verified) {
2020-10-31 02:12:20 +03:00
trustStatus = "trusted";
} else {
2020-10-31 02:12:20 +03:00
trustStatus = "received";
}
}
2020-10-31 02:12:20 +03:00
followStatus = !!viewer.subscriptions.filter((entry) => {
return entry.target_user_id === this.props.data.id;
}).length;
let buttons = (
2020-09-15 00:29:41 +03:00
<div css={STYLES_BUTTONS}>
2020-10-31 02:12:20 +03:00
{followStatus ? (
<ButtonSecondary style={{ marginRight: 8, minWidth: 152 }} onClick={this._handleFollow}>
2020-09-03 00:08:32 +03:00
Unfollow
</ButtonSecondary>
) : (
<ButtonPrimary style={{ marginRight: 8, minWidth: 152 }} onClick={this._handleFollow}>
2020-09-03 00:08:32 +03:00
Follow
</ButtonPrimary>
)}
2020-10-31 02:12:20 +03:00
{trustStatus === "untrusted" || trustStatus === "received" ? (
2020-09-03 00:08:32 +03:00
<ButtonPrimary
style={{ marginRight: 8, minWidth: 152 }}
2020-10-31 02:12:20 +03:00
onClick={() => this._handleTrust(trustStatus, trustId)}
2020-09-03 00:08:32 +03:00
>
2020-10-31 02:12:20 +03:00
{STATUS_BUTTON_MAP[trustStatus]}
2020-09-03 00:08:32 +03:00
</ButtonPrimary>
) : (
<ButtonSecondary
style={{ marginRight: 8, minWidth: 152 }}
2020-10-31 02:12:20 +03:00
onClick={() => this._handleTrust(trustStatus, trustId)}
2020-09-03 00:08:32 +03:00
>
2020-10-31 02:12:20 +03:00
{STATUS_BUTTON_MAP[trustStatus]}
2020-09-03 00:08:32 +03:00
</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}
2020-10-31 02:12:20 +03:00
buttons={this.props.viewer.username === this.props.data.username ? null : buttons}
2020-10-05 00:30:28 +03:00
isOwner={this.props.viewer.username === this.props.data.username}
/>
</ScenePage>
);
}
}