summaryrefslogtreecommitdiffstats
path: root/ui/src/components/search.tsx
blob: ec657bb15fc20f95816e3e886563ad30b1716eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { Component, linkEvent } from 'inferno';
import { Subscription } from "rxjs";
import { retryWhen, delay, take } from 'rxjs/operators';
import { UserOperation, Post, Comment, SortType, SearchForm, SearchResponse, SearchType } from '../interfaces';
import { WebSocketService } from '../services';
import { msgOp, fetchLimit } from '../utils';
import { PostListing } from './post-listing';
import { CommentNodes } from './comment-nodes';

interface SearchState {
  q: string,
  type_: SearchType,
  sort: SortType,
  page: number,
  searchResponse: SearchResponse;
  loading: boolean;
}

export class Search extends Component<any, SearchState> {

  private subscription: Subscription;
  private emptyState: SearchState = {
    q: undefined,
    type_: SearchType.Both,
    sort: SortType.TopAll,
    page: 1,
    searchResponse: {
      op: null,
      posts: [],
      comments: [],
    },
    loading: false,
  }

  constructor(props: any, context: any) {
    super(props, context);

    this.state = this.emptyState;

    this.subscription = WebSocketService.Instance.subject
    .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
    .subscribe(
      (msg) => this.parseMessage(msg),
        (err) => console.error(err),
        () => console.log('complete')
    );

  }

  componentWillUnmount() {
    this.subscription.unsubscribe();
  }

  componentDidMount() {
    document.title = `Search - ${WebSocketService.Instance.site.name}`;
  }

  render() {
    return (
      <div class="container">
        <div class="row">
          <div class="col-12">
            <h5>Search</h5>
            {this.selects()}
            {this.searchForm()}
            {this.state.type_ == SearchType.Both &&
              this.both()
            }
            {this.state.type_ == SearchType.Comments &&
              this.comments()
            }
            {this.state.type_ == SearchType.Posts &&
              this.posts()
            }
            {this.noResults()}
            {this.paginator()}
          </div>
        </div>
      </div>
    )
  }

  searchForm() {
    return (
      <form class="form-inline" onSubmit={linkEvent(this, this.handleSearchSubmit)}>
        <input type="text" class="form-control mr-2" value={this.state.q} placeholder="Search..." onInput={linkEvent(this, this.handleQChange)} required minLength={3} />
        <button type="submit" class="btn btn-secondary mr-2">
          {this.state.loading ?
          <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
          <span>Search</span>
          }
        </button>
      </form>
    )
  }

  selects() {
    return (
      <div className="mb-2">
        <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="custom-select custom-select-sm w-auto">
          <option disabled>Type</option>
          <option value={SearchType.Both}>Both</option>
          <option value={SearchType.Comments}>Comments</option>
          <option value={SearchType.Posts}>Posts</option>
        </select>
        <select value={this.state.sort} onChange={linkEvent(this, this.handleSortChange)} class="custom-select custom-select-sm w-auto ml-2">
          <option disabled>Sort Type</option>
          <option value={SortType.New}>New</option>
          <option value={SortType.TopDay}>Top Day</option>
          <option value={SortType.TopWeek}>Week</option>
          <option value={SortType.TopMonth}>Month</option>
          <option value={SortType.TopYear}>Year</option>
          <option value={SortType.TopAll}>All</option>
        </select>
      </div>
    )

  }

  both() {
    let combined: Array<{type_: string, data: Comment | Post}> = [];
    let comments = this.state.searchResponse.comments.map(e => {return {type_: "comments", data: e}});
    let posts = this.state.searchResponse.posts.map(e => {return {type_: "posts", data: e}});

    combined.push(...comments);
    combined.push(...posts);

    // Sort it
    if (this.state.sort == SortType.New) {
      combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
    } else {
      combined.sort((a, b) => b.data.score - a.data.score);
    }

    return (
      <div>
        {combined.map(i =>
          <div>
            {i.type_ == "posts"
              ? <PostListing post={i.data as Post} showCommunity viewOnly />
              : <CommentNodes nodes={[{comment: i.data as Comment}]} viewOnly noIndent />
            }
          </div>
                     )
        }
      </div>
    )
  }

  comments() {
    return (
      <div>
        {this.state.searchResponse.comments.map(comment => 
          <CommentNodes nodes={[{comment: comment}]} noIndent viewOnly />
        )}
      </div>
    );
  }

  posts() {
    return (
      <div>
        {this.state.searchResponse.posts.map(post => 
          <PostListing post={post} showCommunity viewOnly />
        )}
      </div>
    );
  }

  paginator() {
    return (
      <div class="mt-2">
        {this.state.page > 1 && 
          <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
        }
        <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
      </div>
    );
  }

  noResults() {
    let res = this.state.searchResponse;
    return (
      <div>
        {res && res.op && res.posts.length == 0 && res.comments.length == 0 && 
          <span>No Results</span>
        }
      </div>
    )
  }

  nextPage(i: Search) { 
    i.state.page++;
    i.setState(i.state);
    i.search();
  }

  prevPage(i: Search) { 
    i.state.page--;
    i.setState(i.state);
    i.search();
  }

  search() {
    // TODO community
    let form: SearchForm = {
      q: this.state.q,
      type_: SearchType[this.state.type_],
      sort: SortType[this.state.sort],
      page: this.state.page,
      limit: fetchLimit,
    };

    WebSocketService.Instance.search(form);
  }

  handleSortChange(i: Search, event: any) {
    i.state.sort = Number(event.target.value);
    i.state.page = 1;
    i.setState(i.state);
    i.search();
  }

  handleTypeChange(i: Search, event: any) {
    i.state.type_ = Number(event.target.value);
    i.state.page = 1;
    i.setState(i.state);
    i.search();
  }

  handleSearchSubmit(i: Search, event: any) {
    event.preventDefault();
    i.state.loading = true;
    i.search();
    i.setState(i.state);
  }

  handleQChange(i: Search, event: any) {
    i.state.q = event.target.value;
    i.setState(i.state);
  }

  parseMessage(msg: any) {
    console.log(msg);
    let op: UserOperation = msgOp(msg);
    if (msg.error) {
      alert(msg.error);
      return;
    } else if (op == UserOperation.Search) {
      let res: SearchResponse = msg;
      this.state.searchResponse = res;
      this.state.loading = false;
      document.title = `Search - ${this.state.q} - ${WebSocketService.Instance.site.name}`;
      window.scrollTo(0,0);
      this.setState(this.state);
    }
  }
}