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);
42 : <CommunityForm community={this.props.community} onEdit={this.handleEditCommunity} onCancel={this.handleEditCancel}/>
49 let community = this.props.community;
52 <h5 className="mb-0">{community.title}
54 <small className="ml-2 text-muted font-italic">removed</small>
57 <Link className="text-muted" to={`/community/${community.id}`}>/f/{community.name}</Link>
58 <ul class="list-inline mb-1 text-muted small font-weight-bold">
61 <li className="list-inline-item">
62 <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
65 <li className="list-inline-item">
66 {/* <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span> */}
72 <li className="list-inline-item">
73 {!this.props.community.removed ?
74 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}>remove</span> :
75 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}>restore</span>
81 {this.state.showRemoveDialog &&
82 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
83 <div class="form-group row">
84 <label class="col-form-label">Reason</label>
85 <input type="text" class="form-control mr-2" placeholder="Optional" value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
87 <div class="form-group row">
88 <label class="col-form-label">Expires</label>
89 <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} />
91 <div class="form-group row">
92 <button type="submit" class="btn btn-secondary">Remove Community</button>
96 <ul class="my-1 list-inline">
97 <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
98 <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
99 <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
100 <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
101 <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
103 <ul class="list-inline small">
104 <li class="list-inline-item">mods: </li>
105 {this.props.moderators.map(mod =>
106 <li class="list-inline-item"><Link class="text-info" to={`/user/${mod.user_id}`}>{mod.user_name}</Link></li>
110 {community.subscribed
111 ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
112 : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
115 {community.description &&
118 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
126 handleEditClick(i: Sidebar) {
127 i.state.showEdit = true;
131 handleEditCommunity() {
132 this.state.showEdit = false;
133 this.setState(this.state);
137 this.state.showEdit = false;
138 this.setState(this.state);
141 // TODO no deleting communities yet
142 // handleDeleteClick(i: Sidebar, event) {
145 handleUnsubscribe(communityId: number) {
146 let form: FollowCommunityForm = {
147 community_id: communityId,
150 WebSocketService.Instance.followCommunity(form);
153 handleSubscribe(communityId: number) {
154 let form: FollowCommunityForm = {
155 community_id: communityId,
158 WebSocketService.Instance.followCommunity(form);
161 private get amCreator(): boolean {
162 return this.props.community.creator_id == UserService.Instance.user.id;
165 get canMod(): boolean {
166 return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
169 get canAdmin(): boolean {
170 return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
173 handleDeleteClick() {
176 handleModRemoveShow(i: Sidebar) {
177 i.state.showRemoveDialog = true;
181 handleModRemoveReasonChange(i: Sidebar, event: any) {
182 i.state.removeReason = event.target.value;
186 handleModRemoveExpiresChange(i: Sidebar, event: any) {
187 console.log(event.target.value);
188 i.state.removeExpires = event.target.value;
192 handleModRemoveSubmit(i: Sidebar) {
193 event.preventDefault();
194 let deleteForm: CommunityFormI = {
195 name: i.props.community.name,
196 title: i.props.community.title,
197 category_id: i.props.community.category_id,
198 edit_id: i.props.community.id,
199 removed: !i.props.community.removed,
200 reason: i.state.removeReason,
201 expires: getUnixTime(i.state.removeExpires),
204 WebSocketService.Instance.editCommunity(deleteForm);
206 i.state.showRemoveDialog = false;