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 <small className="ml-2 text-muted font-italic">deleted</small>
63 <Link className="text-muted" to={`/c/${community.name}`}>/c/{community.name}</Link>
64 <ul class="list-inline mb-1 text-muted small font-weight-bold">
67 <li className="list-inline-item">
68 <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
71 <li className="list-inline-item">
72 <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>
73 {!community.deleted ? 'delete' : 'restore'}
80 <li className="list-inline-item">
81 {!this.props.community.removed ?
82 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}>remove</span> :
83 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}>restore</span>
89 {this.state.showRemoveDialog &&
90 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
91 <div class="form-group row">
92 <label class="col-form-label">Reason</label>
93 <input type="text" class="form-control mr-2" placeholder="Optional" value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
95 {/* TODO hold off on expires for now */}
96 {/* <div class="form-group row"> */}
97 {/* <label class="col-form-label">Expires</label> */}
98 {/* <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
100 <div class="form-group row">
101 <button type="submit" class="btn btn-secondary">Remove Community</button>
105 <ul class="my-1 list-inline">
106 <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
107 <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
108 <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
109 <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
110 <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
112 <ul class="list-inline small">
113 <li class="list-inline-item">mods: </li>
114 {this.props.moderators.map(mod =>
115 <li class="list-inline-item"><Link class="text-info" to={`/u/${mod.user_name}`}>{mod.user_name}</Link></li>
119 {community.subscribed
120 ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
121 : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
124 {community.description &&
127 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
135 handleEditClick(i: Sidebar) {
136 i.state.showEdit = true;
140 handleEditCommunity() {
141 this.state.showEdit = false;
142 this.setState(this.state);
146 this.state.showEdit = false;
147 this.setState(this.state);
150 handleDeleteClick(i: Sidebar) {
151 event.preventDefault();
152 let deleteForm: CommunityFormI = {
153 name: i.props.community.name,
154 title: i.props.community.title,
155 category_id: i.props.community.category_id,
156 edit_id: i.props.community.id,
157 deleted: !i.props.community.deleted,
160 WebSocketService.Instance.editCommunity(deleteForm);
163 handleUnsubscribe(communityId: number) {
164 let form: FollowCommunityForm = {
165 community_id: communityId,
168 WebSocketService.Instance.followCommunity(form);
171 handleSubscribe(communityId: number) {
172 let form: FollowCommunityForm = {
173 community_id: communityId,
176 WebSocketService.Instance.followCommunity(form);
179 private get amCreator(): boolean {
180 return this.props.community.creator_id == UserService.Instance.user.id;
183 get canMod(): boolean {
184 return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
187 get canAdmin(): boolean {
188 return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
191 handleModRemoveShow(i: Sidebar) {
192 i.state.showRemoveDialog = true;
196 handleModRemoveReasonChange(i: Sidebar, event: any) {
197 i.state.removeReason = event.target.value;
201 handleModRemoveExpiresChange(i: Sidebar, event: any) {
202 console.log(event.target.value);
203 i.state.removeExpires = event.target.value;
207 handleModRemoveSubmit(i: Sidebar) {
208 event.preventDefault();
209 let deleteForm: CommunityFormI = {
210 name: i.props.community.name,
211 title: i.props.community.title,
212 category_id: i.props.community.category_id,
213 edit_id: i.props.community.id,
214 removed: !i.props.community.removed,
215 reason: i.state.removeReason,
216 expires: getUnixTime(i.state.removeExpires),
219 WebSocketService.Instance.editCommunity(deleteForm);
221 i.state.showRemoveDialog = false;