1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
7 CommunityForm as CommunityFormI,
9 } from '../interfaces';
10 import { WebSocketService, UserService } from '../services';
14 pictshareAvatarThumbnail,
17 import { CommunityForm } from './community-form';
18 import { i18n } from '../i18next';
19 import { T } from 'inferno-i18next';
21 interface SidebarProps {
23 moderators: Array<CommunityUser>;
24 admins: Array<UserView>;
27 interface SidebarState {
29 showRemoveDialog: boolean;
31 removeExpires: string;
34 export class Sidebar extends Component<SidebarProps, SidebarState> {
35 private emptyState: SidebarState = {
37 showRemoveDialog: false,
42 constructor(props: any, context: any) {
43 super(props, context);
44 this.state = this.emptyState;
45 this.handleEditCommunity = this.handleEditCommunity.bind(this);
46 this.handleEditCancel = this.handleEditCancel.bind(this);
52 {!this.state.showEdit ? (
56 community={this.props.community}
57 onEdit={this.handleEditCommunity}
58 onCancel={this.handleEditCancel}
66 let community = this.props.community;
69 <div class="card border-secondary mb-3">
70 <div class="card-body">
72 <span>{community.title}</span>
73 {community.removed && (
74 <small className="ml-2 text-muted font-italic">
78 {community.deleted && (
79 <small className="ml-2 text-muted font-italic">
80 <T i18nKey="deleted">#</T>
84 <Link className="text-muted" to={`/c/${community.name}`}>
87 <ul class="list-inline mb-1 text-muted small font-weight-bold">
90 <li className="list-inline-item">
93 onClick={linkEvent(this, this.handleEditClick)}
99 <li className="list-inline-item">
102 onClick={linkEvent(this, this.handleDeleteClick)}
113 <li className="list-inline-item">
114 {!this.props.community.removed ? (
117 onClick={linkEvent(this, this.handleModRemoveShow)}
124 onClick={linkEvent(this, this.handleModRemoveSubmit)}
126 { i18n.t('restore') }
132 {this.state.showRemoveDialog && (
133 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
134 <div class="form-group row">
135 <label class="col-form-label" for="remove-reason">
141 class="form-control mr-2"
142 placeholder={i18n.t('optional')}
143 value={this.state.removeReason}
144 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
147 {/* TODO hold off on expires for now */}
148 {/* <div class="form-group row"> */}
149 {/* <label class="col-form-label">Expires</label> */}
150 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
152 <div class="form-group row">
153 <button type="submit" class="btn btn-secondary">
154 { i18n.t('remove_community') }
159 <ul class="my-1 list-inline">
160 <li className="list-inline-item">
161 <Link className="badge badge-secondary" to="/communities">
162 {community.category_name}
165 <li className="list-inline-item badge badge-secondary">
167 i18nKey="number_of_subscribers"
168 interpolation={{ count: community.number_of_subscribers }}
173 <li className="list-inline-item badge badge-secondary">
175 i18nKey="number_of_posts"
176 interpolation={{ count: community.number_of_posts }}
181 <li className="list-inline-item badge badge-secondary">
183 i18nKey="number_of_comments"
184 interpolation={{ count: community.number_of_comments }}
189 <li className="list-inline-item">
191 className="badge badge-secondary"
192 to={`/modlog/community/${this.props.community.id}`}
194 <T i18nKey="modlog">#</T>
198 <ul class="list-inline small">
199 <li class="list-inline-item">{i18n.t('mods')}: </li>
200 {this.props.moderators.map(mod => (
201 <li class="list-inline-item">
202 <Link class="text-info" to={`/u/${mod.user_name}`}>
203 {mod.avatar && showAvatars() && (
207 src={pictshareAvatarThumbnail(mod.avatar)}
208 class="rounded-circle mr-1"
211 <span>{mod.user_name}</span>
217 class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
218 community.removed) &&
220 to={`/create_post?community=${community.name}`}
222 { i18n.t('create_a_post') }
225 {community.subscribed ? (
227 class="btn btn-sm btn-secondary btn-block"
228 onClick={linkEvent(community.id, this.handleUnsubscribe)}
230 { i18n.t('unsubscribe') }
234 class="btn btn-sm btn-secondary btn-block"
235 onClick={linkEvent(community.id, this.handleSubscribe)}
237 { i18n.t('subscribe') }
243 {community.description && (
244 <div class="card border-secondary">
245 <div class="card-body">
248 dangerouslySetInnerHTML={mdToHtml(community.description)}
257 handleEditClick(i: Sidebar) {
258 i.state.showEdit = true;
262 handleEditCommunity() {
263 this.state.showEdit = false;
264 this.setState(this.state);
268 this.state.showEdit = false;
269 this.setState(this.state);
272 handleDeleteClick(i: Sidebar) {
273 event.preventDefault();
274 let deleteForm: CommunityFormI = {
275 name: i.props.community.name,
276 title: i.props.community.title,
277 category_id: i.props.community.category_id,
278 edit_id: i.props.community.id,
279 deleted: !i.props.community.deleted,
280 nsfw: i.props.community.nsfw,
283 WebSocketService.Instance.editCommunity(deleteForm);
286 handleUnsubscribe(communityId: number) {
287 let form: FollowCommunityForm = {
288 community_id: communityId,
291 WebSocketService.Instance.followCommunity(form);
294 handleSubscribe(communityId: number) {
295 let form: FollowCommunityForm = {
296 community_id: communityId,
299 WebSocketService.Instance.followCommunity(form);
302 private get amCreator(): boolean {
303 return this.props.community.creator_id == UserService.Instance.user.id;
306 get canMod(): boolean {
308 UserService.Instance.user &&
309 this.props.moderators
311 .includes(UserService.Instance.user.id)
315 get canAdmin(): boolean {
317 UserService.Instance.user &&
318 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
322 handleModRemoveShow(i: Sidebar) {
323 i.state.showRemoveDialog = true;
327 handleModRemoveReasonChange(i: Sidebar, event: any) {
328 i.state.removeReason = event.target.value;
332 handleModRemoveExpiresChange(i: Sidebar, event: any) {
333 console.log(event.target.value);
334 i.state.removeExpires = event.target.value;
338 handleModRemoveSubmit(i: Sidebar) {
339 event.preventDefault();
340 let deleteForm: CommunityFormI = {
341 name: i.props.community.name,
342 title: i.props.community.title,
343 category_id: i.props.community.category_id,
344 edit_id: i.props.community.id,
345 removed: !i.props.community.removed,
346 reason: i.state.removeReason,
347 expires: getUnixTime(i.state.removeExpires),
348 nsfw: i.props.community.nsfw,
351 WebSocketService.Instance.editCommunity(deleteForm);
353 i.state.showRemoveDialog = false;