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">
207 <Link class="text-info" to={`/u/${mod.user_name}`}>
208 {mod.avatar && showAvatars() && (
212 src={pictshareAvatarThumbnail(mod.avatar)}
213 class="rounded-circle mr-1"
216 <span>{mod.user_name}</span>
222 class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
223 community.removed) &&
225 to={`/create_post?community=${community.name}`}
227 {i18n.t('create_a_post')}
230 {community.subscribed ? (
232 class="btn btn-sm btn-secondary btn-block"
233 onClick={linkEvent(community.id, this.handleUnsubscribe)}
235 {i18n.t('unsubscribe')}
239 class="btn btn-sm btn-secondary btn-block"
240 onClick={linkEvent(community.id, this.handleSubscribe)}
242 {i18n.t('subscribe')}
248 {community.description && (
249 <div class="card border-secondary">
250 <div class="card-body">
253 dangerouslySetInnerHTML={mdToHtml(community.description)}
262 handleEditClick(i: Sidebar) {
263 i.state.showEdit = true;
267 handleEditCommunity() {
268 this.state.showEdit = false;
269 this.setState(this.state);
273 this.state.showEdit = false;
274 this.setState(this.state);
277 handleDeleteClick(i: Sidebar) {
278 event.preventDefault();
279 let deleteForm: CommunityFormI = {
280 name: i.props.community.name,
281 title: i.props.community.title,
282 category_id: i.props.community.category_id,
283 edit_id: i.props.community.id,
284 deleted: !i.props.community.deleted,
285 nsfw: i.props.community.nsfw,
288 WebSocketService.Instance.editCommunity(deleteForm);
291 handleUnsubscribe(communityId: number) {
292 let form: FollowCommunityForm = {
293 community_id: communityId,
296 WebSocketService.Instance.followCommunity(form);
299 handleSubscribe(communityId: number) {
300 let form: FollowCommunityForm = {
301 community_id: communityId,
304 WebSocketService.Instance.followCommunity(form);
307 private get amCreator(): boolean {
308 return this.props.community.creator_id == UserService.Instance.user.id;
311 get canMod(): boolean {
313 UserService.Instance.user &&
314 this.props.moderators
316 .includes(UserService.Instance.user.id)
320 get canAdmin(): boolean {
322 UserService.Instance.user &&
323 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
327 handleModRemoveShow(i: Sidebar) {
328 i.state.showRemoveDialog = true;
332 handleModRemoveReasonChange(i: Sidebar, event: any) {
333 i.state.removeReason = event.target.value;
337 handleModRemoveExpiresChange(i: Sidebar, event: any) {
338 console.log(event.target.value);
339 i.state.removeExpires = event.target.value;
343 handleModRemoveSubmit(i: Sidebar) {
344 event.preventDefault();
345 let deleteForm: CommunityFormI = {
346 name: i.props.community.name,
347 title: i.props.community.title,
348 category_id: i.props.community.category_id,
349 edit_id: i.props.community.id,
350 removed: !i.props.community.removed,
351 reason: i.state.removeReason,
352 expires: getUnixTime(i.state.removeExpires),
353 nsfw: i.props.community.nsfw,
356 WebSocketService.Instance.editCommunity(deleteForm);
358 i.state.showRemoveDialog = false;