From 1cc44eae7f530eb580f12e069d5c893272274f1a Mon Sep 17 00:00:00 2001 From: natjms Date: Sat, 22 May 2021 11:26:10 -0300 Subject: [PATCH] Enable favouriting statuses --- src/components/posts/post-action-bar.js | 41 ++++--------- src/components/posts/post.js | 79 +++++++++++++++++++++---- src/requests.js | 21 +++++++ 3 files changed, 101 insertions(+), 40 deletions(-) diff --git a/src/components/posts/post-action-bar.js b/src/components/posts/post-action-bar.js index ac5c5c4..2cc2326 100644 --- a/src/components/posts/post-action-bar.js +++ b/src/components/posts/post-action-bar.js @@ -4,17 +4,17 @@ import { Text, View, Dimensions, - TouchableWithoutFeedback + TouchableOpacity } from "react-native"; import { activeOrNot } from "src/interface/interactions"; const PostActionJsx = (props) => { return ( - + { props.last ? styles.lastIcon : {} ] } /> - + ) } const PostActionBarJsx = (props) => { - let [state, setState] = useState({ - favourited: props.favourited, - commenting: false, - reblogged: props.reblogged, - bookmarked: false - }); - const icons = { heart: { active: require("assets/eva-icons/post-actions/heart-active.png"), @@ -53,33 +46,21 @@ const PostActionBarJsx = (props) => { { - setState({ ...state, favourited: !state.favourited }); - } - } /> + active = { props.favourited } + onPress = { props.onFavourite } /> { - setState({ ...state, reblogged: !state.reblogged }); - } - } /> + reblogged = { props.reblogged } + onPress = { props.onReblog }/> { - setState({ ...state, bookmarked: !state.bookmarked }); - } - } /> + bookmarked = { props.bookmarked } + onPress = { props.onBookmark } /> ) } diff --git a/src/components/posts/post.js b/src/components/posts/post.js index efc661f..884e87a 100644 --- a/src/components/posts/post.js +++ b/src/components/posts/post.js @@ -8,7 +8,10 @@ import { ScrollView, } from "react-native"; -import { pluralize, timeToAge} from "src/interface/rendering" +import { pluralize, timeToAge } from "src/interface/rendering" + +import AsyncStorage from "@react-native-async-storage/async-storage"; +import * as requests from "src/requests"; import PostActionBarJsx from "src/components/posts/post-action-bar"; @@ -46,6 +49,20 @@ function getDimensionsPromises(uris) { })); } +function handleFavouriteFactory(state, setState) { + return async () => { + const newStatus = await requests.favouriteStatus( + state.instance, + state.data.id, + state.accessToken + ); + + setState({...state, + data: newStatus, + }); + }; +} + const PostImageJsx = (props) => { return { flex: 1, width: SCREEN_WIDTH, height: getAutoHeight(props.width, props.height, SCREEN_WIDTH), - // objectFit: "cover" } } /> }; @@ -120,7 +136,10 @@ export const RawPostJsx = (props) => { } + reblogged = { props.data.reblogged } + bookmarked = { false } + onFavourite = { props.onFavourite } + onBookmark = { props.onBookmark } /> @@ -156,25 +175,65 @@ export const PostByDataJsx = (props) => { let [state, setState] = useState({ loaded: false, + data: props.data, dimensions: [] }); useEffect(() => { - Promise.all(getDimensionsPromises(props.data.media_attachments)) - .then(dimensions => { - setState({ - dimensions: dimensions, - loaded: true + let instance, accessToken; + AsyncStorage + .multiGet([ + "@user_instance", + "@user_token", + ]) + .then(([instancePair, tokenPair]) => { + instance = instancePair[1]; + accessToken = JSON.parse(tokenPair[1]).access_token; + }) + .then(() => + Promise.all( + getDimensionsPromises(props.data.media_attachments) + ) + ) + .then(dimensions => { + setState({...state, + dimensions: dimensions, + instance, + accessToken, + loaded: true + }); }); - }); }, []); + const _handleFavourite = async () => { + let newStatus; + + if (!state.data.favourited) { + newStatus = await requests.favouriteStatus( + state.instance, + state.data.id, + state.accessToken + ); + } else { + newStatus = await requests.unfavouriteStatus( + state.instance, + state.data.id, + state.accessToken + ); + } + + setState({...state, + data: newStatus, + }); + }; + return ( { state.loaded ? : } diff --git a/src/requests.js b/src/requests.js index ec1103a..8caae66 100644 --- a/src/requests.js +++ b/src/requests.js @@ -52,6 +52,17 @@ export async function postForm(url, data, token = false) { return resp; } +export async function post(url, token = false) { + const resp = await fetch(url, { + method: "POST", + headers: token + ? { "Authorization": `Bearer ${token}`, } + : {}, + }); + + return resp; +} + export async function get(url, token = false, data = false) { let completeURL; if (data) { @@ -86,6 +97,16 @@ export async function fetchStatusContext(domain, id, token) { return resp.json(); } +export async function favouriteStatus(domain, id, token) { + const resp = await post(`https://${domain}/api/v1/statuses/${id}/favourite`, token); + return resp.json(); +} + +export async function unfavouriteStatus(domain, id, token) { + const resp = await post(`https://${domain}/api/v1/statuses/${id}/unfavourite`, token); + return resp.json(); +} + export async function fetchFollowing(domain, id, token) { const resp = await get(`https://${domain}/api/v1/accounts/${id}/following`, token); return resp.json();