import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { Community, CommunityUser, FollowCommunityForm, DeleteCommunityForm, RemoveCommunityForm, UserView, AddModToCommunityForm, } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { mdToHtml, getUnixTime } from '../utils'; import { CommunityForm } from './community-form'; import { UserListing } from './user-listing'; import { CommunityLink } from './community-link'; import { BannerIconHeader } from './banner-icon-header'; import { i18n } from '../i18next'; interface SidebarProps { community: Community; moderators: Array; admins: Array; online: number; enableNsfw: boolean; showIcon?: boolean; } interface SidebarState { showEdit: boolean; showRemoveDialog: boolean; removeReason: string; removeExpires: string; showConfirmLeaveModTeam: boolean; } export class Sidebar extends Component { private emptyState: SidebarState = { showEdit: false, showRemoveDialog: false, removeReason: null, removeExpires: null, showConfirmLeaveModTeam: false, }; 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() { return (
{this.communityTitle()} {this.adminButtons()}
{this.subscribes()}
{this.description()} {this.badges()} {this.mods()}
); } communityTitle() { let community = this.props.community; return (
{this.props.showIcon && ( )} {community.title} {community.removed && ( {i18n.t('removed')} )} {community.deleted && ( {i18n.t('deleted')} )} {community.nsfw && ( {i18n.t('nsfw')} )}
); } badges() { let community = this.props.community; return (
  • {i18n.t('number_online', { count: this.props.online })}
  • {i18n.t('number_of_subscribers', { count: community.number_of_subscribers, })}
  • {i18n.t('number_of_posts', { count: community.number_of_posts, })}
  • {i18n.t('number_of_comments', { count: community.number_of_comments, })}
  • {community.category_name}
  • {i18n.t('modlog')}
); } mods() { return (
  • {i18n.t('mods')}:
  • {this.props.moderators.map(mod => (
  • ))}
); } subscribes() { let community = this.props.community; return (
{i18n.t('create_a_post')} {community.subscribed ? ( {i18n.t('unsubscribe')} ) : ( {i18n.t('subscribe')} )}
); } description() { let community = this.props.community; return ( community.description && (
) ); } adminButtons() { let community = this.props.community; return ( <>
    {this.canMod && ( <>
  • {!this.amCreator && (!this.state.showConfirmLeaveModTeam ? (
  • {i18n.t('leave_mod_team')}
  • ) : ( <>
  • {i18n.t('are_you_sure')}
  • {i18n.t('yes')}
  • {i18n.t('no')}
  • ))} {this.amCreator && (
  • )} )} {this.canAdmin && (
  • {!this.props.community.removed ? ( {i18n.t('remove')} ) : ( {i18n.t('restore')} )}
  • )}
{this.state.showRemoveDialog && (
{/* TODO hold off on expires for now */} {/*
*/} {/* */} {/* */} {/*
*/}
)} ); } 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); } handleDeleteClick(i: Sidebar) { event.preventDefault(); let deleteForm: DeleteCommunityForm = { edit_id: i.props.community.id, deleted: !i.props.community.deleted, }; WebSocketService.Instance.deleteCommunity(deleteForm); } handleShowConfirmLeaveModTeamClick(i: Sidebar) { i.state.showConfirmLeaveModTeam = true; i.setState(i.state); } handleLeaveModTeamClick(i: Sidebar) { let form: AddModToCommunityForm = { user_id: UserService.Instance.user.id, community_id: i.props.community.id, added: false, }; WebSocketService.Instance.addModToCommunity(form); i.state.showConfirmLeaveModTeam = false; i.setState(i.state); } handleCancelLeaveModTeamClick(i: Sidebar) { i.state.showConfirmLeaveModTeam = false; i.setState(i.state); } handleUnsubscribe(communityId: number) { event.preventDefault(); let form: FollowCommunityForm = { community_id: communityId, follow: false, }; WebSocketService.Instance.followCommunity(form); } handleSubscribe(communityId: number) { event.preventDefault(); let form: FollowCommunityForm = { community_id: communityId, follow: true, }; WebSocketService.Instance.followCommunity(form); } private get amCreator(): boolean { return this.props.community.creator_id == UserService.Instance.user.id; } get canMod(): boolean { return ( UserService.Instance.user && this.props.moderators .map(m => m.user_id) .includes(UserService.Instance.user.id) ); } get canAdmin(): boolean { return ( UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id) ); } 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) { event.preventDefault(); let removeForm: RemoveCommunityForm = { edit_id: i.props.community.id, removed: !i.props.community.removed, reason: i.state.removeReason, expires: getUnixTime(i.state.removeExpires), }; WebSocketService.Instance.removeCommunity(removeForm); i.state.showRemoveDialog = false; i.setState(i.state); } }