1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-10 09:02:50 +00:00

client - search functionality working

This commit is contained in:
2018-01-22 19:56:24 -06:00
parent a8e82cdcf5
commit 04be6543b6

View File

@@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { Link, RouteComponentProps } from 'react-router-dom'; import { Link, RouteComponentProps } from 'react-router-dom';
import { inject, observer } from 'mobx-react'; import { inject, observer } from 'mobx-react';
import { orderBy } from 'lodash'; import { cloneDeep, filter, orderBy } from 'lodash';
import { ThreadService } from '../../services'; import { ThreadService } from '../../services';
import { Editor, ForumNav, LoginButton, PaginationLinks, ScrollToTop } from '../../components'; import { Editor, ForumNav, LoginButton, PaginationLinks, ScrollToTop } from '../../components';
import { ThreadModel } from '../../model'; import { ThreadModel } from '../../model';
@@ -20,8 +20,10 @@ interface Props extends RouteComponentProps<any> {
interface State { interface State {
showEditor: boolean; showEditor: boolean;
threads: ThreadModel[]; threads: ThreadModel[];
initialThreads: ThreadModel[];
pageThreads: ThreadModel[]; pageThreads: ThreadModel[];
pageLinks: (number | string)[]; pageLinks: (number | string)[];
searchText: string;
} }
interface RouteParams { interface RouteParams {
@@ -49,8 +51,10 @@ export class Forum extends React.Component<Props, State> {
this.state = { this.state = {
showEditor: false, showEditor: false,
threads: [], threads: [],
initialThreads: [],
pageThreads: [], pageThreads: [],
pageLinks: [], pageLinks: [],
searchText: '',
}; };
} }
@@ -81,6 +85,7 @@ export class Forum extends React.Component<Props, State> {
// fetch threads from server // fetch threads from server
private async getThreads(categoryId: number) { private async getThreads(categoryId: number) {
const threads = await ThreadService.getCategoryThreads(categoryId); const threads = await ThreadService.getCategoryThreads(categoryId);
this.setState({ initialThreads: threads });
this.processThreads(threads); this.processThreads(threads);
} }
@@ -119,6 +124,15 @@ export class Forum extends React.Component<Props, State> {
this.props.history.push(url); this.props.history.push(url);
} }
private onSearch(event: any) {
event.preventDefault();
const threads = filter(cloneDeep(this.state.initialThreads), (t) => {
return t.title.toLowerCase().match(this.state.searchText.toLowerCase());
});
this.processThreads(threads);
}
private onNewTopic() { private onNewTopic() {
if (this.props.userStore.user) { if (this.props.userStore.user) {
this.setState({ showEditor: true }); this.setState({ showEditor: true });
@@ -148,7 +162,7 @@ export class Forum extends React.Component<Props, State> {
renderBody() { renderBody() {
return ( return (
<div className="forum-body"> <div className="forum-body">
<div className="flex"> <form className="flex" style={{ marginBottom: 0 }} onSubmit={e => this.onSearch(e)}>
<img src={require('../../assets/forum-menu-left.gif')}/> <img src={require('../../assets/forum-menu-left.gif')}/>
<img src={require('../../assets/forum-menu-newtopic.gif')} <img src={require('../../assets/forum-menu-newtopic.gif')}
className="clickable" className="clickable"
@@ -156,11 +170,13 @@ export class Forum extends React.Component<Props, State> {
<img src={require('../../assets/forum-menu-right.gif')}/> <img src={require('../../assets/forum-menu-right.gif')}/>
<img src={require('../../assets/forum-menu-search-left.gif')}/> <img src={require('../../assets/forum-menu-search-left.gif')}/>
<div className="forum-menu-search-bg"> <div className="forum-menu-search-bg">
<input name="SearchText"/> <input name="SearchText" onChange={event => this.setState({ searchText: event.target.value })}/>
</div> </div>
<img src={require('../../assets/forum-menu-search.gif')} className="clickable"/> <input type="image" name="submit"
src={require('../../assets/forum-menu-search.gif')}
className="clickable" style={{ outline: 'none' }}/>
<div className="forumliner-bg"/> <div className="forumliner-bg"/>
</div> </form>
{this.renderTable()} {this.renderTable()}
</div> </div>