mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-10-01 01:36:12 -04:00
ui.components.community: fix duplicate requests
Deprecate componentWillReceiveProps for getDerivedStateFromProps
This commit is contained in:
parent
af2141417a
commit
5aa2a682ff
60
ui/src/components/community.tsx
vendored
60
ui/src/components/community.tsx
vendored
@ -143,16 +143,21 @@ export class Community extends Component<any, State> {
|
|||||||
this.subscription.unsubscribe();
|
this.subscription.unsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Necessary for back button for some reason
|
static getDerivedStateFromProps(props) {
|
||||||
componentWillReceiveProps(nextProps: any) {
|
return {
|
||||||
|
dataType: getDataTypeFromProps(props),
|
||||||
|
sort: getSortTypeFromProps(props),
|
||||||
|
page: getPageFromProps(props),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidUpdate(_, lastState) {
|
||||||
if (
|
if (
|
||||||
nextProps.history.action == 'POP' ||
|
lastState.dataType !== this.state.dataType ||
|
||||||
nextProps.history.action == 'PUSH'
|
lastState.sort !== this.state.sort ||
|
||||||
|
lastState.page !== this.state.page
|
||||||
) {
|
) {
|
||||||
this.state.dataType = getDataTypeFromProps(nextProps);
|
this.setState({ loading: true });
|
||||||
this.state.sort = getSortTypeFromProps(nextProps);
|
|
||||||
this.state.page = getPageFromProps(nextProps);
|
|
||||||
this.setState(this.state);
|
|
||||||
this.fetchData();
|
this.fetchData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -273,46 +278,37 @@ export class Community extends Component<any, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nextPage(i: Community) {
|
nextPage(i: Community) {
|
||||||
i.state.page++;
|
i.updateUrl({ page: i.state.page + 1 });
|
||||||
i.setState(i.state);
|
|
||||||
i.updateUrl();
|
|
||||||
i.fetchData();
|
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
prevPage(i: Community) {
|
prevPage(i: Community) {
|
||||||
i.state.page--;
|
i.updateUrl({ page: i.state.page - 1 });
|
||||||
i.setState(i.state);
|
|
||||||
i.updateUrl();
|
|
||||||
i.fetchData();
|
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSortChange(val: SortType) {
|
handleSortChange(val: SortType) {
|
||||||
this.state.sort = val;
|
this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
|
||||||
this.state.page = 1;
|
|
||||||
this.state.loading = true;
|
|
||||||
this.setState(this.state);
|
|
||||||
this.updateUrl();
|
|
||||||
this.fetchData();
|
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleDataTypeChange(val: DataType) {
|
handleDataTypeChange(val: DataType) {
|
||||||
this.state.dataType = val;
|
this.updateUrl({ data_type: DataType[val].toLowerCase(), page: 1 });
|
||||||
this.state.page = 1;
|
|
||||||
this.state.loading = true;
|
|
||||||
this.setState(this.state);
|
|
||||||
this.updateUrl();
|
|
||||||
this.fetchData();
|
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUrl() {
|
updateUrl(paramUpdates: {
|
||||||
let dataTypeStr = DataType[this.state.dataType].toLowerCase();
|
data_type?: string;
|
||||||
let sortStr = SortType[this.state.sort].toLowerCase();
|
sort?: string;
|
||||||
|
page?: number;
|
||||||
|
}) {
|
||||||
|
const dataTypeStr =
|
||||||
|
paramUpdates.data_type || DataType[this.state.dataType].toLowerCase();
|
||||||
|
const sortStr =
|
||||||
|
paramUpdates.sort || SortType[this.state.sort].toLowerCase();
|
||||||
|
const page = paramUpdates.page || this.state.page;
|
||||||
this.props.history.push(
|
this.props.history.push(
|
||||||
`/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
|
`/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user