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 { UserListing } from './user-listing';
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);
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">
79 {community.deleted && (
80 <small className="ml-2 text-muted font-italic">
85 <Link className="text-muted" to={`/c/${community.name}`}>
88 <ul class="list-inline mb-1 text-muted font-weight-bold">
91 <li className="list-inline-item-action">
94 onClick={linkEvent(this, this.handleEditClick)}
95 data-tippy-content={i18n.t('edit')}
97 <svg class="icon icon-inline">
98 <use xlinkHref="#icon-edit"></use>
103 <li className="list-inline-item-action">
106 onClick={linkEvent(this, this.handleDeleteClick)}
114 class={`icon icon-inline ${
115 community.deleted && 'text-danger'
118 <use xlinkHref="#icon-trash"></use>
126 <li className="list-inline-item">
127 {!this.props.community.removed ? (
130 onClick={linkEvent(this, this.handleModRemoveShow)}
137 onClick={linkEvent(this, this.handleModRemoveSubmit)}
145 {this.state.showRemoveDialog && (
146 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
147 <div class="form-group row">
148 <label class="col-form-label" htmlFor="remove-reason">
154 class="form-control mr-2"
155 placeholder={i18n.t('optional')}
156 value={this.state.removeReason}
157 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
160 {/* TODO hold off on expires for now */}
161 {/* <div class="form-group row"> */}
162 {/* <label class="col-form-label">Expires</label> */}
163 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
165 <div class="form-group row">
166 <button type="submit" class="btn btn-secondary">
167 {i18n.t('remove_community')}
172 <ul class="my-1 list-inline">
173 <li className="list-inline-item badge badge-secondary">
174 {i18n.t('number_online', { count: this.props.online })}
176 <li className="list-inline-item badge badge-secondary">
177 {i18n.t('number_of_subscribers', {
178 count: community.number_of_subscribers,
181 <li className="list-inline-item badge badge-secondary">
182 {i18n.t('number_of_posts', {
183 count: community.number_of_posts,
186 <li className="list-inline-item badge badge-secondary">
187 {i18n.t('number_of_comments', {
188 count: community.number_of_comments,
191 <li className="list-inline-item">
192 <Link className="badge badge-secondary" to="/communities">
193 {community.category_name}
196 <li className="list-inline-item">
198 className="badge badge-secondary"
199 to={`/modlog/community/${this.props.community.id}`}
205 <ul class="list-inline small">
206 <li class="list-inline-item">{i18n.t('mods')}: </li>
207 {this.props.moderators.map(mod => (
208 <li class="list-inline-item">
219 class={`btn btn-sm btn-secondary btn-block mb-3 ${
220 (community.deleted || community.removed) && 'no-click'
222 to={`/create_post?community=${community.name}`}
224 {i18n.t('create_a_post')}
227 {community.subscribed ? (
229 class="btn btn-sm btn-secondary btn-block"
230 onClick={linkEvent(community.id, this.handleUnsubscribe)}
232 {i18n.t('unsubscribe')}
236 class="btn btn-sm btn-secondary btn-block"
237 onClick={linkEvent(community.id, this.handleSubscribe)}
239 {i18n.t('subscribe')}
245 {community.description && (
246 <div class="card border-secondary">
247 <div class="card-body">
250 dangerouslySetInnerHTML={mdToHtml(community.description)}
259 handleEditClick(i: Sidebar) {
260 i.state.showEdit = true;
264 handleEditCommunity() {
265 this.state.showEdit = false;
266 this.setState(this.state);
270 this.state.showEdit = false;
271 this.setState(this.state);
274 handleDeleteClick(i: Sidebar) {
275 event.preventDefault();
276 let deleteForm: CommunityFormI = {
277 name: i.props.community.name,
278 title: i.props.community.title,
279 category_id: i.props.community.category_id,
280 edit_id: i.props.community.id,
281 deleted: !i.props.community.deleted,
282 nsfw: i.props.community.nsfw,
285 WebSocketService.Instance.editCommunity(deleteForm);
288 handleUnsubscribe(communityId: number) {
289 let form: FollowCommunityForm = {
290 community_id: communityId,
293 WebSocketService.Instance.followCommunity(form);
296 handleSubscribe(communityId: number) {
297 let form: FollowCommunityForm = {
298 community_id: communityId,
301 WebSocketService.Instance.followCommunity(form);
304 private get amCreator(): boolean {
305 return this.props.community.creator_id == UserService.Instance.user.id;
308 get canMod(): boolean {
310 UserService.Instance.user &&
311 this.props.moderators
313 .includes(UserService.Instance.user.id)
317 get canAdmin(): boolean {
319 UserService.Instance.user &&
320 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
324 handleModRemoveShow(i: Sidebar) {
325 i.state.showRemoveDialog = true;
329 handleModRemoveReasonChange(i: Sidebar, event: any) {
330 i.state.removeReason = event.target.value;
334 handleModRemoveExpiresChange(i: Sidebar, event: any) {
335 console.log(event.target.value);
336 i.state.removeExpires = event.target.value;
340 handleModRemoveSubmit(i: Sidebar) {
341 event.preventDefault();
342 let deleteForm: CommunityFormI = {
343 name: i.props.community.name,
344 title: i.props.community.title,
345 category_id: i.props.community.category_id,
346 edit_id: i.props.community.id,
347 removed: !i.props.community.removed,
348 reason: i.state.removeReason,
349 expires: getUnixTime(i.state.removeExpires),
350 nsfw: i.props.community.nsfw,
353 WebSocketService.Instance.editCommunity(deleteForm);
355 i.state.showRemoveDialog = false;