Enable loading posts from a grid view

This commit is contained in:
Nat 2021-05-11 06:29:25 -03:00
parent c754892156
commit 1dc775004e
5 changed files with 42 additions and 114 deletions

View File

@ -244,12 +244,6 @@ const ProfileJsx = ({ navigation }) => {
}; };
const RawProfileJsx = (props) => { const RawProfileJsx = (props) => {
let [state, setState] = useState({
own: props.own,
profile: props.profile,
notifs: props.notifs,
});
const notif_pack = { const notif_pack = {
active: require("assets/eva-icons/bell-unread.png"), active: require("assets/eva-icons/bell-unread.png"),
inactive: require("assets/eva-icons/bell-black.png") inactive: require("assets/eva-icons/bell-black.png")
@ -298,7 +292,7 @@ const RawProfileJsx = (props) => {
</Text> </Text>
</View> </View>
{ {
state.own ? props.own ?
<View style = { styles.profileContextContainer }> <View style = { styles.profileContextContainer }>
<TouchableOpacity <TouchableOpacity
onPress = { onPress = {
@ -334,7 +328,7 @@ const RawProfileJsx = (props) => {
} }
}> }>
{ {
state.own ? props.own ?
<>View followers</> <>View followers</>
: <>{ props.mutuals + " mutuals" }</> : <>{ props.mutuals + " mutuals" }</>
} }
@ -370,14 +364,7 @@ const RawProfileJsx = (props) => {
<GridViewJsx <GridViewJsx
posts = { props.posts } posts = { props.posts }
openPostCallback = { navigation = { props.navigation }/>
(id) => {
props.navigation.navigate("ViewPost", {
id: id,
originTab: "Profile"
});
}
} />
</View> </View>
); );
}; };

View File

@ -1,23 +1,25 @@
import React from "react"; import React, { useState, useEffect } from "react";
import { ScreenWithFullNavigationJsx } from "src/components/navigation/navigators"; import { ScreenWithBackBarJsx } from "src/components/navigation/navigators";
import { PostByIdJsx } from "src/components/posts/post"; import { PostByDataJsx } from "src/components/posts/post";
const ViewPostJsx = ({navigation}) => { const ViewPostJsx = ({navigation}) => {
const id = navigation.getParam("id", undefined); const [state, setState] = useState({
post: navigation.getParam("post", null),
loaded: false,
});
if (id == undefined) { if (state.post == null) {
throw Error("ID not specified when navigating to ViewPost!"); throw Error("Post not given when navigating to ViewPost!");
} }
return ( return (
<ScreenWithFullNavigationJsx <ScreenWithBackBarJsx
active = { navigation.getParam("originTab", "Timeline") } navigation = { navigation }>
navigation = { navigation }> <PostByDataJsx
<PostByIdJsx
navigation = { navigation } navigation = { navigation }
id = { id } /> data = { state.post } />
</ScreenWithFullNavigationJsx> </ScreenWithBackBarJsx>
); );
} }

View File

@ -1,23 +0,0 @@
import React, { useEffect, useState } from "react";
import { Image, Dimensions, TouchableWithoutFeedback } from "react-native";
const GridPostJsx = (props) => {
return (
<TouchableWithoutFeedback
onPress={ () => props.openPostCallback(props.id)}>
<Image
source = { { uri: props.previewUrl } }
style = { styles.gridImage } />
</TouchableWithoutFeedback>
)
}
const screen_width = Dimensions.get("window").width;
const styles = {
gridImage: {
width: screen_width / 3,
height: screen_width / 3
}
};
export default GridPostJsx;

View File

@ -3,10 +3,9 @@ import {
View, View,
Dimensions, Dimensions,
Image, Image,
TouchableOpacity,
} from "react-native"; } from "react-native";
import GridPostJsx from "src/components/posts/grid-post";
function partition(arr, size) { function partition(arr, size) {
let newArray = []; let newArray = [];
for (let i = 0; i < arr.length; i += size) { for (let i = 0; i < arr.length; i += size) {
@ -17,6 +16,23 @@ function partition(arr, size) {
return newArray return newArray
} }
const GridPostJsx = (props) => {
return (
<TouchableOpacity
onPress={ () => {
props.navigation.navigate("ViewPost", {
post: props.data,
});
}}>
<Image
source = {
{ uri: props.data.media_attachments[0].preview_url }
}
style = { styles.gridImage } />
</TouchableOpacity>
)
}
const GridViewJsx = (props) => { const GridViewJsx = (props) => {
let rows = partition(props.posts, 3); let rows = partition(props.posts, 3);
return ( return (
@ -28,19 +44,11 @@ const GridViewJsx = (props) => {
key = { i }> key = { i }>
{ {
row.map((post) => { row.map((post) => {
const post_url = post
.media_attachments[0]
.preview_url;
return ( return (
<View key = { post.id }> <View key = { post.id }>
<GridPostJsx <GridPostJsx
id = { post.id } navigation = { props.navigation }
previewUrl = { post_url } data = { post } />
openPostCallback = {
(id) => props.openPostCallback(id)
}
/>
</View> </View>
); );
}) })
@ -59,7 +67,11 @@ const styles = {
padding: 0, padding: 0,
margin: 0, margin: 0,
flexDirection: "row" flexDirection: "row"
} },
gridImage: {
width: screen_width / 3,
height: screen_width / 3
},
}; };
export default GridViewJsx; export default GridViewJsx;

View File

@ -171,56 +171,6 @@ export const PostByDataJsx = (props) => {
); );
} }
export const PostByIdJsx = (props) => {
/*
Renders a post given the post's ID in the properties, as is done when
retrieving an individual post on someone's profile.
*/
let [state, setState] = useState({
avatar: "",
username: "",
media_attachments: [],
favourited: false,
reblogged: false,
content: "",
timestamp: 0,
loaded: false,
dimensions: []
});
useEffect(() => {
// TODO: Make API request using props.id, set it as the state
(() => {
Promise.all(getDimensionsPromises([{ url: TEST_IMAGE }]))
.then(dimensions => {
setState({
avatar: TEST_IMAGE,
username: "njms",
media_attachments: [{ url: TEST_IMAGE }],
favourited: false,
reblogged: false,
content: "Also learning Claire de Lune feels a lot like reading the communist manifesto",
timestamp: 1596745156000,
loaded: true,
dimensions: dimensions
});
});
})();
}, []);
return (
<View>
{ state.loaded ?
<RawPostJsx
data = { state }
dimensions = { state.dimensions }
navigation = { props.navigation }/>
: <View></View>
}
</View>
)
}
const styles = { const styles = {
postHeader: { postHeader: {
display: "flex", display: "flex",