|
@@ -16,15 +16,19 @@ interface IState {
|
|
categoryId: number;
|
|
categoryId: number;
|
|
threadId: number;
|
|
threadId: number;
|
|
postId: number;
|
|
postId: number;
|
|
|
|
+ searchTerm: string;
|
|
}
|
|
}
|
|
|
|
|
|
class Forum extends React.Component<IProps, IState> {
|
|
class Forum extends React.Component<IProps, IState> {
|
|
constructor(props: IProps) {
|
|
constructor(props: IProps) {
|
|
super(props);
|
|
super(props);
|
|
- this.state = { categoryId: 0, threadId: 0, postId: 0 };
|
|
|
|
|
|
+ this.state = { categoryId: 0, threadId: 0, postId: 0, searchTerm: "" };
|
|
this.selectCategory = this.selectCategory.bind(this);
|
|
this.selectCategory = this.selectCategory.bind(this);
|
|
this.selectThread = this.selectThread.bind(this);
|
|
this.selectThread = this.selectThread.bind(this);
|
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
|
|
|
+ this.handleChange = this.handleChange.bind(this);
|
|
|
|
+ this.filterPosts = this.filterPosts.bind(this);
|
|
|
|
+ this.filterThreads = this.filterThreads.bind(this);
|
|
}
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
componentDidMount() {
|
|
@@ -41,6 +45,10 @@ class Forum extends React.Component<IProps, IState> {
|
|
if (this.state.categoryId) return this.selectCategory(0);
|
|
if (this.state.categoryId) return this.selectCategory(0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ handleChange(e: { target: { value: string } }) {
|
|
|
|
+ this.setState({ searchTerm: e.target.value.toLowerCase() });
|
|
|
|
+ }
|
|
|
|
+
|
|
selectCategory(categoryId: number) {
|
|
selectCategory(categoryId: number) {
|
|
this.setState({ categoryId });
|
|
this.setState({ categoryId });
|
|
}
|
|
}
|
|
@@ -57,10 +65,28 @@ class Forum extends React.Component<IProps, IState> {
|
|
if (id > 1) return id;
|
|
if (id > 1) return id;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ filterPosts(posts: Post[], s: string) {
|
|
|
|
+ return s === ""
|
|
|
|
+ ? posts
|
|
|
|
+ : posts.filter(
|
|
|
|
+ (p) =>
|
|
|
|
+ p.text.toLowerCase().includes(s) ||
|
|
|
|
+ this.props.handles[p.authorId].includes(s)
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ filterThreads(list: any[], s: string) {
|
|
|
|
+ return s === ""
|
|
|
|
+ ? list
|
|
|
|
+ : list.filter((i) => i.title.toLowerCase().includes(s));
|
|
|
|
+ }
|
|
|
|
+
|
|
getLatest() {
|
|
getLatest() {
|
|
let threads: number[] = [];
|
|
let threads: number[] = [];
|
|
let categories: number[] = [];
|
|
let categories: number[] = [];
|
|
- let posts = this.props.posts.sort((a, b) => b.id - a.id).slice(0, 20);
|
|
|
|
|
|
+ let posts = this.filterPosts(this.props.posts, this.state.searchTerm)
|
|
|
|
+ .sort((a, b) => b.id - a.id)
|
|
|
|
+ .slice(0, 20);
|
|
posts.forEach((p) => {
|
|
posts.forEach((p) => {
|
|
const thread = this.props.threads.find((t) => t.id === p.threadId);
|
|
const thread = this.props.threads.find((t) => t.id === p.threadId);
|
|
if (!thread) return;
|
|
if (!thread) return;
|
|
@@ -72,7 +98,7 @@ class Forum extends React.Component<IProps, IState> {
|
|
|
|
|
|
render() {
|
|
render() {
|
|
const { block, now, handles, categories, posts, threads } = this.props;
|
|
const { block, now, handles, categories, posts, threads } = this.props;
|
|
- const { categoryId, threadId } = this.state;
|
|
|
|
|
|
+ const { categoryId, threadId, searchTerm } = this.state;
|
|
|
|
|
|
const startTime: number = now - block * 6000;
|
|
const startTime: number = now - block * 6000;
|
|
|
|
|
|
@@ -94,6 +120,8 @@ class Forum extends React.Component<IProps, IState> {
|
|
threads={threads}
|
|
threads={threads}
|
|
thread={thread}
|
|
thread={thread}
|
|
posts={posts}
|
|
posts={posts}
|
|
|
|
+ handleChange={this.handleChange}
|
|
|
|
+ searchTerm={searchTerm}
|
|
/>
|
|
/>
|
|
<Content
|
|
<Content
|
|
latest={this.getLatest()}
|
|
latest={this.getLatest()}
|
|
@@ -106,6 +134,9 @@ class Forum extends React.Component<IProps, IState> {
|
|
thread={thread}
|
|
thread={thread}
|
|
handles={handles}
|
|
handles={handles}
|
|
startTime={startTime}
|
|
startTime={startTime}
|
|
|
|
+ searchTerm={searchTerm}
|
|
|
|
+ filterPosts={this.filterPosts}
|
|
|
|
+ filterThreads={this.filterThreads}
|
|
/>
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
);
|