ui.components.search: fix duplicate requests
Deprecate componentWillReceiveProps for getDerivedStateFromProps
This commit is contained in:
parent
2640efb3f0
commit
af2141417a
1 changed files with 52 additions and 44 deletions
96
ui/src/components/search.tsx
vendored
96
ui/src/components/search.tsx
vendored
|
@ -44,15 +44,17 @@ interface SearchState {
|
||||||
searchResponse: SearchResponse;
|
searchResponse: SearchResponse;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
site: Site;
|
site: Site;
|
||||||
|
searchText: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Search extends Component<any, SearchState> {
|
export class Search extends Component<any, SearchState> {
|
||||||
private subscription: Subscription;
|
private subscription: Subscription;
|
||||||
private emptyState: SearchState = {
|
private emptyState: SearchState = {
|
||||||
q: this.getSearchQueryFromProps(this.props),
|
q: Search.getSearchQueryFromProps(this.props),
|
||||||
type_: this.getSearchTypeFromProps(this.props),
|
type_: Search.getSearchTypeFromProps(this.props),
|
||||||
sort: this.getSortTypeFromProps(this.props),
|
sort: Search.getSortTypeFromProps(this.props),
|
||||||
page: this.getPageFromProps(this.props),
|
page: Search.getPageFromProps(this.props),
|
||||||
|
searchText: Search.getSearchQueryFromProps(this.props),
|
||||||
searchResponse: {
|
searchResponse: {
|
||||||
type_: null,
|
type_: null,
|
||||||
posts: [],
|
posts: [],
|
||||||
|
@ -77,23 +79,23 @@ export class Search extends Component<any, SearchState> {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
getSearchQueryFromProps(props: any): string {
|
static getSearchQueryFromProps(props: any): string {
|
||||||
return props.match.params.q ? props.match.params.q : '';
|
return props.match.params.q ? props.match.params.q : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
getSearchTypeFromProps(props: any): SearchType {
|
static getSearchTypeFromProps(props: any): SearchType {
|
||||||
return props.match.params.type
|
return props.match.params.type
|
||||||
? routeSearchTypeToEnum(props.match.params.type)
|
? routeSearchTypeToEnum(props.match.params.type)
|
||||||
: SearchType.All;
|
: SearchType.All;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSortTypeFromProps(props: any): SortType {
|
static getSortTypeFromProps(props: any): SortType {
|
||||||
return props.match.params.sort
|
return props.match.params.sort
|
||||||
? routeSortTypeToEnum(props.match.params.sort)
|
? routeSortTypeToEnum(props.match.params.sort)
|
||||||
: SortType.TopAll;
|
: SortType.TopAll;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPageFromProps(props: any): number {
|
static getPageFromProps(props: any): number {
|
||||||
return props.match.params.page ? Number(props.match.params.page) : 1;
|
return props.match.params.page ? Number(props.match.params.page) : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,17 +124,23 @@ export class Search extends Component<any, SearchState> {
|
||||||
this.subscription.unsubscribe();
|
this.subscription.unsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Necessary for back button for some reason
|
static getDerivedStateFromProps(props) {
|
||||||
componentWillReceiveProps(nextProps: any) {
|
return {
|
||||||
|
q: Search.getSearchQueryFromProps(props),
|
||||||
|
type_: Search.getSearchTypeFromProps(props),
|
||||||
|
sort: Search.getSortTypeFromProps(props),
|
||||||
|
page: Search.getPageFromProps(props),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(_, lastState) {
|
||||||
if (
|
if (
|
||||||
nextProps.history.action == 'POP' ||
|
lastState.q !== this.state.q ||
|
||||||
nextProps.history.action == 'PUSH'
|
lastState.type_ !== this.state.type_ ||
|
||||||
|
lastState.sort !== this.state.sort ||
|
||||||
|
lastState.page !== this.state.page
|
||||||
) {
|
) {
|
||||||
this.state.q = this.getSearchQueryFromProps(nextProps);
|
this.setState({ loading: true, searchText: this.state.q });
|
||||||
this.state.type_ = this.getSearchTypeFromProps(nextProps);
|
|
||||||
this.state.sort = this.getSortTypeFromProps(nextProps);
|
|
||||||
this.state.page = this.getPageFromProps(nextProps);
|
|
||||||
this.setState(this.state);
|
|
||||||
this.search();
|
this.search();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,7 +171,7 @@ export class Search extends Component<any, SearchState> {
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
class="form-control mr-2"
|
class="form-control mr-2"
|
||||||
value={this.state.q}
|
value={this.state.searchText}
|
||||||
placeholder={`${i18n.t('search')}...`}
|
placeholder={`${i18n.t('search')}...`}
|
||||||
onInput={linkEvent(this, this.handleQChange)}
|
onInput={linkEvent(this, this.handleQChange)}
|
||||||
required
|
required
|
||||||
|
@ -413,17 +421,11 @@ export class Search extends Component<any, SearchState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
nextPage(i: Search) {
|
nextPage(i: Search) {
|
||||||
i.state.page++;
|
i.updateUrl({ page: i.state.page + 1 });
|
||||||
i.setState(i.state);
|
|
||||||
i.updateUrl();
|
|
||||||
i.search();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prevPage(i: Search) {
|
prevPage(i: Search) {
|
||||||
i.state.page--;
|
i.updateUrl({ page: i.state.page - 1 });
|
||||||
i.setState(i.state);
|
|
||||||
i.updateUrl();
|
|
||||||
i.search();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
search() {
|
search() {
|
||||||
|
@ -441,37 +443,43 @@ export class Search extends Component<any, SearchState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSortChange(val: SortType) {
|
handleSortChange(val: SortType) {
|
||||||
this.state.sort = val;
|
this.updateUrl({ sort: val, page: 1 });
|
||||||
this.state.page = 1;
|
|
||||||
this.setState(this.state);
|
|
||||||
this.updateUrl();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleTypeChange(i: Search, event: any) {
|
handleTypeChange(i: Search, event: any) {
|
||||||
i.state.type_ = Number(event.target.value);
|
i.updateUrl({ type_: Number(event.target.value), page: 1 });
|
||||||
i.state.page = 1;
|
|
||||||
i.setState(i.state);
|
|
||||||
i.updateUrl();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSearchSubmit(i: Search, event: any) {
|
handleSearchSubmit(i: Search, event: any) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
i.state.loading = true;
|
i.updateUrl({
|
||||||
i.search();
|
q: i.state.searchText,
|
||||||
i.setState(i.state);
|
type_: i.state.type_,
|
||||||
i.updateUrl();
|
sort: i.state.sort,
|
||||||
|
page: i.state.page,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleQChange(i: Search, event: any) {
|
handleQChange(i: Search, event: any) {
|
||||||
i.state.q = event.target.value;
|
i.setState({ searchText: event.target.value });
|
||||||
i.setState(i.state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUrl() {
|
updateUrl(paramUpdates: {
|
||||||
let typeStr = SearchType[this.state.type_].toLowerCase();
|
q?: string;
|
||||||
let sortStr = SortType[this.state.sort].toLowerCase();
|
type_?: number;
|
||||||
|
sort?: SortType;
|
||||||
|
page?: number;
|
||||||
|
}) {
|
||||||
|
const qStr = paramUpdates.q || Search.getSearchQueryFromProps(this.props);
|
||||||
|
const typeStr =
|
||||||
|
SearchType[paramUpdates.type_] ||
|
||||||
|
SearchType[Search.getSearchTypeFromProps(this.props)];
|
||||||
|
const sortStr =
|
||||||
|
SortType[paramUpdates.sort] ||
|
||||||
|
SortType[Search.getSortTypeFromProps(this.props)];
|
||||||
|
const page = paramUpdates.page || Search.getPageFromProps(this.props);
|
||||||
this.props.history.push(
|
this.props.history.push(
|
||||||
`/search/q/${this.state.q}/type/${typeStr}/sort/${sortStr}/page/${this.state.page}`
|
`/search/q/${qStr}/type/${typeStr.toLowerCase()}/sort/${sortStr.toLowerCase()}/page/${page}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue