1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Community, CommunityUser, FollowCommunityForm, CommunityForm as CommunityFormI, UserView } from '../interfaces';
4 import { WebSocketService, UserService } from '../services';
5 import { mdToHtml, getUnixTime } from '../utils';
6 import { CommunityForm } from './community-form';
7 import { i18n } from '../i18next';
8 import { T } from 'inferno-i18next';
10 interface SidebarProps {
12 moderators: Array<CommunityUser>;
13 admins: Array<UserView>;
16 interface SidebarState {
18 showRemoveDialog: boolean;
20 removeExpires: string;
23 export class Sidebar extends Component<SidebarProps, SidebarState> {
25 private emptyState: SidebarState = {
27 showRemoveDialog: false,
32 constructor(props: any, context: any) {
33 super(props, context);
34 this.state = this.emptyState;
35 this.handleEditCommunity = this.handleEditCommunity.bind(this);
36 this.handleEditCancel = this.handleEditCancel.bind(this);
45 community={this.props.community}
46 onEdit={this.handleEditCommunity}
47 onCancel={this.handleEditCancel} />
54 let community = this.props.community;
57 <h5 className="mb-0">{community.title}
59 <small className="ml-2 text-muted font-italic"><T i18nKey="removed">#</T></small>
62 <small className="ml-2 text-muted font-italic"><T i18nKey="deleted">#</T></small>
65 <Link className="text-muted" to={`/c/${community.name}`}>/c/{community.name}</Link>
66 <ul class="list-inline mb-1 text-muted small font-weight-bold">
69 <li className="list-inline-item">
70 <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}><T i18nKey="edit">#</T></span>
73 <li className="list-inline-item">
74 <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>
75 {!community.deleted ? i18n.t('delete') : i18n.t('restore')}
82 <li className="list-inline-item">
83 {!this.props.community.removed ?
84 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}><T i18nKey="remove">#</T></span> :
85 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}><T i18nKey="restore">#</T></span>
91 {this.state.showRemoveDialog &&
92 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
93 <div class="form-group row">
94 <label class="col-form-label"><T i18nKey="reason">#</T></label>
95 <input type="text" class="form-control mr-2" placeholder={i18n.t('optional')} value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
97 {/* TODO hold off on expires for now */}
98 {/* <div class="form-group row"> */}
99 {/* <label class="col-form-label">Expires</label> */}
100 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
102 <div class="form-group row">
103 <button type="submit" class="btn btn-secondary"><T i18nKey="remove_community">#</T></button>
107 <ul class="my-1 list-inline">
108 <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
109 <li className="list-inline-item badge badge-light"><T i18nKey="number_of_subscribers" interpolation={{count: community.number_of_subscribers}}>#</T></li>
110 <li className="list-inline-item badge badge-light"><T i18nKey="number_of_posts" interpolation={{count: community.number_of_posts}}>#</T></li>
111 <li className="list-inline-item badge badge-light"><T i18nKey="number_of_comments" interpolation={{count: community.number_of_comments}}>#</T></li>
112 <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}><T i18nKey="modlog">#</T></Link></li>
114 <ul class="list-inline small">
115 <li class="list-inline-item">{i18n.t('mods')}: </li>
116 {this.props.moderators.map(mod =>
117 <li class="list-inline-item"><Link class="text-info" to={`/u/${mod.user_name}`}>{mod.user_name}</Link></li>
120 <Link class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted || community.removed) && 'no-click'}`}
121 to={`/create_post/c/${community.name}`}><T i18nKey="create_a_post">#</T></Link>
123 {community.subscribed
124 ? <button class="btn btn-sm btn-secondary btn-block mb-3" onClick={linkEvent(community.id, this.handleUnsubscribe)}><T i18nKey="unsubscribe">#</T></button>
125 : <button class="btn btn-sm btn-secondary btn-block mb-3" onClick={linkEvent(community.id, this.handleSubscribe)}><T i18nKey="subscribe">#</T></button>
128 {community.description &&
131 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
139 handleEditClick(i: Sidebar) {
140 i.state.showEdit = true;
144 handleEditCommunity() {
145 this.state.showEdit = false;
146 this.setState(this.state);
150 this.state.showEdit = false;
151 this.setState(this.state);
154 handleDeleteClick(i: Sidebar) {
155 event.preventDefault();
156 let deleteForm: CommunityFormI = {
157 name: i.props.community.name,
158 title: i.props.community.title,
159 category_id: i.props.community.category_id,
160 edit_id: i.props.community.id,
161 deleted: !i.props.community.deleted,
164 WebSocketService.Instance.editCommunity(deleteForm);
167 handleUnsubscribe(communityId: number) {
168 let form: FollowCommunityForm = {
169 community_id: communityId,
172 WebSocketService.Instance.followCommunity(form);
175 handleSubscribe(communityId: number) {
176 let form: FollowCommunityForm = {
177 community_id: communityId,
180 WebSocketService.Instance.followCommunity(form);
183 private get amCreator(): boolean {
184 return this.props.community.creator_id == UserService.Instance.user.id;
187 get canMod(): boolean {
188 return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
191 get canAdmin(): boolean {
192 return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
195 handleModRemoveShow(i: Sidebar) {
196 i.state.showRemoveDialog = true;
200 handleModRemoveReasonChange(i: Sidebar, event: any) {
201 i.state.removeReason = event.target.value;
205 handleModRemoveExpiresChange(i: Sidebar, event: any) {
206 console.log(event.target.value);
207 i.state.removeExpires = event.target.value;
211 handleModRemoveSubmit(i: Sidebar) {
212 event.preventDefault();
213 let deleteForm: CommunityFormI = {
214 name: i.props.community.name,
215 title: i.props.community.title,
216 category_id: i.props.community.category_id,
217 edit_id: i.props.community.id,
218 removed: !i.props.community.removed,
219 reason: i.state.removeReason,
220 expires: getUnixTime(i.state.removeExpires),
223 WebSocketService.Instance.editCommunity(deleteForm);
225 i.state.showRemoveDialog = false;