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,
18 import { CommunityForm } from './community-form';
19 import { i18n } from '../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);
50 componentDidUpdate() {
57 {!this.state.showEdit ? (
61 community={this.props.community}
62 onEdit={this.handleEditCommunity}
63 onCancel={this.handleEditCancel}
71 let community = this.props.community;
74 <div class="card border-secondary mb-3">
75 <div class="card-body">
77 <span>{community.title}</span>
78 {community.removed && (
79 <small className="ml-2 text-muted font-italic">
83 {community.deleted && (
84 <small className="ml-2 text-muted font-italic">
89 <Link className="text-muted" to={`/c/${community.name}`}>
92 <ul class="list-inline mb-1 text-muted font-weight-bold">
95 <li className="list-inline-item-action">
98 onClick={linkEvent(this, this.handleEditClick)}
99 data-tippy-content={i18n.t('edit')}
101 <svg class="icon icon-inline">
102 <use xlinkHref="#icon-edit"></use>
107 <li className="list-inline-item-action">
110 onClick={linkEvent(this, this.handleDeleteClick)}
118 class={`icon icon-inline ${community.deleted &&
121 <use xlinkHref="#icon-trash"></use>
129 <li className="list-inline-item">
130 {!this.props.community.removed ? (
133 onClick={linkEvent(this, this.handleModRemoveShow)}
140 onClick={linkEvent(this, this.handleModRemoveSubmit)}
148 {this.state.showRemoveDialog && (
149 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
150 <div class="form-group row">
151 <label class="col-form-label" htmlFor="remove-reason">
157 class="form-control mr-2"
158 placeholder={i18n.t('optional')}
159 value={this.state.removeReason}
160 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
163 {/* TODO hold off on expires for now */}
164 {/* <div class="form-group row"> */}
165 {/* <label class="col-form-label">Expires</label> */}
166 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
168 <div class="form-group row">
169 <button type="submit" class="btn btn-secondary">
170 {i18n.t('remove_community')}
175 <ul class="my-1 list-inline">
176 <li className="list-inline-item badge badge-secondary">
177 {i18n.t('number_online', { count: this.props.online })}
179 <li className="list-inline-item badge badge-secondary">
180 {i18n.t('number_of_subscribers', {
181 count: community.number_of_subscribers,
184 <li className="list-inline-item badge badge-secondary">
185 {i18n.t('number_of_posts', {
186 count: community.number_of_posts,
189 <li className="list-inline-item badge badge-secondary">
190 {i18n.t('number_of_comments', {
191 count: community.number_of_comments,
194 <li className="list-inline-item">
195 <Link className="badge badge-secondary" to="/communities">
196 {community.category_name}
199 <li className="list-inline-item">
201 className="badge badge-secondary"
202 to={`/modlog/community/${this.props.community.id}`}
208 <ul class="list-inline small">
209 <li class="list-inline-item">{i18n.t('mods')}: </li>
210 {this.props.moderators.map(mod => (
211 <li class="list-inline-item">
212 <Link class="text-info" to={`/u/${mod.user_name}`}>
213 {mod.avatar && showAvatars() && (
217 src={pictshareAvatarThumbnail(mod.avatar)}
218 class="rounded-circle mr-1"
221 <span>{mod.user_name}</span>
227 class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
228 community.removed) &&
230 to={`/create_post?community=${community.name}`}
232 {i18n.t('create_a_post')}
235 {community.subscribed ? (
237 class="btn btn-sm btn-secondary btn-block"
238 onClick={linkEvent(community.id, this.handleUnsubscribe)}
240 {i18n.t('unsubscribe')}
244 class="btn btn-sm btn-secondary btn-block"
245 onClick={linkEvent(community.id, this.handleSubscribe)}
247 {i18n.t('subscribe')}
253 {community.description && (
254 <div class="card border-secondary">
255 <div class="card-body">
258 dangerouslySetInnerHTML={mdToHtml(community.description)}
267 handleEditClick(i: Sidebar) {
268 i.state.showEdit = true;
272 handleEditCommunity() {
273 this.state.showEdit = false;
274 this.setState(this.state);
278 this.state.showEdit = false;
279 this.setState(this.state);
282 handleDeleteClick(i: Sidebar) {
283 event.preventDefault();
284 let deleteForm: CommunityFormI = {
285 name: i.props.community.name,
286 title: i.props.community.title,
287 category_id: i.props.community.category_id,
288 edit_id: i.props.community.id,
289 deleted: !i.props.community.deleted,
290 nsfw: i.props.community.nsfw,
293 WebSocketService.Instance.editCommunity(deleteForm);
296 handleUnsubscribe(communityId: number) {
297 let form: FollowCommunityForm = {
298 community_id: communityId,
301 WebSocketService.Instance.followCommunity(form);
304 handleSubscribe(communityId: number) {
305 let form: FollowCommunityForm = {
306 community_id: communityId,
309 WebSocketService.Instance.followCommunity(form);
312 private get amCreator(): boolean {
313 return this.props.community.creator_id == UserService.Instance.user.id;
316 get canMod(): boolean {
318 UserService.Instance.user &&
319 this.props.moderators
321 .includes(UserService.Instance.user.id)
325 get canAdmin(): boolean {
327 UserService.Instance.user &&
328 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
332 handleModRemoveShow(i: Sidebar) {
333 i.state.showRemoveDialog = true;
337 handleModRemoveReasonChange(i: Sidebar, event: any) {
338 i.state.removeReason = event.target.value;
342 handleModRemoveExpiresChange(i: Sidebar, event: any) {
343 console.log(event.target.value);
344 i.state.removeExpires = event.target.value;
348 handleModRemoveSubmit(i: Sidebar) {
349 event.preventDefault();
350 let deleteForm: CommunityFormI = {
351 name: i.props.community.name,
352 title: i.props.community.title,
353 category_id: i.props.community.category_id,
354 edit_id: i.props.community.id,
355 removed: !i.props.community.removed,
356 reason: i.state.removeReason,
357 expires: getUnixTime(i.state.removeExpires),
358 nsfw: i.props.community.nsfw,
361 WebSocketService.Instance.editCommunity(deleteForm);
363 i.state.showRemoveDialog = false;