1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Community, CommunityUser, FollowCommunityForm, CommunityForm as CommunityFormI, UserView } from '../interfaces';
4 import { WebSocketService, UserService } from '../services';
5 import { mdToHtml, getUnixTime } from '../utils';
6 import { CommunityForm } from './community-form';
8 interface SidebarProps {
10 moderators: Array<CommunityUser>;
11 admins: Array<UserView>;
14 interface SidebarState {
16 showRemoveDialog: boolean;
18 removeExpires: string;
21 export class Sidebar extends Component<SidebarProps, SidebarState> {
23 private emptyState: SidebarState = {
25 showRemoveDialog: false,
30 constructor(props: any, context: any) {
31 super(props, context);
32 this.state = this.emptyState;
33 this.handleEditCommunity = this.handleEditCommunity.bind(this);
34 this.handleEditCancel = this.handleEditCancel.bind(this);
43 community={this.props.community}
44 onEdit={this.handleEditCommunity}
45 onCancel={this.handleEditCancel} />
52 let community = this.props.community;
55 <h5 className="mb-0">{community.title}
57 <small className="ml-2 text-muted font-italic">removed</small>
60 <Link className="text-muted" to={`/community/${community.id}`}>/f/{community.name}</Link>
61 <ul class="list-inline mb-1 text-muted small font-weight-bold">
64 <li className="list-inline-item">
65 <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
68 <li className="list-inline-item">
69 {/* <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span> */}
75 <li className="list-inline-item">
76 {!this.props.community.removed ?
77 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}>remove</span> :
78 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}>restore</span>
84 {this.state.showRemoveDialog &&
85 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
86 <div class="form-group row">
87 <label class="col-form-label">Reason</label>
88 <input type="text" class="form-control mr-2" placeholder="Optional" value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
90 <div class="form-group row">
91 <label class="col-form-label">Expires</label>
92 <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} />
94 <div class="form-group row">
95 <button type="submit" class="btn btn-secondary">Remove Community</button>
99 <ul class="my-1 list-inline">
100 <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
101 <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
102 <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
103 <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
104 <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
106 <ul class="list-inline small">
107 <li class="list-inline-item">mods: </li>
108 {this.props.moderators.map(mod =>
109 <li class="list-inline-item"><Link class="text-info" to={`/user/${mod.user_id}`}>{mod.user_name}</Link></li>
113 {community.subscribed
114 ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
115 : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
118 {community.description &&
121 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
129 handleEditClick(i: Sidebar) {
130 i.state.showEdit = true;
134 handleEditCommunity() {
135 this.state.showEdit = false;
136 this.setState(this.state);
140 this.state.showEdit = false;
141 this.setState(this.state);
144 // TODO no deleting communities yet
145 // handleDeleteClick(i: Sidebar, event) {
148 handleUnsubscribe(communityId: number) {
149 let form: FollowCommunityForm = {
150 community_id: communityId,
153 WebSocketService.Instance.followCommunity(form);
156 handleSubscribe(communityId: number) {
157 let form: FollowCommunityForm = {
158 community_id: communityId,
161 WebSocketService.Instance.followCommunity(form);
164 private get amCreator(): boolean {
165 return this.props.community.creator_id == UserService.Instance.user.id;
168 get canMod(): boolean {
169 return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
172 get canAdmin(): boolean {
173 return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
176 handleDeleteClick() {
179 handleModRemoveShow(i: Sidebar) {
180 i.state.showRemoveDialog = true;
184 handleModRemoveReasonChange(i: Sidebar, event: any) {
185 i.state.removeReason = event.target.value;
189 handleModRemoveExpiresChange(i: Sidebar, event: any) {
190 console.log(event.target.value);
191 i.state.removeExpires = event.target.value;
195 handleModRemoveSubmit(i: Sidebar) {
196 event.preventDefault();
197 let deleteForm: CommunityFormI = {
198 name: i.props.community.name,
199 title: i.props.community.title,
200 category_id: i.props.community.category_id,
201 edit_id: i.props.community.id,
202 removed: !i.props.community.removed,
203 reason: i.state.removeReason,
204 expires: getUnixTime(i.state.removeExpires),
207 WebSocketService.Instance.editCommunity(deleteForm);
209 i.state.showRemoveDialog = false;