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 {/* TODO hold off on expires for now */}
91 {/* <div class="form-group row"> */}
92 {/* <label class="col-form-label">Expires</label> */}
93 {/* <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
95 <div class="form-group row">
96 <button type="submit" class="btn btn-secondary">Remove Community</button>
100 <ul class="my-1 list-inline">
101 <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
102 <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
103 <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
104 <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
105 <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
107 <ul class="list-inline small">
108 <li class="list-inline-item">mods: </li>
109 {this.props.moderators.map(mod =>
110 <li class="list-inline-item"><Link class="text-info" to={`/user/${mod.user_id}`}>{mod.user_name}</Link></li>
114 {community.subscribed
115 ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
116 : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
119 {community.description &&
122 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
130 handleEditClick(i: Sidebar) {
131 i.state.showEdit = true;
135 handleEditCommunity() {
136 this.state.showEdit = false;
137 this.setState(this.state);
141 this.state.showEdit = false;
142 this.setState(this.state);
145 // TODO no deleting communities yet
146 // handleDeleteClick(i: Sidebar, event) {
149 handleUnsubscribe(communityId: number) {
150 let form: FollowCommunityForm = {
151 community_id: communityId,
154 WebSocketService.Instance.followCommunity(form);
157 handleSubscribe(communityId: number) {
158 let form: FollowCommunityForm = {
159 community_id: communityId,
162 WebSocketService.Instance.followCommunity(form);
165 private get amCreator(): boolean {
166 return this.props.community.creator_id == UserService.Instance.user.id;
169 get canMod(): boolean {
170 return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
173 get canAdmin(): boolean {
174 return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
177 handleDeleteClick() {
180 handleModRemoveShow(i: Sidebar) {
181 i.state.showRemoveDialog = true;
185 handleModRemoveReasonChange(i: Sidebar, event: any) {
186 i.state.removeReason = event.target.value;
190 handleModRemoveExpiresChange(i: Sidebar, event: any) {
191 console.log(event.target.value);
192 i.state.removeExpires = event.target.value;
196 handleModRemoveSubmit(i: Sidebar) {
197 event.preventDefault();
198 let deleteForm: CommunityFormI = {
199 name: i.props.community.name,
200 title: i.props.community.title,
201 category_id: i.props.community.category_id,
202 edit_id: i.props.community.id,
203 removed: !i.props.community.removed,
204 reason: i.state.removeReason,
205 expires: getUnixTime(i.state.removeExpires),
208 WebSocketService.Instance.editCommunity(deleteForm);
210 i.state.showRemoveDialog = false;