import React from 'react'; import { Link, RouteComponentProps } from 'react-router-dom'; import { inject, observer } from 'mobx-react'; import { orderBy } from 'lodash'; import { ThreadService } from '../../services'; import { Editor, ForumNav, LoginButton, ScrollToTop } from '../../components'; import { ThreadModel } from '../../model'; import { UserStore } from '../../stores/user-store'; import { Oauth } from '../../util'; import './forum.scss'; const stickyImage = require('../../assets/sticky.gif'); interface Props extends RouteComponentProps { userStore: UserStore; } interface State { showEditor: boolean; threads: ThreadModel[]; } @inject('userStore') @observer export class Forum extends React.Component { constructor(props: Props) { super(props); this.state = { showEditor: false, threads: [], }; } componentDidMount() { this.getThreads(this.props.match.params['id']); } // update the page if the route params change componentWillReceiveProps(nextProps: Props) { if (this.props.match.params !== nextProps.match.params) { this.getThreads(nextProps.match.params['id']); } } private async getThreads(categoryId: string) { let threads = await ThreadService.getCategoryThreads(categoryId); threads = this.orderBy(threads); this.setState({ threads }); } private orderBy(threads: ThreadModel[]) { return orderBy(threads, ['sticky', 'updated_at'], ['desc', 'desc']); } onNewTopic() { if (this.props.userStore.user) { this.setState({ showEditor: true }); } else { Oauth.openOuathWindow(); } } onNewTopicClose(cancel: boolean) { this.setState({ showEditor: false }); if (!cancel) { this.getThreads(this.props.match.params['id']); } } renderHeader() { return (
this.props.history.push(dest)}/>
); } renderBody() { return (
this.onNewTopic()}/>
{this.renderTable()}
); } renderCell(content: JSX.Element | string, style: any, center?: boolean, header?: boolean) { let classNames: string = ''; classNames += center ? ' forum-cell--center' : ''; classNames += header ? ' forum-cell--header' : ' forum-cell--body'; return
{content}
; } renderThreadRows() { const categoryId = this.props.match.params['id']; return this.state.threads.map((thread, index) => { const authorBluePost = thread.user.permissions === 'admin' ? 'blue' : ''; const lastReplyBluePost = thread.last_reply.permissions === 'admin' ? 'blue' : ''; const sticky = thread.sticky ? : ''; return (
{this.renderCell(sticky, { maxWidth: '50px' }, true)} {this.renderCell({thread.title}, { minWidth: '200px' })} {this.renderCell({thread.user.character_name || thread.user.battletag}, { maxWidth: '150px' })} {this.renderCell({thread.reply_count}, { maxWidth: '150px' }, true)} {this.renderCell({thread.view_count}, { maxWidth: '150px' }, true)} {this.renderCell(
by {thread.last_reply.character_name || thread.last_reply.battletag}
, { maxWidth: '200px' }, )}
); }); } renderTable() { return (
TODO:
{this.renderCell(, { maxWidth: '50px' }, true, true)} {this.renderCell(Subject, { minWidth: '200px' }, false, true)} {this.renderCell(Author, { maxWidth: '150px' }, true, true)} {this.renderCell(Replies, { maxWidth: '150px' }, true, true)} {this.renderCell(Views, { maxWidth: '150px' }, true, true)} {this.renderCell(Last Post, { maxWidth: '200px' }, true, true)}
{this.renderThreadRows()}
); } render() { return ( {this.state.showEditor && this.onNewTopicClose(cancel)}/>} {this.renderHeader()} {this.renderBody()} ); } }