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>
118 <Link class="btn btn-sm btn-secondary btn-block mb-3"
119 to={`/create_post/c/${community.name}`}>Create a Post</Link>
121 {community.subscribed
122 ? <button class="btn btn-sm btn-secondary btn-block mb-3" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
123 : <button class="btn btn-sm btn-secondary btn-block mb-3" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
126 {community.description &&
129 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
137 handleEditClick(i: Sidebar) {
138 i.state.showEdit = true;
142 handleEditCommunity() {
143 this.state.showEdit = false;
144 this.setState(this.state);
148 this.state.showEdit = false;
149 this.setState(this.state);
152 handleDeleteClick(i: Sidebar) {
153 event.preventDefault();
154 let deleteForm: CommunityFormI = {
155 name: i.props.community.name,
156 title: i.props.community.title,
157 category_id: i.props.community.category_id,
158 edit_id: i.props.community.id,
159 deleted: !i.props.community.deleted,
162 WebSocketService.Instance.editCommunity(deleteForm);
165 handleUnsubscribe(communityId: number) {
166 let form: FollowCommunityForm = {
167 community_id: communityId,
170 WebSocketService.Instance.followCommunity(form);
173 handleSubscribe(communityId: number) {
174 let form: FollowCommunityForm = {
175 community_id: communityId,
178 WebSocketService.Instance.followCommunity(form);
181 private get amCreator(): boolean {
182 return this.props.community.creator_id == UserService.Instance.user.id;
185 get canMod(): boolean {
186 return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
189 get canAdmin(): boolean {
190 return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
193 handleModRemoveShow(i: Sidebar) {
194 i.state.showRemoveDialog = true;
198 handleModRemoveReasonChange(i: Sidebar, event: any) {
199 i.state.removeReason = event.target.value;
203 handleModRemoveExpiresChange(i: Sidebar, event: any) {
204 console.log(event.target.value);
205 i.state.removeExpires = event.target.value;
209 handleModRemoveSubmit(i: Sidebar) {
210 event.preventDefault();
211 let deleteForm: CommunityFormI = {
212 name: i.props.community.name,
213 title: i.props.community.title,
214 category_id: i.props.community.category_id,
215 edit_id: i.props.community.id,
216 removed: !i.props.community.removed,
217 reason: i.state.removeReason,
218 expires: getUnixTime(i.state.removeExpires),
221 WebSocketService.Instance.editCommunity(deleteForm);
223 i.state.showRemoveDialog = false;