import React from 'react'; import { Link, RouteComponentProps } from 'react-router-dom'; import { get } from 'lodash'; import { inject, observer } from 'mobx-react'; import { ThreadService } from '../../services'; import { Editor, ForumNav, LoginButton, ScrollToTop } from '../../components'; import { ThreadModel } from '../../model'; import { UserStore } from '../../stores/user-store'; import './forum.scss'; import { Oauth } from '../../util'; 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) { const threads = await ThreadService.getCategoryThreads(categoryId); this.setState({ threads }); } 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) => { return (
{this.renderCell('flag', { maxWidth: '50px' })} {this.renderCell({thread.title}, { minWidth: '200px' })} {this.renderCell({thread.user.battletag}, { maxWidth: '150px' })} {this.renderCell({thread.reply_count}, { maxWidth: '150px' }, true)} {this.renderCell({thread.view_count}, { maxWidth: '150px' }, true)} {this.renderCell(by {get(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()} ); } }