From 995a672270d84676f8749c5d2a06ceb0914aec9c Mon Sep 17 00:00:00 2001 From: natjms Date: Wed, 5 May 2021 17:21:55 -0300 Subject: [PATCH] Implement home timeline requests --- src/components/pages/authenticate.js | 2 +- src/components/pages/feed.js | 85 ++++++++++++++++++++-------- src/components/posts/post.js | 14 +++-- src/requests.js | 40 ++++++++++--- 4 files changed, 103 insertions(+), 38 deletions(-) diff --git a/src/components/pages/authenticate.js b/src/components/pages/authenticate.js index 1d4817a..807dddf 100644 --- a/src/components/pages/authenticate.js +++ b/src/components/pages/authenticate.js @@ -55,7 +55,7 @@ const AuthenticateJsx = ({navigation}) => { .then(resp => resp.json()); // Store the token - AsyncStorage.setItem("@user_token", JSON.stringify(token)); + await AsyncStorage.setItem("@user_token", JSON.stringify(token)); const profile = await requests.get( `${api}/api/v1/accounts/verify_credentials`, diff --git a/src/components/pages/feed.js b/src/components/pages/feed.js index 02d017c..28dac04 100644 --- a/src/components/pages/feed.js +++ b/src/components/pages/feed.js @@ -1,10 +1,14 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; import { Dimensions, View, Image, Text } from "react-native"; import TimelineViewJsx from "src/components/posts/timeline-view"; import { ScreenWithTrayJsx } from "src/components/navigation/navigators"; import { TouchableWithoutFeedback } from "react-native-gesture-handler"; +import AsyncStorage from "@react-native-async-storage/async-storage"; + +import * as requests from "src/requests"; + const TEST_IMAGE = "https://cache.desktopnexus.com/thumbseg/2255/2255124-bigthumbnail.jpg"; const TEST_POSTS = [ @@ -40,35 +44,68 @@ const TEST_POSTS = [ const FeedJsx = (props) => { const checkmark = require("assets/eva-icons/checkmark-circle-large.png"); + const [state, setState] = useState({ + loaded: false, + }); + + useEffect(() => { + let accessToken; + let instance; + + AsyncStorage + .multiGet([ + "@user_token", + "@user_instance", + ]) + .then(([tokenPair, instancePair]) => { + accessToken = JSON.parse(tokenPair[1]).access_token; + instance = instancePair[1]; + + return requests.fetchHomeTimeline( + instance, + accessToken + ) + }) + .then(posts => + setState({...state, + posts: posts, + loaded: true, + }) + ); + }, []); return ( - + <> + { state.loaded + ? - - + + - - + + - - You're all caught up. - - Wow, it sure is a lovely day outside 🌳 - - - See older posts - - - - + + You're all caught up. + + Wow, it sure is a lovely day outside 🌳 + + See older posts + + + + + : <> + } + ); }; diff --git a/src/components/posts/post.js b/src/components/posts/post.js index a2d433e..ad693b2 100644 --- a/src/components/posts/post.js +++ b/src/components/posts/post.js @@ -76,9 +76,10 @@ export const RawPostJsx = (props) => { - { props.data.username } + source = { { uri: props.data.account.avatar } } /> + + { props.data.account.username } + @@ -112,7 +113,10 @@ export const RawPostJsx = (props) => { reblogged = { props.data.reblogged } /> - { props.data.username } { props.data.content } + + { props.data.account.username } + +  { props.data.content } { - { timeToAge((new Date()).getTime(), props.data.timestamp) } + { timeToAge((new Date()).getTime(), props.data.created_at) } diff --git a/src/requests.js b/src/requests.js index 3147c0a..5bdacfe 100644 --- a/src/requests.js +++ b/src/requests.js @@ -4,6 +4,16 @@ const TEST_NOTIFICATIONS = [{ id: 1 }, { id: 2 }]; const TEST_NEW_NOTIFICATIONS_1 = [{ id: 1 }, { id: 2 }]; const TEST_NEW_NOTIFICATIONS_2 = [{ id: 1 }, { id: 2 }, { id: 3 }]; +function objectToForm(obj) { + let form = new FormData(); + + Object.keys(obj).forEach(key => + form.append(key, obj[key]) + ); + + return form; +} + export async function checkUnreadNotifications() { // If the check has already been made since the last time notifications.js // has been opened @@ -31,14 +41,9 @@ export async function checkUnreadNotifications() { export async function postForm(url, data, token = false) { // Send a POST request with data formatted with FormData returning JSON - let form = new FormData(); - for (let key in data) { - form.append(key, data[key]); - } - const resp = await fetch(url, { method: "POST", - body: form, + body: objectToForm(data), headers: token ? { "Authorization": `Bearer ${token}`, } : {}, @@ -47,13 +52,23 @@ export async function postForm(url, data, token = false) { return resp; } -export function get(url, token = false) { - return fetch(url, { +export async function get(url, token = false, data = false) { + let completeURL; + if (data) { + let params = new URLSearchParams(data) + completeURL = `${url}?${params.toString()}`; + } else { + completeURL = url; + } + + const resp = await fetch(completeURL, { method: "GET", headers: token ? { "Authorization": `Bearer ${token}`, } : {}, }); + + return resp; } export async function fetchProfile(domain, id) { @@ -70,3 +85,12 @@ export async function fetchFollowers(domain, id, token) { const resp = await get(`https://${domain}/api/v1/accounts/${id}/followers`, token); return resp.json(); } + +export async function fetchHomeTimeline(domain, token, params = false) { + const resp = await get( + `https://${domain}/api/v1/timelines/home`, + token, + params + ); + return resp.json(); +}