Enable following and unfollowing users

This commit is contained in:
Nat 2021-05-22 14:06:13 -03:00
parent eb47557cc4
commit 0eb4f8599e
2 changed files with 64 additions and 15 deletions

View File

@ -66,7 +66,7 @@ const ViewProfileJsx = ({navigation}) => {
}); });
useEffect(() => { useEffect(() => {
let ownProfile, ownDomain, accessToken, domain; let ownProfile, instance, accessToken, domain;
AsyncStorage AsyncStorage
.multiGet(["@user_profile", "@user_instance", "@user_token"]) .multiGet(["@user_profile", "@user_instance", "@user_token"])
.then(([ ownProfilePair, ownDomainPair, tokenPair ]) => { .then(([ ownProfilePair, ownDomainPair, tokenPair ]) => {
@ -96,10 +96,35 @@ const ViewProfileJsx = ({navigation}) => {
setState({...state, setState({...state,
mutuals: getMutuals(ownFollowing, theirFollowers), mutuals: getMutuals(ownFollowing, theirFollowers),
posts: posts, posts: posts,
instance,
ownProfile,
accessToken,
followed: ownFollowing.some(x => x.id == state.profile.id),
loaded: true, 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 ( return (
<> <>
{ state.loaded { state.loaded
@ -107,9 +132,10 @@ const ViewProfileJsx = ({navigation}) => {
active = { navigation.getParam("originTab") } active = { navigation.getParam("originTab") }
navigation = { navigation }> navigation = { navigation }>
<RawProfileJsx <RawProfileJsx
onFollow = { _handleFollow }
profile = { state.profile } profile = { state.profile }
mutuals = { state.mutuals } mutuals = { state.mutuals }
notifs = { state.notifs } followed = { state.followed }
posts = { state.posts }/> posts = { state.posts }/>
</ScreenWithBackBarJsx> </ScreenWithBackBarJsx>
: <></> : <></>
@ -197,16 +223,27 @@ const RawProfileJsx = (props) => {
props.navigation.navigate("Settings"); props.navigation.navigate("Settings");
} }
}> }>
<View style = { styles.button }> <View style = { styles.button.container }>
<Text style = { styles.buttonText }>Settings</Text> <Text style = { styles.button.text }>Settings</Text>
</View> </View>
</TouchableOpacity> </TouchableOpacity>
); );
} else { } else {
profileButton = ( profileButton = (
<TouchableOpacity onPress = { _handleFollow }> <TouchableOpacity onPress = { props.onFollow }>
<View style = { styles.button }> <View style = { [
<Text style = { styles.buttonText }>Follow</Text> styles.button.container,
props.followed ? styles.button.dark : {},
] }>
<Text style = {[
styles.button.text,
props.followed ? styles.button.darkText : {},
]}>
{ props.followed
? "Unfollow"
: "Follow"
}
</Text>
</View> </View>
</TouchableOpacity> </TouchableOpacity>
) )
@ -373,15 +410,17 @@ const styles = {
textDecorationLine: "underline" textDecorationLine: "underline"
}, },
button: { button: {
borderWidth: 1, container: {
borderColor: "#888", borderWidth: 1,
borderRadius: 5, borderColor: "#888",
borderRadius: 5,
padding: 10, padding: 10,
marginTop: 10 marginTop: 10
}, },
buttonText: { dark: { backgroundColor: "black", },
textAlign: "center" text: { textAlign: "center" },
darkText: { color: "white", },
}, },
strong: { strong: {
fontWeight: "bold", fontWeight: "bold",

View File

@ -137,6 +137,16 @@ export async function fetchFollowers(domain, id, token) {
return resp.json(); 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) { export async function fetchHomeTimeline(domain, token, params = false) {
const resp = await get( const resp = await get(
`https://${domain}/api/v1/timelines/home`, `https://${domain}/api/v1/timelines/home`,