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>;
26 interface SidebarState {
28 showRemoveDialog: boolean;
30 removeExpires: string;
33 export class Sidebar extends Component<SidebarProps, SidebarState> {
34 private emptyState: SidebarState = {
36 showRemoveDialog: false,
41 constructor(props: any, context: any) {
42 super(props, context);
43 this.state = this.emptyState;
44 this.handleEditCommunity = this.handleEditCommunity.bind(this);
45 this.handleEditCancel = this.handleEditCancel.bind(this);
51 {!this.state.showEdit ? (
55 community={this.props.community}
56 onEdit={this.handleEditCommunity}
57 onCancel={this.handleEditCancel}
65 let community = this.props.community;
68 <div class="card border-secondary mb-3">
69 <div class="card-body">
71 <span>{community.title}</span>
72 {community.removed && (
73 <small className="ml-2 text-muted font-italic">
77 {community.deleted && (
78 <small className="ml-2 text-muted font-italic">
83 <Link className="text-muted" to={`/c/${community.name}`}>
86 <ul class="list-inline mb-1 text-muted small font-weight-bold">
89 <li className="list-inline-item">
92 onClick={linkEvent(this, this.handleEditClick)}
98 <li className="list-inline-item">
101 onClick={linkEvent(this, this.handleDeleteClick)}
112 <li className="list-inline-item">
113 {!this.props.community.removed ? (
116 onClick={linkEvent(this, this.handleModRemoveShow)}
123 onClick={linkEvent(this, this.handleModRemoveSubmit)}
131 {this.state.showRemoveDialog && (
132 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
133 <div class="form-group row">
134 <label class="col-form-label" htmlFor="remove-reason">
140 class="form-control mr-2"
141 placeholder={i18n.t('optional')}
142 value={this.state.removeReason}
143 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
146 {/* TODO hold off on expires for now */}
147 {/* <div class="form-group row"> */}
148 {/* <label class="col-form-label">Expires</label> */}
149 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
151 <div class="form-group row">
152 <button type="submit" class="btn btn-secondary">
153 {i18n.t('remove_community')}
158 <ul class="my-1 list-inline">
159 <li className="list-inline-item">
160 <Link className="badge badge-secondary" to="/communities">
161 {community.category_name}
164 <li className="list-inline-item badge badge-secondary">
165 {i18n.t('number_of_subscribers', {
166 count: community.number_of_subscribers,
169 <li className="list-inline-item badge badge-secondary">
170 {i18n.t('number_of_posts', {
171 count: community.number_of_posts,
174 <li className="list-inline-item badge badge-secondary">
175 {i18n.t('number_of_comments', {
176 count: community.number_of_comments,
179 <li className="list-inline-item">
181 className="badge badge-secondary"
182 to={`/modlog/community/${this.props.community.id}`}
188 <ul class="list-inline small">
189 <li class="list-inline-item">{i18n.t('mods')}: </li>
190 {this.props.moderators.map(mod => (
191 <li class="list-inline-item">
192 <Link class="text-info" to={`/u/${mod.user_name}`}>
193 {mod.avatar && showAvatars() && (
197 src={pictshareAvatarThumbnail(mod.avatar)}
198 class="rounded-circle mr-1"
201 <span>{mod.user_name}</span>
207 class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
208 community.removed) &&
210 to={`/create_post?community=${community.name}`}
212 {i18n.t('create_a_post')}
215 {community.subscribed ? (
217 class="btn btn-sm btn-secondary btn-block"
218 onClick={linkEvent(community.id, this.handleUnsubscribe)}
220 {i18n.t('unsubscribe')}
224 class="btn btn-sm btn-secondary btn-block"
225 onClick={linkEvent(community.id, this.handleSubscribe)}
227 {i18n.t('subscribe')}
233 {community.description && (
234 <div class="card border-secondary">
235 <div class="card-body">
238 dangerouslySetInnerHTML={mdToHtml(community.description)}
247 handleEditClick(i: Sidebar) {
248 i.state.showEdit = true;
252 handleEditCommunity() {
253 this.state.showEdit = false;
254 this.setState(this.state);
258 this.state.showEdit = false;
259 this.setState(this.state);
262 handleDeleteClick(i: Sidebar) {
263 event.preventDefault();
264 let deleteForm: CommunityFormI = {
265 name: i.props.community.name,
266 title: i.props.community.title,
267 category_id: i.props.community.category_id,
268 edit_id: i.props.community.id,
269 deleted: !i.props.community.deleted,
270 nsfw: i.props.community.nsfw,
273 WebSocketService.Instance.editCommunity(deleteForm);
276 handleUnsubscribe(communityId: number) {
277 let form: FollowCommunityForm = {
278 community_id: communityId,
281 WebSocketService.Instance.followCommunity(form);
284 handleSubscribe(communityId: number) {
285 let form: FollowCommunityForm = {
286 community_id: communityId,
289 WebSocketService.Instance.followCommunity(form);
292 private get amCreator(): boolean {
293 return this.props.community.creator_id == UserService.Instance.user.id;
296 get canMod(): boolean {
298 UserService.Instance.user &&
299 this.props.moderators
301 .includes(UserService.Instance.user.id)
305 get canAdmin(): boolean {
307 UserService.Instance.user &&
308 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
312 handleModRemoveShow(i: Sidebar) {
313 i.state.showRemoveDialog = true;
317 handleModRemoveReasonChange(i: Sidebar, event: any) {
318 i.state.removeReason = event.target.value;
322 handleModRemoveExpiresChange(i: Sidebar, event: any) {
323 console.log(event.target.value);
324 i.state.removeExpires = event.target.value;
328 handleModRemoveSubmit(i: Sidebar) {
329 event.preventDefault();
330 let deleteForm: CommunityFormI = {
331 name: i.props.community.name,
332 title: i.props.community.title,
333 category_id: i.props.community.category_id,
334 edit_id: i.props.community.id,
335 removed: !i.props.community.removed,
336 reason: i.state.removeReason,
337 expires: getUnixTime(i.state.removeExpires),
338 nsfw: i.props.community.nsfw,
341 WebSocketService.Instance.editCommunity(deleteForm);
343 i.state.showRemoveDialog = false;