Style demo

This commit is contained in:
Imbus 2023-12-14 22:16:09 +01:00
parent 755aed62c6
commit a656b409ac
4 changed files with 65 additions and 21 deletions

View file

@ -1,26 +1,66 @@
import React from "react"; import React from "react";
import { View, Text } from "react-native"; import { View, Text } from "react-native";
import { getPosts, Post } from "../util/api"; import { getPost, getPosts, Post } from "../util/api";
import { PostView } from "./PostView"; import { PostView } from "./PostView";
import { style } from "../util/style"; import { style } from "../util/style";
import { SafeAreaView } from "react-native";
import { VirtualizedList } from "react-native";
export function PostsContainer(): JSX.Element { type ItemData = {
const [posts, setPosts] = React.useState<Post[]>([]); id: string;
title: string;
};
React.useEffect(() => { type ItemProps = {
async function fetchPosts(): Promise<void> { title: string;
const posts = await getPosts(); };
setPosts(posts);
}
fetchPosts();
}, []);
return ( const Item = ({title}: ItemProps): JSX.Element => (
<View style={style.postsContainer}> <View style={style.postView}>
<Text>Hello World!</Text> <Text style={style.postFont}>{title}</Text>
{posts.map((post) => (
<PostView key={post.id} post={post} />
))}
</View> </View>
); );
export function PostsContainer(): JSX.Element {
// const [posts, setPosts] = React.useState<Post[]>([]);
// React.useEffect(() => {
// refreshPosts();
// }, []);
const getItemCount = (_data: unknown): number => 50;
// function refreshPosts(): void {
// async function fetchPosts(): Promise<void> {
// const posts = await getPosts();
// setPosts(posts);
// }
// fetchPosts();
// }
// function getItemCount(): number {
// return posts.length;
// }
const getItem = (_data: unknown, index: number): ItemData => ({
id: Math.random().toString(12).substring(0),
title: `Item ${index + 1}`,
});
// const getItem = async (data: any, index: number): Promise<Post> => {
// const p = await getPost(index);
// return p;
// };
return (
<SafeAreaView style={style.postsContainer}>
<VirtualizedList
initialNumToRender={4}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={(item: ItemData) => item.id}
getItemCount={getItemCount}
getItem={getItem}
/>
</SafeAreaView>
);
} }

View file

@ -63,7 +63,7 @@ export async function getPosts(): Promise<Post[]> {
* @param {string} id - The ID of the post to fetch. * @param {string} id - The ID of the post to fetch.
* @returns {Promise<Post>} A promise that resolves to the requested post. * @returns {Promise<Post>} A promise that resolves to the requested post.
*/ */
export async function getPost(id: string): Promise<Post> { export async function getPost(id: number): Promise<Post> {
const res = await fetch(URL + `/api/posts/${id}`); const res = await fetch(URL + `/api/posts/${id}`);
const data = await res.json(); const data = await res.json();
return data; return data;

View file

@ -14,19 +14,23 @@ export const style = StyleSheet.create({
backgroundColor: colorScheme.background, // For the "entire" app backgroundColor: colorScheme.background, // For the "entire" app
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
margin: 0,
}, },
postsContainer: { postsContainer: {
margin: 0,
height: '100%', height: '100%',
width: '100%',
padding: 5, padding: 5,
flex: 1, paddingVertical: 30,
alignItems: 'center',
justifyContent: 'flex-start', justifyContent: 'flex-start',
backgroundColor: colorScheme.background, // For the container holding the posts backgroundColor: colorScheme.background, // For the container holding the posts
}, },
postView: { postView: {
borderRadius: 6,
flexDirection: "row", flexDirection: "row",
backgroundColor: colorScheme.postBox, backgroundColor: colorScheme.postBox,
paddingVertical: 5, margin: 5,
padding: 15,
}, },
postFont: { postFont: {
color: colorScheme.postFont, color: colorScheme.postFont,