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';
20 interface SidebarProps {
22 moderators: Array<CommunityUser>;
23 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">
84 <Link className="text-muted" to={`/c/${community.name}`}>
87 <ul class="list-inline mb-1 text-muted font-weight-bold">
90 <li className="list-inline-item-action">
93 onClick={linkEvent(this, this.handleEditClick)}
94 data-tippy-content={i18n.t('edit')}
96 <svg class="icon icon-inline">
97 <use xlinkHref="#icon-edit"></use>
102 <li className="list-inline-item-action">
105 onClick={linkEvent(this, this.handleDeleteClick)}
113 class={`icon icon-inline ${community.deleted &&
116 <use xlinkHref="#icon-trash"></use>
124 <li className="list-inline-item">
125 {!this.props.community.removed ? (
128 onClick={linkEvent(this, this.handleModRemoveShow)}
135 onClick={linkEvent(this, this.handleModRemoveSubmit)}
143 {this.state.showRemoveDialog && (
144 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
145 <div class="form-group row">
146 <label class="col-form-label" htmlFor="remove-reason">
152 class="form-control mr-2"
153 placeholder={i18n.t('optional')}
154 value={this.state.removeReason}
155 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
158 {/* TODO hold off on expires for now */}
159 {/* <div class="form-group row"> */}
160 {/* <label class="col-form-label">Expires</label> */}
161 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
163 <div class="form-group row">
164 <button type="submit" class="btn btn-secondary">
165 {i18n.t('remove_community')}
170 <ul class="my-1 list-inline">
171 <li className="list-inline-item badge badge-secondary">
172 {i18n.t('number_online', { count: this.props.online })}
174 <li className="list-inline-item badge badge-secondary">
175 {i18n.t('number_of_subscribers', {
176 count: community.number_of_subscribers,
179 <li className="list-inline-item badge badge-secondary">
180 {i18n.t('number_of_posts', {
181 count: community.number_of_posts,
184 <li className="list-inline-item badge badge-secondary">
185 {i18n.t('number_of_comments', {
186 count: community.number_of_comments,
189 <li className="list-inline-item">
190 <Link className="badge badge-secondary" to="/communities">
191 {community.category_name}
194 <li className="list-inline-item">
196 className="badge badge-secondary"
197 to={`/modlog/community/${this.props.community.id}`}
203 <ul class="list-inline small">
204 <li class="list-inline-item">{i18n.t('mods')}: </li>
205 {this.props.moderators.map(mod => (
206 <li class="list-inline-item">
208 class="text-body font-weight-bold"
209 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 {i18n.t('create_a_post')}
233 {community.subscribed ? (
235 class="btn btn-sm btn-secondary btn-block"
236 onClick={linkEvent(community.id, this.handleUnsubscribe)}
238 {i18n.t('unsubscribe')}
242 class="btn btn-sm btn-secondary btn-block"
243 onClick={linkEvent(community.id, this.handleSubscribe)}
245 {i18n.t('subscribe')}
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;