Auto fetch new posts for front page and communities.

- Fixes #286
This commit is contained in:
Dessalines 2019-10-13 12:37:10 -07:00
parent 27f566ed95
commit b16d78aaff
3 changed files with 26 additions and 5 deletions

View file

@ -5,7 +5,7 @@ import { UserOperation, Community as CommunityI, GetCommunityResponse, Community
import { WebSocketService } from '../services'; import { WebSocketService } from '../services';
import { PostListings } from './post-listings'; import { PostListings } from './post-listings';
import { Sidebar } from './sidebar'; import { Sidebar } from './sidebar';
import { msgOp, routeSortTypeToEnum, fetchLimit } from '../utils'; import { msgOp, routeSortTypeToEnum, fetchLimit, postRefetchSeconds } from '../utils';
import { T, i18n } from 'inferno-i18next'; import { T, i18n } from 'inferno-i18next';
interface State { interface State {
@ -23,6 +23,7 @@ interface State {
export class Community extends Component<any, State> { export class Community extends Component<any, State> {
private subscription: Subscription; private subscription: Subscription;
private postFetcher: any;
private emptyState: State = { private emptyState: State = {
community: { community: {
id: null, id: null,
@ -79,10 +80,12 @@ export class Community extends Component<any, State> {
WebSocketService.Instance.getCommunityByName(this.state.communityName); WebSocketService.Instance.getCommunityByName(this.state.communityName);
} }
this.keepFetchingPosts();
} }
componentWillUnmount() { componentWillUnmount() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
clearInterval(this.postFetcher);
} }
// Necessary for back button for some reason // Necessary for back button for some reason
@ -161,6 +164,7 @@ export class Community extends Component<any, State> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
} }
prevPage(i: Community) { prevPage(i: Community) {
@ -168,6 +172,7 @@ export class Community extends Component<any, State> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
} }
handleSortChange(i: Community, event: any) { handleSortChange(i: Community, event: any) {
@ -176,6 +181,7 @@ export class Community extends Component<any, State> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
} }
updateUrl() { updateUrl() {
@ -183,6 +189,11 @@ export class Community extends Component<any, State> {
this.props.history.push(`/c/${this.state.community.name}/sort/${sortStr}/page/${this.state.page}`); this.props.history.push(`/c/${this.state.community.name}/sort/${sortStr}/page/${this.state.page}`);
} }
keepFetchingPosts() {
this.fetchPosts();
this.postFetcher = setInterval(() => this.fetchPosts(), postRefetchSeconds);
}
fetchPosts() { fetchPosts() {
let getPostsForm: GetPostsForm = { let getPostsForm: GetPostsForm = {
page: this.state.page, page: this.state.page,
@ -221,7 +232,6 @@ export class Community extends Component<any, State> {
let res: GetPostsResponse = msg; let res: GetPostsResponse = msg;
this.state.posts = res.posts; this.state.posts = res.posts;
this.state.loading = false; this.state.loading = false;
window.scrollTo(0,0);
this.setState(this.state); this.setState(this.state);
} else if (op == UserOperation.CreatePostLike) { } else if (op == UserOperation.CreatePostLike) {
let res: CreatePostLikeResponse = msg; let res: CreatePostLikeResponse = msg;

View file

@ -6,7 +6,7 @@ import { UserOperation, CommunityUser, GetFollowedCommunitiesResponse, ListCommu
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { PostListings } from './post-listings'; import { PostListings } from './post-listings';
import { SiteForm } from './site-form'; import { SiteForm } from './site-form';
import { msgOp, repoUrl, mdToHtml, fetchLimit, routeSortTypeToEnum, routeListingTypeToEnum } from '../utils'; import { msgOp, repoUrl, mdToHtml, fetchLimit, routeSortTypeToEnum, routeListingTypeToEnum, postRefetchSeconds } from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
import { T } from 'inferno-i18next'; import { T } from 'inferno-i18next';
@ -25,6 +25,7 @@ interface MainState {
export class Main extends Component<any, MainState> { export class Main extends Component<any, MainState> {
private subscription: Subscription; private subscription: Subscription;
private postFetcher: any;
private emptyState: MainState = { private emptyState: MainState = {
subscribedCommunities: [], subscribedCommunities: [],
trendingCommunities: [], trendingCommunities: [],
@ -98,11 +99,12 @@ export class Main extends Component<any, MainState> {
WebSocketService.Instance.listCommunities(listCommunitiesForm); WebSocketService.Instance.listCommunities(listCommunitiesForm);
this.fetchPosts(); this.keepFetchingPosts();
} }
componentWillUnmount() { componentWillUnmount() {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
clearInterval(this.postFetcher);
} }
// Necessary for back button for some reason // Necessary for back button for some reason
@ -362,6 +364,7 @@ export class Main extends Component<any, MainState> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
} }
prevPage(i: Main) { prevPage(i: Main) {
@ -370,6 +373,7 @@ export class Main extends Component<any, MainState> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
} }
handleSortChange(i: Main, event: any) { handleSortChange(i: Main, event: any) {
@ -379,6 +383,7 @@ export class Main extends Component<any, MainState> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
} }
handleTypeChange(i: Main, event: any) { handleTypeChange(i: Main, event: any) {
@ -388,6 +393,12 @@ export class Main extends Component<any, MainState> {
i.setState(i.state); i.setState(i.state);
i.updateUrl(); i.updateUrl();
i.fetchPosts(); i.fetchPosts();
window.scrollTo(0,0);
}
keepFetchingPosts() {
this.fetchPosts();
this.postFetcher = setInterval(() => this.fetchPosts(), postRefetchSeconds);
} }
fetchPosts() { fetchPosts() {
@ -437,7 +448,6 @@ export class Main extends Component<any, MainState> {
let res: GetPostsResponse = msg; let res: GetPostsResponse = msg;
this.state.posts = res.posts; this.state.posts = res.posts;
this.state.loading = false; this.state.loading = false;
window.scrollTo(0,0);
this.setState(this.state); this.setState(this.state);
} else if (op == UserOperation.CreatePostLike) { } else if (op == UserOperation.CreatePostLike) {
let res: CreatePostLikeResponse = msg; let res: CreatePostLikeResponse = msg;

1
ui/src/utils.ts vendored
View file

@ -17,6 +17,7 @@ import * as emojiShortName from 'emoji-short-name';
export const repoUrl = 'https://github.com/dessalines/lemmy'; export const repoUrl = 'https://github.com/dessalines/lemmy';
export const markdownHelpUrl = 'https://commonmark.org/help/'; export const markdownHelpUrl = 'https://commonmark.org/help/';
export const postRefetchSeconds: number = 60*1000;
export const fetchLimit: number = 20; export const fetchLimit: number = 20;
export const mentionDropdownFetchLimit = 6; export const mentionDropdownFetchLimit = 6;