1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Community, CommunityUser, FollowCommunityForm, CommunityForm as CommunityFormI } 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>;
13 interface SidebarState {
15 showRemoveDialog: boolean;
17 removeExpires: string;
20 export class Sidebar extends Component<SidebarProps, SidebarState> {
22 private emptyState: SidebarState = {
24 showRemoveDialog: false,
29 constructor(props: any, context: any) {
30 super(props, context);
31 this.state = this.emptyState;
32 this.handleEditCommunity = this.handleEditCommunity.bind(this);
33 this.handleEditCancel = this.handleEditCancel.bind(this);
41 : <CommunityForm community={this.props.community} onEdit={this.handleEditCommunity} onCancel={this.handleEditCancel}/>
48 let community = this.props.community;
51 <h5 className="mb-0">{community.title}
53 <small className="ml-2 text-muted font-italic">removed</small>
56 <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">
59 <li className="list-inline-item">
60 <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
63 <li className="list-inline-item">
64 {/* <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span> */}
67 <li className="list-inline-item">
68 {!this.props.community.removed ?
69 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}>remove</span> :
70 <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}>restore</span>
75 {this.state.showRemoveDialog &&
76 <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
77 <div class="form-group row">
78 <label class="col-form-label">Reason</label>
79 <input type="text" class="form-control mr-2" placeholder="Optional" value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
81 <div class="form-group row">
82 <label class="col-form-label">Expires</label>
83 <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} />
85 <div class="form-group row">
86 <button type="submit" class="btn btn-secondary">Remove Community</button>
90 <ul class="my-1 list-inline">
91 <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
92 <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
93 <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
94 <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
95 <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
97 <ul class="list-inline small">
98 <li class="list-inline-item">mods: </li>
99 {this.props.moderators.map(mod =>
100 <li class="list-inline-item"><Link class="text-info" to={`/user/${mod.user_id}`}>{mod.user_name}</Link></li>
104 {community.subscribed
105 ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
106 : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
109 {community.description &&
112 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
120 handleEditClick(i: Sidebar) {
121 i.state.showEdit = true;
125 handleEditCommunity() {
126 this.state.showEdit = false;
127 this.setState(this.state);
131 this.state.showEdit = false;
132 this.setState(this.state);
135 // TODO no deleting communities yet
136 // handleDeleteClick(i: Sidebar, event) {
139 handleUnsubscribe(communityId: number) {
140 let form: FollowCommunityForm = {
141 community_id: communityId,
144 WebSocketService.Instance.followCommunity(form);
147 handleSubscribe(communityId: number) {
148 let form: FollowCommunityForm = {
149 community_id: communityId,
152 WebSocketService.Instance.followCommunity(form);
155 private get amCreator(): boolean {
156 return this.props.community.creator_id == UserService.Instance.user.id;
159 // private get amMod(): boolean {
160 // return UserService.Instance.loggedIn &&
161 // this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
164 handleDeleteClick() {
167 handleModRemoveShow(i: Sidebar) {
168 i.state.showRemoveDialog = true;
172 handleModRemoveReasonChange(i: Sidebar, event: any) {
173 i.state.removeReason = event.target.value;
177 handleModRemoveExpiresChange(i: Sidebar, event: any) {
178 console.log(event.target.value);
179 i.state.removeExpires = event.target.value;
183 handleModRemoveSubmit(i: Sidebar) {
184 event.preventDefault();
185 let deleteForm: CommunityFormI = {
186 name: i.props.community.name,
187 title: i.props.community.title,
188 category_id: i.props.community.category_id,
189 edit_id: i.props.community.id,
190 removed: !i.props.community.removed,
191 reason: i.state.removeReason,
192 expires: getUnixTime(i.state.removeExpires),
195 WebSocketService.Instance.editCommunity(deleteForm);
197 i.state.showRemoveDialog = false;