import React from 'react'; import { RouteComponentProps } from 'react-router-dom'; import { get, find, map } from 'lodash'; import marked from 'marked'; import { DateTime } from 'luxon'; import { CharacterService, ThreadService } from '../../services'; import { Editor, Portrait, ScrollToTop } from '../../components'; import { ReplyModel, ThreadModel } from '../../model'; import './thread.scss'; interface Props extends RouteComponentProps {} interface State { quotedReply?: ReplyModel; showEditor: boolean; thread?: ThreadModel; } export class Thread extends React.Component { constructor(props: Props) { super(props); this.state = { showEditor: false, }; } componentDidMount() { this.getReplies(); } private async getReplies() { const thread = await ThreadService.getThread(this.props.match.params['threadId']); thread.replies = map(thread.replies, (reply) => { // add the thread topic to the front of the list return reply; }); this.setState({ thread }); } private onReplyClick() { this.setState({ showEditor: true }); } private onQuoteClick(reply: ReplyModel) { this.setState({ showEditor: true, quotedReply: reply, }); } private onEditorClose(cancel: boolean) { this.setState({ showEditor: false, quotedReply: undefined, }); if (!cancel) { this.getReplies(); } } private navigateForumIndex() { this.props.history.push(`/f/${this.state.thread!.category_id}`); } private getTimeFormat(dateTime: string) { return DateTime.fromISO(dateTime).toLocaleString({ year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: '2-digit', timeZoneName: 'short', }); } private renderQuotedReply(reply: ReplyModel) { const quotedReply = find(this.state.thread!.replies, { id: reply.quote_id }); return (quotedReply &&
Q u o t e:
); } renderUserInfo(reply: ReplyModel) { const { battletag, character_avatar, character_class, character_guild, character_name, character_realm } = reply.user; return (
{character_name || battletag}
{character_class &&
{character_class}
} {character_guild &&
Guild: {character_guild}
} {character_realm &&
Realm: {character_realm}
}
); } renderReplies(): any { return this.state.thread!.replies.map((reply, index) => { const replyDark = index % 2 === 0 ? 'reply--dark' : ''; return (
{this.renderUserInfo(reply)}
{`${index + 1}. `}{index > 0 && 'Re: '}{this.state.thread!.title} | {this.getTimeFormat(reply.inserted_at)}
this.onQuoteClick(reply)}/> this.onReplyClick()}/>
{this.renderQuotedReply(reply)}
); }); } render() { if (!this.state.thread) { return
; } const replies = get(this.state, 'thread.replies'); return ( {this.state.showEditor && this.onEditorClose(cancel)} quotedReply={this.state.quotedReply} /> }
Topic: | {this.getTimeFormat(this.state.thread!.inserted_at)}
this.navigateForumIndex()} style={{ cursor: 'pointer' }} />
{replies && this.renderReplies()}
this.navigateForumIndex()} style={{ cursor: 'pointer', float: 'right' }} />
); } }