Compare commits
No commits in common. "fee1475779796224b2528d1a98cd273819ec9d77" and "2409fa2b8d5a63c81bac840125da132607ac3d2a" have entirely different histories.
fee1475779
...
2409fa2b8d
@ -13,44 +13,22 @@ let interval: NodeJS.Timer | null = null;
|
||||
let moreOlderPostsAvailable = true;
|
||||
let loadingOlderPosts = false;
|
||||
|
||||
interface FetchOptions {
|
||||
since?: string,
|
||||
before?: string,
|
||||
count?: number
|
||||
}
|
||||
|
||||
async function fetchPosts(options: FetchOptions): Promise<Post[]> {
|
||||
const params = new URLSearchParams();
|
||||
if (options?.since !== undefined) {
|
||||
params.set('since', options.since);
|
||||
}
|
||||
if (options?.before !== undefined) {
|
||||
params.set('before', options.before);
|
||||
}
|
||||
if (options?.count !== undefined) {
|
||||
params.set('count', options.count.toFixed(0));
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/posts?${params}`);
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
function filterDuplicates(posts: Post[]): Post[] {
|
||||
return posts.filter((obj, index, arr) => {
|
||||
return arr.map(mapObj => mapObj.url).indexOf(obj.url) === index;
|
||||
});
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
let filter: FetchOptions = {};
|
||||
const params = new URLSearchParams();
|
||||
if (data.posts.length > 0) {
|
||||
filter = { since: data.posts[0].created_at };
|
||||
params.set('since', data.posts[0].created_at);
|
||||
}
|
||||
fetchPosts(filter).then(resp => {
|
||||
fetch(`/api/posts?${params}`)
|
||||
.then(r => r.json())
|
||||
.then((resp: Post[]) => {
|
||||
if (resp.length > 0) {
|
||||
// Prepend new posts, filter dupes
|
||||
// There shouldn't be any duplicates, but better be safe than sorry
|
||||
data.posts = filterDuplicates(resp.concat(data.posts));
|
||||
const combined = resp.concat(data.posts);
|
||||
const filteredPosts = combined.filter((obj, index, arr) => {
|
||||
return arr.map(mapObj => mapObj.url).indexOf(obj.url) === index;
|
||||
});
|
||||
data.posts = filteredPosts;
|
||||
}
|
||||
})
|
||||
.catch(e => {
|
||||
@ -79,21 +57,24 @@ onMount(async () => {
|
||||
}
|
||||
});
|
||||
|
||||
function loadOlderPosts() {
|
||||
async function loadOlderPosts() {
|
||||
loadingOlderPosts = true;
|
||||
const filter: FetchOptions = { count: 20 };
|
||||
const params = new URLSearchParams();
|
||||
if (data.posts.length > 0) {
|
||||
filter.before = data.posts[data.posts.length - 1].created_at;
|
||||
params.set('before', data.posts[data.posts.length - 1].created_at);
|
||||
}
|
||||
|
||||
|
||||
fetchPosts(filter).then(resp => {
|
||||
await fetch(`/api/posts?${params}`)
|
||||
.then(r => r.json())
|
||||
.then((resp: Post[]) => {
|
||||
if (resp.length > 0) {
|
||||
// Append old posts, filter dupes
|
||||
// There shouldn't be any duplicates, but better be safe than sorry
|
||||
data.posts = filterDuplicates(data.posts.concat(resp));
|
||||
// If we got less than we expected, there are no older posts available
|
||||
moreOlderPostsAvailable = resp.length < (filter.count ?? 20);
|
||||
const combined = data.posts.concat(resp);
|
||||
const filteredPosts = combined.filter((obj, index, arr) => {
|
||||
return arr.map(mapObj => mapObj.url).indexOf(obj.url) === index;
|
||||
});
|
||||
data.posts = filteredPosts;
|
||||
moreOlderPostsAvailable = true;
|
||||
} else {
|
||||
moreOlderPostsAvailable = false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user