2019-04-03 23:01:20 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-04-04 20:53:32 +00:00
|
|
|
import { Link } from 'inferno-router';
|
|
|
|
import { Community, CommunityUser } from '../interfaces';
|
2019-04-04 22:29:14 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-03 23:01:20 +00:00
|
|
|
import { mdToHtml } from '../utils';
|
2019-04-04 22:29:14 +00:00
|
|
|
import { CommunityForm } from './community-form';
|
2019-04-03 23:01:20 +00:00
|
|
|
|
|
|
|
interface SidebarProps {
|
|
|
|
community: Community;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface SidebarState {
|
2019-04-04 22:29:14 +00:00
|
|
|
showEdit: boolean;
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Sidebar extends Component<SidebarProps, SidebarState> {
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
private emptyState: SidebarState = {
|
|
|
|
showEdit: false
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
2019-04-04 22:29:14 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
this.handleEditCommunity = this.handleEditCommunity.bind(this);
|
2019-04-05 02:08:21 +00:00
|
|
|
this.handleEditCancel = this.handleEditCancel.bind(this);
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-04-04 22:29:14 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{!this.state.showEdit
|
|
|
|
? this.sidebar()
|
2019-04-05 02:08:21 +00:00
|
|
|
: <CommunityForm community={this.props.community} onEdit={this.handleEditCommunity} onCancel={this.handleEditCancel}/>
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
sidebar() {
|
2019-04-03 23:01:20 +00:00
|
|
|
let community = this.props.community;
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h4>{community.title}</h4>
|
2019-04-04 22:29:14 +00:00
|
|
|
{this.amMod &&
|
|
|
|
<ul class="list-inline mb-1 text-muted small font-weight-bold">
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
|
|
|
|
</li>
|
|
|
|
{this.amCreator &&
|
|
|
|
<li className="list-inline-item">
|
|
|
|
{/* <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span> */}
|
|
|
|
</li>
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
}
|
2019-04-04 20:00:19 +00:00
|
|
|
<ul class="list-inline">
|
2019-04-04 20:53:32 +00:00
|
|
|
<li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
|
2019-04-04 20:00:19 +00:00
|
|
|
<li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
|
|
|
|
<li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
|
|
|
|
<li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
|
|
|
|
</ul>
|
2019-04-03 23:01:20 +00:00
|
|
|
<div><button type="button" class="btn btn-secondary mb-2">Subscribe</button></div>
|
2019-04-04 20:53:32 +00:00
|
|
|
{community.description &&
|
|
|
|
<div>
|
|
|
|
<hr />
|
|
|
|
<div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
|
|
|
|
</div>
|
|
|
|
}
|
2019-04-03 23:01:20 +00:00
|
|
|
<hr />
|
2019-04-05 00:01:10 +00:00
|
|
|
<h4>Moderators</h4>
|
2019-04-04 20:53:32 +00:00
|
|
|
{this.props.moderators.map(mod =>
|
|
|
|
<Link to={`/user/${mod.user_id}`}>{mod.user_name}</Link>
|
|
|
|
)}
|
2019-04-03 23:01:20 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-04-04 22:29:14 +00:00
|
|
|
|
|
|
|
handleEditClick(i: Sidebar, event) {
|
|
|
|
i.state.showEdit = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEditCommunity(community: Community) {
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:08:21 +00:00
|
|
|
handleEditCancel() {
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
// TODO no deleting communities yet
|
|
|
|
handleDeleteClick(i: Sidebar, event) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private get amCreator(): boolean {
|
|
|
|
return UserService.Instance.loggedIn && this.props.community.creator_id == UserService.Instance.user.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private get amMod(): boolean {
|
|
|
|
console.log(this.props.moderators);
|
|
|
|
console.log(this.props);
|
|
|
|
return UserService.Instance.loggedIn &&
|
|
|
|
this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
|
|
|
|
}
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|