2019-04-23 22:05:50 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2020-07-27 15:54:42 +00:00
|
|
|
import { Helmet } from 'inferno-helmet';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-04-23 22:05:50 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
Post,
|
|
|
|
Comment,
|
|
|
|
Community,
|
|
|
|
UserView,
|
|
|
|
SortType,
|
|
|
|
SearchForm,
|
|
|
|
SearchResponse,
|
|
|
|
SearchType,
|
2020-02-02 18:50:44 +00:00
|
|
|
PostResponse,
|
2020-01-20 23:39:45 +00:00
|
|
|
CommentResponse,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2020-07-08 05:02:14 +00:00
|
|
|
GetSiteResponse,
|
|
|
|
Site,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2019-04-23 22:05:50 +00:00
|
|
|
import { WebSocketService } from '../services';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
2020-01-19 04:54:10 +00:00
|
|
|
wsJsonToRes,
|
2019-10-19 00:20:27 +00:00
|
|
|
fetchLimit,
|
|
|
|
routeSearchTypeToEnum,
|
|
|
|
routeSortTypeToEnum,
|
2020-01-23 03:29:11 +00:00
|
|
|
toast,
|
2020-02-09 04:20:11 +00:00
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeFindRes,
|
|
|
|
commentsToFlatNodes,
|
2020-07-14 05:13:43 +00:00
|
|
|
getPageFromProps,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../utils';
|
2019-04-23 22:05:50 +00:00
|
|
|
import { PostListing } from './post-listing';
|
2020-04-10 20:55:57 +00:00
|
|
|
import { UserListing } from './user-listing';
|
2020-04-28 15:53:05 +00:00
|
|
|
import { CommunityLink } from './community-link';
|
2019-10-21 00:49:13 +00:00
|
|
|
import { SortSelect } from './sort-select';
|
2019-04-23 22:05:50 +00:00
|
|
|
import { CommentNodes } from './comment-nodes';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
2019-04-23 22:05:50 +00:00
|
|
|
|
|
|
|
interface SearchState {
|
2019-10-19 00:20:27 +00:00
|
|
|
q: string;
|
|
|
|
type_: SearchType;
|
|
|
|
sort: SortType;
|
|
|
|
page: number;
|
2019-04-23 22:05:50 +00:00
|
|
|
searchResponse: SearchResponse;
|
|
|
|
loading: boolean;
|
2020-07-08 05:02:14 +00:00
|
|
|
site: Site;
|
2020-07-13 02:58:48 +00:00
|
|
|
searchText: string;
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
interface SearchProps {
|
|
|
|
q: string;
|
|
|
|
type_: SearchType;
|
|
|
|
sort: SortType;
|
|
|
|
page: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UrlParams {
|
|
|
|
q?: string;
|
|
|
|
type_?: string;
|
|
|
|
sort?: string;
|
|
|
|
page?: number;
|
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
export class Search extends Component<any, SearchState> {
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: SearchState = {
|
2020-07-13 02:58:48 +00:00
|
|
|
q: Search.getSearchQueryFromProps(this.props),
|
|
|
|
type_: Search.getSearchTypeFromProps(this.props),
|
|
|
|
sort: Search.getSortTypeFromProps(this.props),
|
2020-07-14 05:13:43 +00:00
|
|
|
page: getPageFromProps(this.props),
|
2020-07-13 02:58:48 +00:00
|
|
|
searchText: Search.getSearchQueryFromProps(this.props),
|
2019-04-23 22:05:50 +00:00
|
|
|
searchResponse: {
|
2019-08-22 05:17:15 +00:00
|
|
|
type_: null,
|
2019-04-23 22:05:50 +00:00
|
|
|
posts: [],
|
|
|
|
comments: [],
|
2019-08-10 17:32:06 +00:00
|
|
|
communities: [],
|
|
|
|
users: [],
|
2019-04-23 22:05:50 +00:00
|
|
|
},
|
|
|
|
loading: false,
|
2020-07-08 05:02:14 +00:00
|
|
|
site: {
|
|
|
|
id: undefined,
|
|
|
|
name: undefined,
|
|
|
|
creator_id: undefined,
|
|
|
|
published: undefined,
|
|
|
|
creator_name: undefined,
|
|
|
|
number_of_users: undefined,
|
|
|
|
number_of_posts: undefined,
|
|
|
|
number_of_comments: undefined,
|
|
|
|
number_of_communities: undefined,
|
|
|
|
enable_downvotes: undefined,
|
|
|
|
open_registration: undefined,
|
|
|
|
enable_nsfw: undefined,
|
|
|
|
},
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-23 22:05:50 +00:00
|
|
|
|
2020-07-13 02:58:48 +00:00
|
|
|
static getSearchQueryFromProps(props: any): string {
|
2019-10-19 00:20:27 +00:00
|
|
|
return props.match.params.q ? props.match.params.q : '';
|
2019-09-06 01:34:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 02:58:48 +00:00
|
|
|
static getSearchTypeFromProps(props: any): SearchType {
|
2019-10-19 00:20:27 +00:00
|
|
|
return props.match.params.type
|
|
|
|
? routeSearchTypeToEnum(props.match.params.type)
|
|
|
|
: SearchType.All;
|
2019-09-06 01:34:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 02:58:48 +00:00
|
|
|
static getSortTypeFromProps(props: any): SortType {
|
2019-10-19 00:20:27 +00:00
|
|
|
return props.match.params.sort
|
|
|
|
? routeSortTypeToEnum(props.match.params.sort)
|
|
|
|
: SortType.TopAll;
|
2019-09-06 01:34:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-10-21 00:49:13 +00:00
|
|
|
this.handleSortChange = this.handleSortChange.bind(this);
|
2019-04-23 22:05:50 +00:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-14 04:04:47 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-10-19 00:20:27 +00:00
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-04-23 22:05:50 +00:00
|
|
|
() => console.log('complete')
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
|
|
|
|
2020-07-08 05:02:14 +00:00
|
|
|
WebSocketService.Instance.getSite();
|
|
|
|
|
2019-09-06 01:34:10 +00:00
|
|
|
if (this.state.q) {
|
|
|
|
this.search();
|
|
|
|
}
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
static getDerivedStateFromProps(props: any): SearchProps {
|
2020-07-13 02:58:48 +00:00
|
|
|
return {
|
|
|
|
q: Search.getSearchQueryFromProps(props),
|
|
|
|
type_: Search.getSearchTypeFromProps(props),
|
|
|
|
sort: Search.getSortTypeFromProps(props),
|
2020-07-14 05:13:43 +00:00
|
|
|
page: getPageFromProps(props),
|
2020-07-13 02:58:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
componentDidUpdate(_: any, lastState: SearchState) {
|
2019-10-19 00:20:27 +00:00
|
|
|
if (
|
2020-07-13 02:58:48 +00:00
|
|
|
lastState.q !== this.state.q ||
|
|
|
|
lastState.type_ !== this.state.type_ ||
|
|
|
|
lastState.sort !== this.state.sort ||
|
|
|
|
lastState.page !== this.state.page
|
2019-10-19 00:20:27 +00:00
|
|
|
) {
|
2020-07-13 02:58:48 +00:00
|
|
|
this.setState({ loading: true, searchText: this.state.q });
|
2019-09-06 01:34:10 +00:00
|
|
|
this.search();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:54:42 +00:00
|
|
|
get documentTitle(): string {
|
|
|
|
if (this.state.site.name) {
|
|
|
|
if (this.state.q) {
|
|
|
|
return `${i18n.t('search')} - ${this.state.q} - ${
|
|
|
|
this.state.site.name
|
|
|
|
}`;
|
|
|
|
} else {
|
|
|
|
return `${i18n.t('search')} - ${this.state.site.name}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 'Lemmy';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-07-27 15:54:42 +00:00
|
|
|
<Helmet title={this.documentTitle} />
|
2020-02-02 18:50:44 +00:00
|
|
|
<h5>{i18n.t('search')}</h5>
|
2020-01-20 23:39:45 +00:00
|
|
|
{this.selects()}
|
|
|
|
{this.searchForm()}
|
|
|
|
{this.state.type_ == SearchType.All && this.all()}
|
|
|
|
{this.state.type_ == SearchType.Comments && this.comments()}
|
|
|
|
{this.state.type_ == SearchType.Posts && this.posts()}
|
|
|
|
{this.state.type_ == SearchType.Communities && this.communities()}
|
|
|
|
{this.state.type_ == SearchType.Users && this.users()}
|
2020-07-10 00:03:33 +00:00
|
|
|
{this.resultsCount() == 0 && <span>{i18n.t('no_results')}</span>}
|
2020-01-20 23:39:45 +00:00
|
|
|
{this.paginator()}
|
2019-04-23 22:05:50 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
searchForm() {
|
|
|
|
return (
|
2019-10-19 00:20:27 +00:00
|
|
|
<form
|
|
|
|
class="form-inline"
|
|
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
2020-07-29 04:23:24 +00:00
|
|
|
class="form-control mr-2 mb-2"
|
2020-07-13 02:58:48 +00:00
|
|
|
value={this.state.searchText}
|
2019-10-19 00:20:27 +00:00
|
|
|
placeholder={`${i18n.t('search')}...`}
|
|
|
|
onInput={linkEvent(this, this.handleQChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
/>
|
2020-07-29 04:23:24 +00:00
|
|
|
<button type="submit" class="btn btn-secondary mr-2 mb-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : (
|
2020-02-02 18:50:44 +00:00
|
|
|
<span>{i18n.t('search')}</span>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-04-23 22:05:50 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selects() {
|
|
|
|
return (
|
|
|
|
<div className="mb-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
<select
|
|
|
|
value={this.state.type_}
|
|
|
|
onChange={linkEvent(this, this.handleTypeChange)}
|
2020-07-29 04:23:24 +00:00
|
|
|
class="custom-select w-auto mb-2"
|
2019-10-19 00:20:27 +00:00
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
<option disabled>{i18n.t('type')}</option>
|
|
|
|
<option value={SearchType.All}>{i18n.t('all')}</option>
|
|
|
|
<option value={SearchType.Comments}>{i18n.t('comments')}</option>
|
|
|
|
<option value={SearchType.Posts}>{i18n.t('posts')}</option>
|
2019-10-19 00:20:27 +00:00
|
|
|
<option value={SearchType.Communities}>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('communities')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</option>
|
2020-02-02 18:50:44 +00:00
|
|
|
<option value={SearchType.Users}>{i18n.t('users')}</option>
|
2019-04-23 22:05:50 +00:00
|
|
|
</select>
|
2019-10-21 00:49:13 +00:00
|
|
|
<span class="ml-2">
|
|
|
|
<SortSelect
|
|
|
|
sort={this.state.sort}
|
|
|
|
onChange={this.handleSortChange}
|
|
|
|
hideHot
|
|
|
|
/>
|
|
|
|
</span>
|
2019-04-23 22:05:50 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 17:32:06 +00:00
|
|
|
all() {
|
2019-10-19 00:20:27 +00:00
|
|
|
let combined: Array<{
|
|
|
|
type_: string;
|
|
|
|
data: Comment | Post | Community | UserView;
|
|
|
|
}> = [];
|
|
|
|
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 };
|
|
|
|
});
|
|
|
|
let communities = this.state.searchResponse.communities.map(e => {
|
|
|
|
return { type_: 'communities', data: e };
|
|
|
|
});
|
|
|
|
let users = this.state.searchResponse.users.map(e => {
|
|
|
|
return { type_: 'users', data: e };
|
|
|
|
});
|
2019-04-23 22:05:50 +00:00
|
|
|
|
|
|
|
combined.push(...comments);
|
|
|
|
combined.push(...posts);
|
2019-08-10 17:32:06 +00:00
|
|
|
combined.push(...communities);
|
|
|
|
combined.push(...users);
|
2019-04-23 22:05:50 +00:00
|
|
|
|
|
|
|
// Sort it
|
|
|
|
if (this.state.sort == SortType.New) {
|
|
|
|
combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
|
|
|
|
} else {
|
2019-10-19 00:20:27 +00:00
|
|
|
combined.sort(
|
|
|
|
(a, b) =>
|
|
|
|
((b.data as Comment | Post).score |
|
|
|
|
(b.data as Community).number_of_subscribers |
|
|
|
|
(b.data as UserView).comment_score) -
|
|
|
|
((a.data as Comment | Post).score |
|
|
|
|
(a.data as Community).number_of_subscribers |
|
|
|
|
(a.data as UserView).comment_score)
|
|
|
|
);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2019-10-19 00:20:27 +00:00
|
|
|
{combined.map(i => (
|
2020-01-20 23:39:45 +00:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-12">
|
|
|
|
{i.type_ == 'posts' && (
|
2020-07-08 05:02:14 +00:00
|
|
|
<PostListing
|
|
|
|
post={i.data as Post}
|
|
|
|
showCommunity
|
|
|
|
enableDownvotes={this.state.site.enable_downvotes}
|
|
|
|
enableNsfw={this.state.site.enable_nsfw}
|
|
|
|
/>
|
2020-01-20 23:39:45 +00:00
|
|
|
)}
|
|
|
|
{i.type_ == 'comments' && (
|
|
|
|
<CommentNodes
|
|
|
|
nodes={[{ comment: i.data as Comment }]}
|
|
|
|
locked
|
|
|
|
noIndent
|
2020-07-08 05:02:14 +00:00
|
|
|
enableDownvotes={this.state.site.enable_downvotes}
|
2020-01-20 23:39:45 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{i.type_ == 'communities' && (
|
2020-04-28 15:53:05 +00:00
|
|
|
<div>{this.communityListing(i.data as Community)}</div>
|
2020-01-20 23:39:45 +00:00
|
|
|
)}
|
|
|
|
{i.type_ == 'users' && (
|
|
|
|
<div>
|
|
|
|
<span>
|
2020-07-11 23:25:17 +00:00
|
|
|
@
|
2020-04-10 20:55:57 +00:00
|
|
|
<UserListing
|
|
|
|
user={{
|
|
|
|
name: (i.data as UserView).name,
|
|
|
|
avatar: (i.data as UserView).avatar,
|
|
|
|
}}
|
|
|
|
/>
|
2020-01-20 23:39:45 +00:00
|
|
|
</span>
|
2020-07-11 23:25:17 +00:00
|
|
|
<span>{` - ${i18n.t('number_of_comments', {
|
|
|
|
count: (i.data as UserView).number_of_comments,
|
|
|
|
})}`}</span>
|
2020-01-20 23:39:45 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2019-04-23 22:05:50 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
))}
|
2019-04-23 22:05:50 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
comments() {
|
|
|
|
return (
|
2020-02-09 04:20:11 +00:00
|
|
|
<CommentNodes
|
|
|
|
nodes={commentsToFlatNodes(this.state.searchResponse.comments)}
|
|
|
|
locked
|
|
|
|
noIndent
|
2020-07-08 05:02:14 +00:00
|
|
|
enableDownvotes={this.state.site.enable_downvotes}
|
2020-02-09 04:20:11 +00:00
|
|
|
/>
|
2019-04-23 22:05:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
posts() {
|
|
|
|
return (
|
2020-01-20 23:39:45 +00:00
|
|
|
<>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.searchResponse.posts.map(post => (
|
2020-01-20 23:39:45 +00:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-12">
|
2020-07-08 05:02:14 +00:00
|
|
|
<PostListing
|
|
|
|
post={post}
|
|
|
|
showCommunity
|
|
|
|
enableDownvotes={this.state.site.enable_downvotes}
|
|
|
|
enableNsfw={this.state.site.enable_nsfw}
|
|
|
|
/>
|
2020-01-20 23:39:45 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
))}
|
2020-01-20 23:39:45 +00:00
|
|
|
</>
|
2019-04-23 22:05:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-10 17:32:06 +00:00
|
|
|
// Todo possibly create UserListing and CommunityListing
|
|
|
|
communities() {
|
|
|
|
return (
|
2020-01-20 23:39:45 +00:00
|
|
|
<>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.searchResponse.communities.map(community => (
|
2020-01-20 23:39:45 +00:00
|
|
|
<div class="row">
|
2020-04-28 15:53:05 +00:00
|
|
|
<div class="col-12">{this.communityListing(community)}</div>
|
2019-08-10 17:32:06 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
))}
|
2020-01-20 23:39:45 +00:00
|
|
|
</>
|
2019-08-10 17:32:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:53:05 +00:00
|
|
|
communityListing(community: Community) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<span>
|
|
|
|
<CommunityLink community={community} />
|
|
|
|
</span>
|
|
|
|
<span>{` - ${community.title} -
|
|
|
|
${i18n.t('number_of_subscribers', {
|
|
|
|
count: community.number_of_subscribers,
|
|
|
|
})}
|
|
|
|
`}</span>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-10 17:32:06 +00:00
|
|
|
users() {
|
|
|
|
return (
|
2020-01-20 23:39:45 +00:00
|
|
|
<>
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.searchResponse.users.map(user => (
|
2020-01-20 23:39:45 +00:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-12">
|
|
|
|
<span>
|
2020-07-11 23:25:17 +00:00
|
|
|
@
|
2020-07-11 20:57:13 +00:00
|
|
|
<UserListing
|
|
|
|
user={{
|
|
|
|
name: user.name,
|
|
|
|
avatar: user.avatar,
|
|
|
|
}}
|
|
|
|
/>
|
2020-01-20 23:39:45 +00:00
|
|
|
</span>
|
2020-07-11 23:25:17 +00:00
|
|
|
<span>{` - ${i18n.t('number_of_comments', {
|
|
|
|
count: user.number_of_comments,
|
|
|
|
})}`}</span>
|
2020-01-20 23:39:45 +00:00
|
|
|
</div>
|
2019-08-10 17:32:06 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
))}
|
2020-01-20 23:39:45 +00:00
|
|
|
</>
|
2019-08-10 17:32:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
paginator() {
|
|
|
|
return (
|
|
|
|
<div class="mt-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.page > 1 && (
|
|
|
|
<button
|
2020-07-23 06:10:22 +00:00
|
|
|
class="btn btn-secondary mr-1"
|
2019-10-19 00:20:27 +00:00
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('prev')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2020-07-10 00:03:33 +00:00
|
|
|
|
|
|
|
{this.resultsCount() > 0 && (
|
|
|
|
<button
|
2020-07-23 06:10:22 +00:00
|
|
|
class="btn btn-secondary"
|
2020-07-10 00:03:33 +00:00
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
|
|
|
{i18n.t('next')}
|
|
|
|
</button>
|
|
|
|
)}
|
2019-04-23 22:05:50 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:03:33 +00:00
|
|
|
resultsCount(): number {
|
2019-04-23 22:05:50 +00:00
|
|
|
let res = this.state.searchResponse;
|
|
|
|
return (
|
2020-07-10 00:03:33 +00:00
|
|
|
res.posts.length +
|
|
|
|
res.comments.length +
|
|
|
|
res.communities.length +
|
|
|
|
res.users.length
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
nextPage(i: Search) {
|
2020-07-13 02:58:48 +00:00
|
|
|
i.updateUrl({ page: i.state.page + 1 });
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
prevPage(i: Search) {
|
2020-07-13 02:58:48 +00:00
|
|
|
i.updateUrl({ page: i.state.page - 1 });
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
search() {
|
|
|
|
let form: SearchForm = {
|
|
|
|
q: this.state.q,
|
|
|
|
type_: SearchType[this.state.type_],
|
|
|
|
sort: SortType[this.state.sort],
|
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
|
|
|
};
|
|
|
|
|
2019-09-06 01:34:10 +00:00
|
|
|
if (this.state.q != '') {
|
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
}
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 00:49:13 +00:00
|
|
|
handleSortChange(val: SortType) {
|
2020-07-14 05:13:43 +00:00
|
|
|
this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTypeChange(i: Search, event: any) {
|
2020-07-14 05:13:43 +00:00
|
|
|
i.updateUrl({
|
|
|
|
type_: SearchType[Number(event.target.value)].toLowerCase(),
|
|
|
|
page: 1,
|
|
|
|
});
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchSubmit(i: Search, event: any) {
|
|
|
|
event.preventDefault();
|
2020-07-13 02:58:48 +00:00
|
|
|
i.updateUrl({
|
|
|
|
q: i.state.searchText,
|
2020-07-14 05:13:43 +00:00
|
|
|
type_: SearchType[i.state.type_].toLowerCase(),
|
|
|
|
sort: SortType[i.state.sort].toLowerCase(),
|
2020-07-13 02:58:48 +00:00
|
|
|
page: i.state.page,
|
|
|
|
});
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleQChange(i: Search, event: any) {
|
2020-07-13 02:58:48 +00:00
|
|
|
i.setState({ searchText: event.target.value });
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
updateUrl(paramUpdates: UrlParams) {
|
|
|
|
const qStr = paramUpdates.q || this.state.q;
|
2020-07-13 02:58:48 +00:00
|
|
|
const typeStr =
|
2020-07-14 05:13:43 +00:00
|
|
|
paramUpdates.type_ || SearchType[this.state.type_].toLowerCase();
|
2020-07-13 02:58:48 +00:00
|
|
|
const sortStr =
|
2020-07-14 05:13:43 +00:00
|
|
|
paramUpdates.sort || SortType[this.state.sort].toLowerCase();
|
|
|
|
const page = paramUpdates.page || this.state.page;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.props.history.push(
|
2020-07-14 05:13:43 +00:00
|
|
|
`/search/q/${qStr}/type/${typeStr}/sort/${sortStr}/page/${page}`
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-09-06 01:34:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2019-04-23 22:05:50 +00:00
|
|
|
console.log(msg);
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 14:58:53 +00:00
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-04-23 22:05:50 +00:00
|
|
|
return;
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.Search) {
|
|
|
|
let data = res.data as SearchResponse;
|
|
|
|
this.state.searchResponse = data;
|
2019-04-23 22:05:50 +00:00
|
|
|
this.state.loading = false;
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-23 22:05:50 +00:00
|
|
|
this.setState(this.state);
|
2020-01-24 00:17:42 +00:00
|
|
|
} else if (res.op == UserOperation.CreateCommentLike) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
createCommentLikeRes(data, this.state.searchResponse.comments);
|
2020-01-20 23:39:45 +00:00
|
|
|
this.setState(this.state);
|
2020-01-24 00:17:42 +00:00
|
|
|
} else if (res.op == UserOperation.CreatePostLike) {
|
2020-02-02 18:50:44 +00:00
|
|
|
let data = res.data as PostResponse;
|
2020-02-09 04:20:11 +00:00
|
|
|
createPostLikeFindRes(data, this.state.searchResponse.posts);
|
2020-01-20 23:39:45 +00:00
|
|
|
this.setState(this.state);
|
2020-07-08 05:02:14 +00:00
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
|
|
|
this.state.site = data.site;
|
|
|
|
this.setState(this.state);
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|