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';
11 import { mdToHtml, getUnixTime } from '../utils';
12 import { CommunityForm } from './community-form';
13 import { i18n } from '../i18next';
14 import { T } from 'inferno-i18next';
16 interface SidebarProps {
18 moderators: Array<CommunityUser>;
19 admins: Array<UserView>;
22 interface SidebarState {
24 showRemoveDialog: boolean;
26 removeExpires: string;
29 export class Sidebar extends Component<SidebarProps, SidebarState> {
30 private emptyState: SidebarState = {
32 showRemoveDialog: false,
37 constructor(props: any, context: any) {
38 super(props, context);
39 this.state = this.emptyState;
40 this.handleEditCommunity = this.handleEditCommunity.bind(this);
41 this.handleEditCancel = this.handleEditCancel.bind(this);
47 {!this.state.showEdit ? (
51 community={this.props.community}
52 onEdit={this.handleEditCommunity}
53 onCancel={this.handleEditCancel}
61 let community = this.props.community;
64 <div class="card border-secondary mb-3">
65 <div class="card-body">
67 <span>{community.title}</span>
68 {community.removed && (
69 <small className="ml-2 text-muted font-italic">
70 <T i18nKey="removed">#</T>
73 {community.deleted && (
74 <small className="ml-2 text-muted font-italic">
75 <T i18nKey="deleted">#</T>
79 <Link className="text-muted" to={`/c/${community.name}`}>
82 <ul class="list-inline mb-1 text-muted small font-weight-bold">
85 <li className="list-inline-item">
88 onClick={linkEvent(this, this.handleEditClick)}
90 <T i18nKey="edit">#</T>
94 <li className="list-inline-item">
97 onClick={linkEvent(this, this.handleDeleteClick)}
108 <li className="list-inline-item">
109 {!this.props.community.removed ? (
112 onClick={linkEvent(this, this.handleModRemoveShow)}
114 <T i18nKey="remove">#</T>
119 onClick={linkEvent(this, this.handleModRemoveSubmit)}
121 <T i18nKey="restore">#</T>
127 {this.state.showRemoveDialog && (
128 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
129 <div class="form-group row">
130 <label class="col-form-label">
131 <T i18nKey="reason">#</T>
135 class="form-control mr-2"
136 placeholder={i18n.t('optional')}
137 value={this.state.removeReason}
138 onInput={linkEvent(this, this.handleModRemoveReasonChange)}
141 {/* TODO hold off on expires for now */}
142 {/* <div class="form-group row"> */}
143 {/* <label class="col-form-label">Expires</label> */}
144 {/* <input type="date" class="form-control mr-2" placeholder={i18n.t('expires')} value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
146 <div class="form-group row">
147 <button type="submit" class="btn btn-secondary">
148 <T i18nKey="remove_community">#</T>
153 <ul class="my-1 list-inline">
154 <li className="list-inline-item">
155 <Link className="badge badge-secondary" to="/communities">
156 {community.category_name}
159 <li className="list-inline-item badge badge-secondary">
161 i18nKey="number_of_subscribers"
162 interpolation={{ count: community.number_of_subscribers }}
167 <li className="list-inline-item badge badge-secondary">
169 i18nKey="number_of_posts"
170 interpolation={{ count: community.number_of_posts }}
175 <li className="list-inline-item badge badge-secondary">
177 i18nKey="number_of_comments"
178 interpolation={{ count: community.number_of_comments }}
183 <li className="list-inline-item">
185 className="badge badge-secondary"
186 to={`/modlog/community/${this.props.community.id}`}
188 <T i18nKey="modlog">#</T>
192 <ul class="list-inline small">
193 <li class="list-inline-item">{i18n.t('mods')}: </li>
194 {this.props.moderators.map(mod => (
195 <li class="list-inline-item">
196 <Link class="text-info" to={`/u/${mod.user_name}`}>
203 class={`btn btn-sm btn-secondary btn-block mb-3 ${(community.deleted ||
204 community.removed) &&
206 to={`/create_post?community=${community.name}`}
208 <T i18nKey="create_a_post">#</T>
211 {community.subscribed ? (
213 class="btn btn-sm btn-secondary btn-block"
214 onClick={linkEvent(community.id, this.handleUnsubscribe)}
216 <T i18nKey="unsubscribe">#</T>
220 class="btn btn-sm btn-secondary btn-block"
221 onClick={linkEvent(community.id, this.handleSubscribe)}
223 <T i18nKey="subscribe">#</T>
229 {community.description && (
230 <div class="card border-secondary">
231 <div class="card-body">
234 dangerouslySetInnerHTML={mdToHtml(community.description)}
243 handleEditClick(i: Sidebar) {
244 i.state.showEdit = true;
248 handleEditCommunity() {
249 this.state.showEdit = false;
250 this.setState(this.state);
254 this.state.showEdit = false;
255 this.setState(this.state);
258 handleDeleteClick(i: Sidebar) {
259 event.preventDefault();
260 let deleteForm: CommunityFormI = {
261 name: i.props.community.name,
262 title: i.props.community.title,
263 category_id: i.props.community.category_id,
264 edit_id: i.props.community.id,
265 deleted: !i.props.community.deleted,
266 nsfw: i.props.community.nsfw,
269 WebSocketService.Instance.editCommunity(deleteForm);
272 handleUnsubscribe(communityId: number) {
273 let form: FollowCommunityForm = {
274 community_id: communityId,
277 WebSocketService.Instance.followCommunity(form);
280 handleSubscribe(communityId: number) {
281 let form: FollowCommunityForm = {
282 community_id: communityId,
285 WebSocketService.Instance.followCommunity(form);
288 private get amCreator(): boolean {
289 return this.props.community.creator_id == UserService.Instance.user.id;
292 get canMod(): boolean {
294 UserService.Instance.user &&
295 this.props.moderators
297 .includes(UserService.Instance.user.id)
301 get canAdmin(): boolean {
303 UserService.Instance.user &&
304 this.props.admins.map(a => a.id).includes(UserService.Instance.user.id)
308 handleModRemoveShow(i: Sidebar) {
309 i.state.showRemoveDialog = true;
313 handleModRemoveReasonChange(i: Sidebar, event: any) {
314 i.state.removeReason = event.target.value;
318 handleModRemoveExpiresChange(i: Sidebar, event: any) {
319 console.log(event.target.value);
320 i.state.removeExpires = event.target.value;
324 handleModRemoveSubmit(i: Sidebar) {
325 event.preventDefault();
326 let deleteForm: CommunityFormI = {
327 name: i.props.community.name,
328 title: i.props.community.title,
329 category_id: i.props.community.category_id,
330 edit_id: i.props.community.id,
331 removed: !i.props.community.removed,
332 reason: i.state.removeReason,
333 expires: getUnixTime(i.state.removeExpires),
334 nsfw: i.props.community.nsfw,
337 WebSocketService.Instance.editCommunity(deleteForm);
339 i.state.showRemoveDialog = false;