New refresh logic

This commit is contained in:
Imbus 2023-12-15 01:24:37 +01:00
parent 3855a587a0
commit 72a4b26c82
3 changed files with 41 additions and 9 deletions

View file

@ -12,11 +12,11 @@ import { style } from "../util/style";
export function PostView({ post }: { post: Post }): JSX.Element { export function PostView({ post }: { post: Post }): JSX.Element {
// WARNING THIS IS NOT ACCEPTABLE WARNING REMOVE WARNING // WARNING THIS IS NOT ACCEPTABLE WARNING REMOVE WARNING
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const [thisPost, setThisPost] = React.useState<Post>(post); // const [thisPost, setThisPost] = React.useState<Post>(post);
return ( return (
<View style={style.postView}> <View style={style.postView}>
<Text style={style.postFont}>{thisPost.content}</Text> <Text style={style.postFont}>{post.id + " " + post.content}</Text>
</View> </View>
); );
} }

View file

@ -17,20 +17,34 @@ export function PostsContainer(): JSX.Element {
React.useEffect(() => { React.useEffect(() => {
refreshPosts(); refreshPosts();
}, []);
React.useEffect(() => {
fetchMorePosts();
}, [page]); }, [page]);
// Full refresh
function refreshPosts(): void { function refreshPosts(): void {
async function fetchPosts(): Promise<void> { async function fetchPosts(): Promise<void> {
const newPosts = await getPostsInterval(page*10, 10) setPage(0);
setPostData((oldPosts: Post[]) => [...oldPosts, ...newPosts]); setPostData(await getPostsInterval(0, 10));
}
setPostData([])
fetchPosts();
}
// Incremental fetch, should prune old posts
function fetchMorePosts(): void {
async function fetchPosts(): Promise<void> {
const newPosts = await getPostsInterval(page * 10, 10);
setPostData((oldPosts) => [...oldPosts, ...newPosts]);
} }
fetchPosts(); fetchPosts();
} }
function onRefresh(): void { function onRefresh(): void {
setRefreshing(true); setRefreshing(true);
setPostData([]); refreshPosts();
setPage(0);
setRefreshing(false); setRefreshing(false);
} }
@ -43,8 +57,8 @@ export function PostsContainer(): JSX.Element {
<VirtualizedList <VirtualizedList
data={postData} data={postData}
initialNumToRender={4} initialNumToRender={4}
renderItem={({ item }) => <PostView key={item.id} post={item} />} renderItem={({ item }) => <PostView key={item.id + item.createdAt} post={item} />}
keyExtractor={(item: Post) => item.id} keyExtractor={(item: Post) => item.id + item.createdAt}
getItemCount={getItemCount} getItemCount={getItemCount}
getItem={getItem} getItem={getItem}
onEndReached={handleEndReached} onEndReached={handleEndReached}

View file

@ -10,7 +10,6 @@ const colorScheme = {
export const style = StyleSheet.create({ export const style = StyleSheet.create({
app: { app: {
// Outermost container
flex: 1, flex: 1,
backgroundColor: colorScheme.background, // For the "entire" app backgroundColor: colorScheme.background, // For the "entire" app
alignItems: "center", alignItems: "center",
@ -44,4 +43,23 @@ export const style = StyleSheet.create({
fontWeight: "bold", fontWeight: "bold",
color: colorScheme.postFont, color: colorScheme.postFont,
}, },
newPostContainer: {
backgroundColor: colorScheme.background,
margin: 0,
padding: 0,
width: "100%",
height: "100%",
alignItems: "center",
justifyContent: "center",
},
newPostInput: {
backgroundColor: colorScheme.postBox,
width: "100%",
height: "100%",
padding: 10,
color: colorScheme.postFont,
},
errorText: {
color: colorScheme.error,
},
}); });