2019-04-03 23:01:20 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-04-04 20:53:32 +00:00
|
|
|
import { Link } from 'inferno-router';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
Community,
|
|
|
|
CommunityUser,
|
|
|
|
FollowCommunityForm,
|
|
|
|
CommunityForm as CommunityFormI,
|
|
|
|
UserView,
|
|
|
|
} from '../interfaces';
|
2019-04-04 22:29:14 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-15 23:12:06 +00:00
|
|
|
import { mdToHtml, getUnixTime } from '../utils';
|
2019-04-04 22:29:14 +00:00
|
|
|
import { CommunityForm } from './community-form';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
|
|
|
import { T } from 'inferno-i18next';
|
2019-04-03 23:01:20 +00:00
|
|
|
|
|
|
|
interface SidebarProps {
|
|
|
|
community: Community;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-21 20:52:55 +00:00
|
|
|
admins: Array<UserView>;
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface SidebarState {
|
2019-04-04 22:29:14 +00:00
|
|
|
showEdit: boolean;
|
2019-04-15 23:12:06 +00:00
|
|
|
showRemoveDialog: boolean;
|
|
|
|
removeReason: string;
|
|
|
|
removeExpires: string;
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Sidebar extends Component<SidebarProps, SidebarState> {
|
2019-04-04 22:29:14 +00:00
|
|
|
private emptyState: SidebarState = {
|
2019-04-15 23:12:06 +00:00
|
|
|
showEdit: false,
|
|
|
|
showRemoveDialog: false,
|
|
|
|
removeReason: null,
|
2019-10-19 00:20:27 +00:00
|
|
|
removeExpires: null,
|
|
|
|
};
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-03 23:01:20 +00:00
|
|
|
super(props, context);
|
2019-04-04 22:29:14 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
this.handleEditCommunity = this.handleEditCommunity.bind(this);
|
2019-04-05 02:08:21 +00:00
|
|
|
this.handleEditCancel = this.handleEditCancel.bind(this);
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-04-04 22:29:14 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2019-10-19 00:20:27 +00:00
|
|
|
{!this.state.showEdit ? (
|
|
|
|
this.sidebar()
|
|
|
|
) : (
|
|
|
|
<CommunityForm
|
|
|
|
community={this.props.community}
|
|
|
|
onEdit={this.handleEditCommunity}
|
|
|
|
onCancel={this.handleEditCancel}
|
|
|
|
/>
|
|
|
|
)}
|
2019-04-04 22:29:14 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sidebar() {
|
2019-04-03 23:01:20 +00:00
|
|
|
let community = this.props.community;
|
|
|
|
return (
|
|
|
|
<div>
|
2019-08-20 02:37:32 +00:00
|
|
|
<div class="card border-secondary mb-3">
|
|
|
|
<div class="card-body">
|
|
|
|
<h5 className="mb-0">
|
|
|
|
<span>{community.title}</span>
|
2019-10-19 00:20:27 +00:00
|
|
|
{community.removed && (
|
|
|
|
<small className="ml-2 text-muted font-italic">
|
|
|
|
<T i18nKey="removed">#</T>
|
|
|
|
</small>
|
|
|
|
)}
|
|
|
|
{community.deleted && (
|
|
|
|
<small className="ml-2 text-muted font-italic">
|
|
|
|
<T i18nKey="deleted">#</T>
|
|
|
|
</small>
|
|
|
|
)}
|
2019-08-20 02:37:32 +00:00
|
|
|
</h5>
|
2019-10-19 00:20:27 +00:00
|
|
|
<Link className="text-muted" to={`/c/${community.name}`}>
|
|
|
|
/c/{community.name}
|
|
|
|
</Link>
|
|
|
|
<ul class="list-inline mb-1 text-muted small font-weight-bold">
|
|
|
|
{this.canMod && (
|
2019-08-20 02:37:32 +00:00
|
|
|
<>
|
|
|
|
<li className="list-inline-item">
|
2019-10-19 00:20:27 +00:00
|
|
|
<span
|
|
|
|
class="pointer"
|
|
|
|
onClick={linkEvent(this, this.handleEditClick)}
|
|
|
|
>
|
|
|
|
<T i18nKey="edit">#</T>
|
|
|
|
</span>
|
2019-08-20 02:37:32 +00:00
|
|
|
</li>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.amCreator && (
|
2019-08-20 02:37:32 +00:00
|
|
|
<li className="list-inline-item">
|
2019-10-19 00:20:27 +00:00
|
|
|
<span
|
|
|
|
class="pointer"
|
|
|
|
onClick={linkEvent(this, this.handleDeleteClick)}
|
|
|
|
>
|
|
|
|
{!community.deleted
|
|
|
|
? i18n.t('delete')
|
|
|
|
: i18n.t('restore')}
|
2019-08-20 02:37:32 +00:00
|
|
|
</span>
|
|
|
|
</li>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-08-20 02:37:32 +00:00
|
|
|
</>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
|
|
|
{this.canAdmin && (
|
2019-08-20 02:37:32 +00:00
|
|
|
<li className="list-inline-item">
|
2019-10-19 00:20:27 +00:00
|
|
|
{!this.props.community.removed ? (
|
|
|
|
<span
|
|
|
|
class="pointer"
|
|
|
|
onClick={linkEvent(this, this.handleModRemoveShow)}
|
|
|
|
>
|
|
|
|
<T i18nKey="remove">#</T>
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
class="pointer"
|
|
|
|
onClick={linkEvent(this, this.handleModRemoveSubmit)}
|
|
|
|
>
|
|
|
|
<T i18nKey="restore">#</T>
|
|
|
|
</span>
|
|
|
|
)}
|
2019-08-20 02:37:32 +00:00
|
|
|
</li>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-08-20 02:37:32 +00:00
|
|
|
</ul>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.showRemoveDialog && (
|
2019-08-20 02:37:32 +00:00
|
|
|
<form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
|
|
|
|
<div class="form-group row">
|
2019-10-19 00:20:27 +00:00
|
|
|
<label class="col-form-label">
|
|
|
|
<T i18nKey="reason">#</T>
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
class="form-control mr-2"
|
|
|
|
placeholder={i18n.t('optional')}
|
|
|
|
value={this.state.removeReason}
|
|
|
|
onInput={linkEvent(this, this.handleModRemoveReasonChange)}
|
|
|
|
/>
|
2019-08-20 02:37:32 +00:00
|
|
|
</div>
|
|
|
|
{/* TODO hold off on expires for now */}
|
|
|
|
{/* <div class="form-group row"> */}
|
2019-10-19 00:20:27 +00:00
|
|
|
{/* <label class="col-form-label">Expires</label> */}
|
|
|
|
{/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
|
|
|
|
{/* </div> */}
|
|
|
|
<div class="form-group row">
|
|
|
|
<button type="submit" class="btn btn-secondary">
|
|
|
|
<T i18nKey="remove_community">#</T>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
)}
|
2019-08-20 02:37:32 +00:00
|
|
|
<ul class="my-1 list-inline">
|
2019-10-19 00:20:27 +00:00
|
|
|
<li className="list-inline-item">
|
|
|
|
<Link className="badge badge-secondary" to="/communities">
|
|
|
|
{community.category_name}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item badge badge-secondary">
|
|
|
|
<T
|
|
|
|
i18nKey="number_of_subscribers"
|
|
|
|
interpolation={{ count: community.number_of_subscribers }}
|
|
|
|
>
|
|
|
|
#
|
|
|
|
</T>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item badge badge-secondary">
|
|
|
|
<T
|
|
|
|
i18nKey="number_of_posts"
|
|
|
|
interpolation={{ count: community.number_of_posts }}
|
|
|
|
>
|
|
|
|
#
|
|
|
|
</T>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item badge badge-secondary">
|
|
|
|
<T
|
|
|
|
i18nKey="number_of_comments"
|
|
|
|
interpolation={{ count: community.number_of_comments }}
|
|
|
|
>
|
|
|
|
#
|
|
|
|
</T>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<Link
|
|
|
|
className="badge badge-secondary"
|
|
|
|
to={`/modlog/community/${this.props.community.id}`}
|
|
|
|
>
|
|
|
|
<T i18nKey="modlog">#</T>
|
|
|
|
</Link>
|
|
|
|
</li>
|
2019-08-20 02:37:32 +00:00
|
|
|
</ul>
|
2019-10-19 00:20:27 +00:00
|
|
|
<ul class="list-inline small">
|
2019-08-20 02:37:32 +00:00
|
|
|
<li class="list-inline-item">{i18n.t('mods')}: </li>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.props.moderators.map(mod => (
|
|
|
|
<li class="list-inline-item">
|
|
|
|
<Link class="text-info" to={`/u/${mod.user_name}`}>
|
|
|
|
{mod.user_name}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
2019-08-20 02:37:32 +00:00
|
|
|
</ul>
|
2019-10-19 00:20:27 +00:00
|
|
|
<Link
|
|
|
|
class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
|
|
|
|
community.removed) &&
|
|
|
|
'no-click'}`}
|
|
|
|
to={`/create_post?community=${community.name}`}
|
|
|
|
>
|
|
|
|
<T i18nKey="create_a_post">#</T>
|
|
|
|
</Link>
|
|
|
|
<div>
|
|
|
|
{community.subscribed ? (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary btn-block"
|
|
|
|
onClick={linkEvent(community.id, this.handleUnsubscribe)}
|
|
|
|
>
|
|
|
|
<T i18nKey="unsubscribe">#</T>
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary btn-block"
|
|
|
|
onClick={linkEvent(community.id, this.handleSubscribe)}
|
|
|
|
>
|
|
|
|
<T i18nKey="subscribe">#</T>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
2019-04-04 20:53:32 +00:00
|
|
|
</div>
|
2019-08-20 02:37:32 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
{community.description && (
|
2019-08-20 02:37:32 +00:00
|
|
|
<div class="card border-secondary">
|
|
|
|
<div class="card-body">
|
2019-10-19 00:20:27 +00:00
|
|
|
<div
|
|
|
|
className="md-div"
|
|
|
|
dangerouslySetInnerHTML={mdToHtml(community.description)}
|
|
|
|
/>
|
2019-08-20 02:37:32 +00:00
|
|
|
</div>
|
2019-04-15 23:12:06 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2019-04-03 23:01:20 +00:00
|
|
|
);
|
|
|
|
}
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleEditClick(i: Sidebar) {
|
2019-04-04 22:29:14 +00:00
|
|
|
i.state.showEdit = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleEditCommunity() {
|
2019-04-04 22:29:14 +00:00
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:08:21 +00:00
|
|
|
handleEditCancel() {
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-29 19:14:54 +00:00
|
|
|
handleDeleteClick(i: Sidebar) {
|
|
|
|
event.preventDefault();
|
|
|
|
let deleteForm: CommunityFormI = {
|
|
|
|
name: i.props.community.name,
|
|
|
|
title: i.props.community.title,
|
|
|
|
category_id: i.props.community.category_id,
|
|
|
|
edit_id: i.props.community.id,
|
|
|
|
deleted: !i.props.community.deleted,
|
2019-08-25 07:22:46 +00:00
|
|
|
nsfw: i.props.community.nsfw,
|
2019-04-29 19:14:54 +00:00
|
|
|
auth: null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.editCommunity(deleteForm);
|
|
|
|
}
|
2019-04-04 22:29:14 +00:00
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
handleUnsubscribe(communityId: number) {
|
|
|
|
let form: FollowCommunityForm = {
|
|
|
|
community_id: communityId,
|
2019-10-19 00:20:27 +00:00
|
|
|
follow: false,
|
2019-04-05 06:26:38 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.followCommunity(form);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubscribe(communityId: number) {
|
|
|
|
let form: FollowCommunityForm = {
|
|
|
|
community_id: communityId,
|
2019-10-19 00:20:27 +00:00
|
|
|
follow: true,
|
2019-04-05 06:26:38 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.followCommunity(form);
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
private get amCreator(): boolean {
|
2019-04-16 23:04:23 +00:00
|
|
|
return this.props.community.creator_id == UserService.Instance.user.id;
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 20:52:55 +00:00
|
|
|
get canMod(): boolean {
|
2019-10-19 00:20:27 +00:00
|
|
|
return (
|
|
|
|
UserService.Instance.user &&
|
|
|
|
this.props.moderators
|
|
|
|
.map(m => m.user_id)
|
|
|
|
.includes(UserService.Instance.user.id)
|
|
|
|
);
|
2019-04-21 20:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get canAdmin(): boolean {
|
2019-10-19 00:20:27 +00:00
|
|
|
return (
|
|
|
|
UserService.Instance.user &&
|
|
|
|
this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
|
|
|
|
);
|
2019-04-21 20:52:55 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
handleModRemoveShow(i: Sidebar) {
|
|
|
|
i.state.showRemoveDialog = true;
|
|
|
|
i.setState(i.state);
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
2019-04-15 23:12:06 +00:00
|
|
|
|
|
|
|
handleModRemoveReasonChange(i: Sidebar, event: any) {
|
|
|
|
i.state.removeReason = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModRemoveExpiresChange(i: Sidebar, event: any) {
|
|
|
|
console.log(event.target.value);
|
|
|
|
i.state.removeExpires = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleModRemoveSubmit(i: Sidebar) {
|
2019-04-16 23:04:23 +00:00
|
|
|
event.preventDefault();
|
2019-04-15 23:12:06 +00:00
|
|
|
let deleteForm: CommunityFormI = {
|
|
|
|
name: i.props.community.name,
|
|
|
|
title: i.props.community.title,
|
|
|
|
category_id: i.props.community.category_id,
|
|
|
|
edit_id: i.props.community.id,
|
|
|
|
removed: !i.props.community.removed,
|
|
|
|
reason: i.state.removeReason,
|
|
|
|
expires: getUnixTime(i.state.removeExpires),
|
2019-08-25 07:22:46 +00:00
|
|
|
nsfw: i.props.community.nsfw,
|
2019-04-15 23:12:06 +00:00
|
|
|
auth: null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.editCommunity(deleteForm);
|
|
|
|
|
|
|
|
i.state.showRemoveDialog = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|