import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { Community, CommunityUser, FollowCommunityForm, CommunityForm as CommunityFormI } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { mdToHtml, getUnixTime } from '../utils'; import { CommunityForm } from './community-form'; interface SidebarProps { community: Community; moderators: Array; } interface SidebarState { showEdit: boolean; showRemoveDialog: boolean; removeReason: string; removeExpires: string; } export class Sidebar extends Component { private emptyState: SidebarState = { showEdit: false, showRemoveDialog: false, removeReason: null, removeExpires: null } constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; this.handleEditCommunity = this.handleEditCommunity.bind(this); this.handleEditCancel = this.handleEditCancel.bind(this); } render() { return (
{!this.state.showEdit ? this.sidebar() : }
) } sidebar() { let community = this.props.community; return (

{community.title} {community.removed && removed }

/f/{community.name} {community.am_mod &&
  • edit
  • {this.amCreator &&
  • {/* delete */}
  • }
  • {!this.props.community.removed ? remove : restore }
} {this.state.showRemoveDialog &&
}
  • {community.category_name}
  • {community.number_of_subscribers} Subscribers
  • {community.number_of_posts} Posts
  • {community.number_of_comments} Comments
{community.subscribed ? : }
{community.description &&

}

Moderators

    {this.props.moderators.map(mod =>
  • {mod.user_name}
  • )}
); } handleEditClick(i: Sidebar) { i.state.showEdit = true; i.setState(i.state); } handleEditCommunity() { this.state.showEdit = false; this.setState(this.state); } handleEditCancel() { this.state.showEdit = false; this.setState(this.state); } // TODO no deleting communities yet // handleDeleteClick(i: Sidebar, event) { // } handleUnsubscribe(communityId: number) { let form: FollowCommunityForm = { community_id: communityId, follow: false }; WebSocketService.Instance.followCommunity(form); } handleSubscribe(communityId: number) { let form: FollowCommunityForm = { community_id: communityId, follow: true }; WebSocketService.Instance.followCommunity(form); } private get amCreator(): boolean { return UserService.Instance.loggedIn && this.props.community.creator_id == UserService.Instance.user.id; } // private get amMod(): boolean { // return UserService.Instance.loggedIn && // this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id); // } handleDeleteClick() { } handleModRemoveShow(i: Sidebar) { i.state.showRemoveDialog = true; i.setState(i.state); } handleModRemoveReasonChange(i: Sidebar, event: any) { i.state.removeReason = event.target.value; i.setState(i.state); } handleModRemoveExpiresChange(i: Sidebar, event: any) { console.log(event.target.value); i.state.removeExpires = event.target.value; i.setState(i.state); } handleModRemoveSubmit(i: Sidebar) { let deleteForm: CommunityFormI = { name: i.props.community.name, title: i.props.community.title, category_id: i.props.community.category_id, edit_id: i.props.community.id, removed: !i.props.community.removed, reason: i.state.removeReason, expires: getUnixTime(i.state.removeExpires), auth: null, }; WebSocketService.Instance.editCommunity(deleteForm); i.state.showRemoveDialog = false; i.setState(i.state); } }