From 0eb4f8599ed29f715fba25f14dda8e7cf6acd75e Mon Sep 17 00:00:00 2001 From: natjms Date: Sat, 22 May 2021 14:06:13 -0300 Subject: [PATCH] Enable following and unfollowing users --- src/components/pages/profile.js | 69 ++++++++++++++++++++++++++------- src/requests.js | 10 +++++ 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/components/pages/profile.js b/src/components/pages/profile.js index a915d6c..05a2dbb 100644 --- a/src/components/pages/profile.js +++ b/src/components/pages/profile.js @@ -66,7 +66,7 @@ const ViewProfileJsx = ({navigation}) => { }); useEffect(() => { - let ownProfile, ownDomain, accessToken, domain; + let ownProfile, instance, accessToken, domain; AsyncStorage .multiGet(["@user_profile", "@user_instance", "@user_token"]) .then(([ ownProfilePair, ownDomainPair, tokenPair ]) => { @@ -96,10 +96,35 @@ const ViewProfileJsx = ({navigation}) => { setState({...state, mutuals: getMutuals(ownFollowing, theirFollowers), posts: posts, + instance, + ownProfile, + accessToken, + followed: ownFollowing.some(x => x.id == state.profile.id), loaded: true, }); }); }, []); + + const _handleFollow = async () => { + if (!state.followed) { + await requests.followAccount( + state.instance, + state.profile.id, + state.accessToken + ); + } else { + await requests.unfollowAccount( + state.instance, + state.profile.id, + state.accessToken + ); + } + + setState({...state, + followed: !state.followed, + }); + }; + return ( <> { state.loaded @@ -107,9 +132,10 @@ const ViewProfileJsx = ({navigation}) => { active = { navigation.getParam("originTab") } navigation = { navigation }> : <> @@ -197,16 +223,27 @@ const RawProfileJsx = (props) => { props.navigation.navigate("Settings"); } }> - - Settings + + Settings ); } else { profileButton = ( - - - Follow + + + + { props.followed + ? "Unfollow" + : "Follow" + } + ) @@ -373,15 +410,17 @@ const styles = { textDecorationLine: "underline" }, button: { - borderWidth: 1, - borderColor: "#888", - borderRadius: 5, + container: { + borderWidth: 1, + borderColor: "#888", + borderRadius: 5, - padding: 10, - marginTop: 10 - }, - buttonText: { - textAlign: "center" + padding: 10, + marginTop: 10 + }, + dark: { backgroundColor: "black", }, + text: { textAlign: "center" }, + darkText: { color: "white", }, }, strong: { fontWeight: "bold", diff --git a/src/requests.js b/src/requests.js index fde88c0..3f949e4 100644 --- a/src/requests.js +++ b/src/requests.js @@ -137,6 +137,16 @@ export async function fetchFollowers(domain, id, token) { return resp.json(); } +export async function followAccount(domain, id, token) { + const resp = await post(`https://${domain}/api/v1/accounts/${id}/follow`, token); + return resp.json(); +} + +export async function unfollowAccount(domain, id, token) { + const resp = await post(`https://${domain}/api/v1/accounts/${id}/unfollow`, token); + return resp.json(); +} + export async function fetchHomeTimeline(domain, token, params = false) { const resp = await get( `https://${domain}/api/v1/timelines/home`,