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>;
28 interface SidebarState {
30 showRemoveDialog: boolean;
32 removeExpires: string;
35 export class Sidebar extends Component<SidebarProps, SidebarState> {
36 private emptyState: SidebarState = {
38 showRemoveDialog: false,
43 constructor(props: any, context: any) {
44 super(props, context);
45 this.state = this.emptyState;
46 this.handleEditCommunity = this.handleEditCommunity.bind(this);
47 this.handleEditCancel = this.handleEditCancel.bind(this);
53 {!this.state.showEdit ? (
57 community={this.props.community}
58 onEdit={this.handleEditCommunity}
59 onCancel={this.handleEditCancel}
67 let community = this.props.community;
70 <div class="card border-secondary mb-3">
71 <div class="card-body">
73 <span>{community.title}</span>
74 {community.removed && (
75 <small className="ml-2 text-muted font-italic">
76 <T i18nKey="removed">#</T>
79 {community.deleted && (
80 <small className="ml-2 text-muted font-italic">
81 <T i18nKey="deleted">#</T>
85 <Link className="text-muted" to={`/c/${community.name}`}>
88 <ul class="list-inline mb-1 text-muted small font-weight-bold">
91 <li className="list-inline-item">
94 onClick={linkEvent(this, this.handleEditClick)}
96 <T i18nKey="edit">#</T>
100 <li className="list-inline-item">
103 onClick={linkEvent(this, this.handleDeleteClick)}
114 <li className="list-inline-item">
115 {!this.props.community.removed ? (
118 onClick={linkEvent(this, this.handleModRemoveShow)}
120 <T i18nKey="remove">#</T>
125 onClick={linkEvent(this, this.handleModRemoveSubmit)}
127 <T i18nKey="restore">#</T>
133 {this.state.showRemoveDialog && (
134 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
135 <div class="form-group row">
136 <label class="col-form-label">
137 <T i18nKey="reason">#</T>
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 <T i18nKey="remove_community">#</T>
159 <ul class="my-1 list-inline">
160 <li className="list-inline-item badge badge-secondary">
162 i18nKey="number_online"
163 interpolation={{ count: this.props.online }}
168 <li className="list-inline-item badge badge-secondary">
170 i18nKey="number_of_subscribers"
171 interpolation={{ count: community.number_of_subscribers }}
176 <li className="list-inline-item badge badge-secondary">
178 i18nKey="number_of_posts"
179 interpolation={{ count: community.number_of_posts }}
184 <li className="list-inline-item badge badge-secondary">
186 i18nKey="number_of_comments"
187 interpolation={{ count: community.number_of_comments }}
192 <li className="list-inline-item">
193 <Link className="badge badge-secondary" to="/communities">
194 {community.category_name}
197 <li className="list-inline-item">
199 className="badge badge-secondary"
200 to={`/modlog/community/${this.props.community.id}`}
202 <T i18nKey="modlog">#</T>
206 <ul class="list-inline small">
207 <li class="list-inline-item">{i18n.t('mods')}: </li>
208 {this.props.moderators.map(mod => (
209 <li class="list-inline-item">
210 <Link class="text-info" to={`/u/${mod.user_name}`}>
211 {mod.avatar && showAvatars() && (
215 src={pictshareAvatarThumbnail(mod.avatar)}
216 class="rounded-circle mr-1"
219 <span>{mod.user_name}</span>
225 class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
226 community.removed) &&
228 to={`/create_post?community=${community.name}`}
230 <T i18nKey="create_a_post">#</T>
233 {community.subscribed ? (
235 class="btn btn-sm btn-secondary btn-block"
236 onClick={linkEvent(community.id, this.handleUnsubscribe)}
238 <T i18nKey="unsubscribe">#</T>
242 class="btn btn-sm btn-secondary btn-block"
243 onClick={linkEvent(community.id, this.handleSubscribe)}
245 <T i18nKey="subscribe">#</T>
251 {community.description && (
252 <div class="card border-secondary">
253 <div class="card-body">
256 dangerouslySetInnerHTML={mdToHtml(community.description)}
265 handleEditClick(i: Sidebar) {
266 i.state.showEdit = true;
270 handleEditCommunity() {
271 this.state.showEdit = false;
272 this.setState(this.state);
276 this.state.showEdit = false;
277 this.setState(this.state);
280 handleDeleteClick(i: Sidebar) {
281 event.preventDefault();
282 let deleteForm: CommunityFormI = {
283 name: i.props.community.name,
284 title: i.props.community.title,
285 category_id: i.props.community.category_id,
286 edit_id: i.props.community.id,
287 deleted: !i.props.community.deleted,
288 nsfw: i.props.community.nsfw,
291 WebSocketService.Instance.editCommunity(deleteForm);
294 handleUnsubscribe(communityId: number) {
295 let form: FollowCommunityForm = {
296 community_id: communityId,
299 WebSocketService.Instance.followCommunity(form);
302 handleSubscribe(communityId: number) {
303 let form: FollowCommunityForm = {
304 community_id: communityId,
307 WebSocketService.Instance.followCommunity(form);
310 private get amCreator(): boolean {
311 return this.props.community.creator_id == UserService.Instance.user.id;
314 get canMod(): boolean {
316 UserService.Instance.user &&
317 this.props.moderators
319 .includes(UserService.Instance.user.id)
323 get canAdmin(): boolean {
325 UserService.Instance.user &&
326 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
330 handleModRemoveShow(i: Sidebar) {
331 i.state.showRemoveDialog = true;
335 handleModRemoveReasonChange(i: Sidebar, event: any) {
336 i.state.removeReason = event.target.value;
340 handleModRemoveExpiresChange(i: Sidebar, event: any) {
341 console.log(event.target.value);
342 i.state.removeExpires = event.target.value;
346 handleModRemoveSubmit(i: Sidebar) {
347 event.preventDefault();
348 let deleteForm: CommunityFormI = {
349 name: i.props.community.name,
350 title: i.props.community.title,
351 category_id: i.props.community.category_id,
352 edit_id: i.props.community.id,
353 removed: !i.props.community.removed,
354 reason: i.state.removeReason,
355 expires: getUnixTime(i.state.removeExpires),
356 nsfw: i.props.community.nsfw,
359 WebSocketService.Instance.editCommunity(deleteForm);
361 i.state.showRemoveDialog = false;