loading: boolean;
}
+interface CommunitiesProps {
+ page: number;
+}
+
export class Communities extends Component<any, CommunitiesState> {
private subscription: Subscription;
private emptyState: CommunitiesState = {
this.subscription.unsubscribe();
}
- static getDerivedStateFromProps(props) {
+ static getDerivedStateFromProps(props: any): CommunitiesProps {
return {
page: getPageFromProps(props),
};
}
- componentDidUpdate(_, lastState) {
+ componentDidUpdate(_: any, lastState: CommunitiesState) {
if (lastState.page !== this.state.page) {
this.setState({ loading: true });
this.refetch();
);
}
- updateUrl(paramUpdates: { page: number }) {
+ updateUrl(paramUpdates: CommunitiesProps) {
const page = paramUpdates.page || this.state.page;
this.props.history.push(`/communities/page/${page}`);
}
site: Site;
}
+interface CommunityProps {
+ dataType: DataType;
+ sort: SortType;
+ page: number;
+}
+
+interface UrlParams {
+ dataType?: string;
+ sort?: string;
+ page?: number;
+}
+
export class Community extends Component<any, State> {
private subscription: Subscription;
private emptyState: State = {
this.subscription.unsubscribe();
}
- static getDerivedStateFromProps(props) {
+ static getDerivedStateFromProps(props: any): CommunityProps {
return {
dataType: getDataTypeFromProps(props),
sort: getSortTypeFromProps(props),
};
}
- componentDidUpdate(_, lastState) {
+ componentDidUpdate(_: any, lastState: State) {
if (
lastState.dataType !== this.state.dataType ||
lastState.sort !== this.state.sort ||
}
handleDataTypeChange(val: DataType) {
- this.updateUrl({ data_type: DataType[val].toLowerCase(), page: 1 });
+ this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
window.scrollTo(0, 0);
}
- updateUrl(paramUpdates: {
- data_type?: string;
- sort?: string;
- page?: number;
- }) {
+ updateUrl(paramUpdates: UrlParams) {
const dataTypeStr =
- paramUpdates.data_type || DataType[this.state.dataType].toLowerCase();
+ paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
const sortStr =
paramUpdates.sort || SortType[this.state.sort].toLowerCase();
const page = paramUpdates.page || this.state.page;
page: number;
}
+interface MainProps {
+ listingType: ListingType;
+ dataType: DataType;
+ sort: SortType;
+ page: number;
+}
+
+interface UrlParams {
+ listingType?: string;
+ dataType?: string;
+ sort?: string;
+ page?: number;
+}
+
export class Main extends Component<any, MainState> {
private subscription: Subscription;
private emptyState: MainState = {
this.subscription.unsubscribe();
}
- static getDerivedStateFromProps(props) {
+ static getDerivedStateFromProps(props: any): MainProps {
return {
listingType: getListingTypeFromProps(props),
dataType: getDataTypeFromProps(props),
};
}
- componentDidUpdate(_, lastState) {
+ componentDidUpdate(_: any, lastState: MainState) {
if (
lastState.listingType !== this.state.listingType ||
lastState.dataType !== this.state.dataType ||
);
}
- updateUrl(paramUpdates: {
- listing_type?: string;
- data_type?: string;
- sort?: string;
- page?: number;
- }) {
+ updateUrl(paramUpdates: UrlParams) {
const listingTypeStr =
- paramUpdates.listing_type ||
+ paramUpdates.listingType ||
ListingType[this.state.listingType].toLowerCase();
const dataTypeStr =
- paramUpdates.data_type || DataType[this.state.dataType].toLowerCase();
+ paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
const sortStr =
paramUpdates.sort || SortType[this.state.sort].toLowerCase();
const page = paramUpdates.page || this.state.page;
}
handleListingTypeChange(val: ListingType) {
- this.updateUrl({ listing_type: ListingType[val].toLowerCase(), page: 1 });
+ this.updateUrl({ listingType: ListingType[val].toLowerCase(), page: 1 });
window.scrollTo(0, 0);
}
handleDataTypeChange(val: DataType) {
- this.updateUrl({ data_type: DataType[val].toLowerCase(), page: 1 });
+ this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
window.scrollTo(0, 0);
}
createCommentLikeRes,
createPostLikeFindRes,
commentsToFlatNodes,
+ getPageFromProps,
} from '../utils';
import { PostListing } from './post-listing';
import { UserListing } from './user-listing';
searchText: string;
}
+interface SearchProps {
+ q: string;
+ type_: SearchType;
+ sort: SortType;
+ page: number;
+}
+
+interface UrlParams {
+ q?: string;
+ type_?: string;
+ sort?: string;
+ page?: number;
+}
+
export class Search extends Component<any, SearchState> {
private subscription: Subscription;
private emptyState: SearchState = {
q: Search.getSearchQueryFromProps(this.props),
type_: Search.getSearchTypeFromProps(this.props),
sort: Search.getSortTypeFromProps(this.props),
- page: Search.getPageFromProps(this.props),
+ page: getPageFromProps(this.props),
searchText: Search.getSearchQueryFromProps(this.props),
searchResponse: {
type_: null,
: SortType.TopAll;
}
- static getPageFromProps(props: any): number {
- return props.match.params.page ? Number(props.match.params.page) : 1;
- }
-
constructor(props: any, context: any) {
super(props, context);
this.subscription.unsubscribe();
}
- static getDerivedStateFromProps(props) {
+ static getDerivedStateFromProps(props: any): SearchProps {
return {
q: Search.getSearchQueryFromProps(props),
type_: Search.getSearchTypeFromProps(props),
sort: Search.getSortTypeFromProps(props),
- page: Search.getPageFromProps(props),
+ page: getPageFromProps(props),
};
}
- componentDidUpdate(_, lastState) {
+ componentDidUpdate(_: any, lastState: SearchState) {
if (
lastState.q !== this.state.q ||
lastState.type_ !== this.state.type_ ||
}
handleSortChange(val: SortType) {
- this.updateUrl({ sort: val, page: 1 });
+ this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
}
handleTypeChange(i: Search, event: any) {
- i.updateUrl({ type_: Number(event.target.value), page: 1 });
+ i.updateUrl({
+ type_: SearchType[Number(event.target.value)].toLowerCase(),
+ page: 1,
+ });
}
handleSearchSubmit(i: Search, event: any) {
event.preventDefault();
i.updateUrl({
q: i.state.searchText,
- type_: i.state.type_,
- sort: i.state.sort,
+ type_: SearchType[i.state.type_].toLowerCase(),
+ sort: SortType[i.state.sort].toLowerCase(),
page: i.state.page,
});
}
i.setState({ searchText: event.target.value });
}
- updateUrl(paramUpdates: {
- q?: string;
- type_?: number;
- sort?: SortType;
- page?: number;
- }) {
- const qStr = paramUpdates.q || Search.getSearchQueryFromProps(this.props);
+ updateUrl(paramUpdates: UrlParams) {
+ const qStr = paramUpdates.q || this.state.q;
const typeStr =
- SearchType[paramUpdates.type_] ||
- SearchType[Search.getSearchTypeFromProps(this.props)];
+ paramUpdates.type_ || SearchType[this.state.type_].toLowerCase();
const sortStr =
- SortType[paramUpdates.sort] ||
- SortType[Search.getSortTypeFromProps(this.props)];
- const page = paramUpdates.page || Search.getPageFromProps(this.props);
+ paramUpdates.sort || SortType[this.state.sort].toLowerCase();
+ const page = paramUpdates.page || this.state.page;
this.props.history.push(
- `/search/q/${qStr}/type/${typeStr.toLowerCase()}/sort/${sortStr.toLowerCase()}/page/${page}`
+ `/search/q/${qStr}/type/${typeStr}/sort/${sortStr}/page/${page}`
);
}
this.state = this.emptyState;
}
- static getDerivedStateFromProps(props) {
- console.log('sort-select', props);
+ static getDerivedStateFromProps(props: any): SortSelectState {
return {
- sort: Number(props.sort),
+ sort: props.sort,
};
}
-import { Component } from 'inferno';
+import { Component, linkEvent } from 'inferno';
import { WebSocketService, UserService } from '../services';
import { Subscription } from 'rxjs';
import { retryWhen, delay, take, last } from 'rxjs/operators';
enableDownvotes: boolean;
enableNsfw: boolean;
view: UserDetailsView;
+ onPageChange(page: number): number | any;
}
interface UserDetailsState {
}
render() {
- return this.viewSelector(this.props.view);
+ return (
+ <div>
+ {this.viewSelector(this.props.view)}
+ {this.paginator()}
+ </div>
+ );
}
viewSelector(view: UserDetailsView) {
);
}
+ paginator() {
+ return (
+ <div class="my-2">
+ {this.props.page > 1 && (
+ <button
+ class="btn btn-sm btn-secondary mr-1"
+ onClick={linkEvent(this, this.prevPage)}
+ >
+ {i18n.t('prev')}
+ </button>
+ )}
+ {this.state.comments.length + this.state.posts.length > 0 && (
+ <button
+ class="btn btn-sm btn-secondary"
+ onClick={linkEvent(this, this.nextPage)}
+ >
+ {i18n.t('next')}
+ </button>
+ )}
+ </div>
+ );
+ }
+
+ nextPage(i: UserDetails) {
+ i.props.onPageChange(i.props.page + 1);
+ }
+
+ prevPage(i: UserDetails) {
+ i.props.onPageChange(i.props.page - 1);
+ }
+
parseMessage(msg: WebSocketJsonResponse) {
const res = wsJsonToRes(msg);
site: Site;
}
+interface UserProps {
+ view: UserDetailsView;
+ sort: SortType;
+ page: number;
+ user_id: number | null;
+ username: string;
+}
+
+interface UrlParams {
+ view?: string;
+ sort?: string;
+ page?: number;
+}
+
export class User extends Component<any, UserState> {
private subscription: Subscription;
private emptyState: UserState = {
constructor(props: any, context: any) {
super(props, context);
- this.state = Object.assign({}, this.emptyState);
+ this.state = this.emptyState;
this.handleSortChange = this.handleSortChange.bind(this);
this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
this
this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
this
);
+ this.handlePageChange = this.handlePageChange.bind(this);
this.state.user_id = Number(this.props.match.params.id) || null;
this.state.username = this.props.match.params.username;
this.subscription.unsubscribe();
}
- static getDerivedStateFromProps(props) {
+ static getDerivedStateFromProps(props: any): UserProps {
return {
view: this.getViewFromProps(props.match.params.view),
sort: this.getSortTypeFromProps(props.match.params.sort),
enableDownvotes={this.state.site.enable_downvotes}
enableNsfw={this.state.site.enable_nsfw}
view={this.state.view}
- key={this.state.user_id || this.state.username}
+ onPageChange={this.handlePageChange}
/>
- {this.paginator()}
</div>
<div class="col-12 col-md-4">
{this.userInfo()}
);
}
- paginator() {
- return (
- <div class="my-2">
- {this.state.page > 1 && (
- <button
- class="btn btn-sm btn-secondary mr-1"
- onClick={linkEvent(this, this.prevPage)}
- >
- {i18n.t('prev')}
- </button>
- )}
- {this.state.comments.length + this.state.posts.length > 0 && (
- <button
- class="btn btn-sm btn-secondary"
- onClick={linkEvent(this, this.nextPage)}
- >
- {i18n.t('next')}
- </button>
- )}
- </div>
- );
- }
-
- updateUrl() {
- let viewStr = UserDetailsView[this.state.view].toLowerCase();
- let sortStr = SortType[this.state.sort].toLowerCase();
+ updateUrl(paramUpdates: UrlParams) {
+ const page = paramUpdates.page || this.state.page;
+ const viewStr =
+ paramUpdates.view || UserDetailsView[this.state.view].toLowerCase();
+ const sortStr =
+ paramUpdates.sort || SortType[this.state.sort].toLowerCase();
this.props.history.push(
- `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
+ `/u/${this.state.username}/view/${viewStr}/sort/${sortStr}/page/${page}`
);
- this.setState(this.state);
}
- nextPage(i: User) {
- i.state.page++;
- i.updateUrl();
- }
-
- prevPage(i: User) {
- i.state.page--;
- i.updateUrl();
+ handlePageChange(page: number) {
+ this.updateUrl({ page });
}
handleSortChange(val: SortType) {
- this.state.sort = val;
- this.state.page = 1;
- this.updateUrl();
+ this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
}
handleViewChange(i: User, event: any) {
- i.state.view = Number(event.target.value);
- i.state.page = 1;
- i.updateUrl();
+ i.updateUrl({
+ view: UserDetailsView[Number(event.target.value)].toLowerCase(),
+ page: 1,
+ });
}
handleUserSettingsShowNsfwChange(i: User, event: any) {
- i.setState({
- userSettingsForm: {
- ...i.state.userSettingsForm,
- show_nsfw: event.target.checked,
- },
- });
+ i.state.userSettingsForm.show_nsfw = event.target.checked;
+ i.setState(i.state);
}
handleUserSettingsShowAvatarsChange(i: User, event: any) {