Enable favouriting statuses

This commit is contained in:
Nat 2021-05-22 11:26:10 -03:00
parent 05981e9711
commit 1cc44eae7f
3 changed files with 101 additions and 40 deletions

View File

@ -4,17 +4,17 @@ import {
Text, Text,
View, View,
Dimensions, Dimensions,
TouchableWithoutFeedback TouchableOpacity
} from "react-native"; } from "react-native";
import { activeOrNot } from "src/interface/interactions"; import { activeOrNot } from "src/interface/interactions";
const PostActionJsx = (props) => { const PostActionJsx = (props) => {
return ( return (
<TouchableWithoutFeedback <TouchableOpacity
onPress = { props.callback }> onPress = { props.onPress }>
<Image <Image
source = { source = {
activeOrNot(props.state[props.field], props.pack) activeOrNot(props.active, props.pack)
} }
style = { style = {
[ [
@ -22,18 +22,11 @@ const PostActionJsx = (props) => {
props.last ? styles.lastIcon : {} props.last ? styles.lastIcon : {}
] ]
} /> } />
</TouchableWithoutFeedback> </TouchableOpacity>
) )
} }
const PostActionBarJsx = (props) => { const PostActionBarJsx = (props) => {
let [state, setState] = useState({
favourited: props.favourited,
commenting: false,
reblogged: props.reblogged,
bookmarked: false
});
const icons = { const icons = {
heart: { heart: {
active: require("assets/eva-icons/post-actions/heart-active.png"), active: require("assets/eva-icons/post-actions/heart-active.png"),
@ -53,33 +46,21 @@ const PostActionBarJsx = (props) => {
<PostActionJsx <PostActionJsx
field = "favourited" field = "favourited"
pack = { icons.heart } pack = { icons.heart }
state = { state } active = { props.favourited }
callback = { onPress = { props.onFavourite } />
() => {
setState({ ...state, favourited: !state.favourited });
}
} />
<PostActionJsx <PostActionJsx
field = "reblogged" field = "reblogged"
pack = { icons.reblog } pack = { icons.reblog }
state = { state } reblogged = { props.reblogged }
callback = { onPress = { props.onReblog }/>
() => {
setState({ ...state, reblogged: !state.reblogged });
}
} />
<PostActionJsx <PostActionJsx
field = "bookmarked" field = "bookmarked"
pack = { icons.bookmark } pack = { icons.bookmark }
last = { true } last = { true }
state = { state } bookmarked = { props.bookmarked }
callback = { onPress = { props.onBookmark } />
() => {
setState({ ...state, bookmarked: !state.bookmarked });
}
} />
</View> </View>
) )
} }

View File

@ -8,7 +8,10 @@ import {
ScrollView, ScrollView,
} from "react-native"; } 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"; 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) => { const PostImageJsx = (props) => {
return <Image return <Image
source = { { uri: props.uri } } source = { { uri: props.uri } }
@ -54,7 +71,6 @@ const PostImageJsx = (props) => {
flex: 1, flex: 1,
width: SCREEN_WIDTH, width: SCREEN_WIDTH,
height: getAutoHeight(props.width, props.height, SCREEN_WIDTH), height: getAutoHeight(props.width, props.height, SCREEN_WIDTH),
// objectFit: "cover"
} }
} /> } />
}; };
@ -120,7 +136,10 @@ export const RawPostJsx = (props) => {
} }
<PostActionBarJsx <PostActionBarJsx
favourited = { props.data.favourited } favourited = { props.data.favourited }
reblogged = { props.data.reblogged } /> reblogged = { props.data.reblogged }
bookmarked = { false }
onFavourite = { props.onFavourite }
onBookmark = { props.onBookmark } />
<View style = { styles.caption }> <View style = { styles.caption }>
<Text> <Text>
<Text style = { styles.strong }> <Text style = { styles.strong }>
@ -156,25 +175,65 @@ export const PostByDataJsx = (props) => {
let [state, setState] = useState({ let [state, setState] = useState({
loaded: false, loaded: false,
data: props.data,
dimensions: [] dimensions: []
}); });
useEffect(() => { useEffect(() => {
Promise.all(getDimensionsPromises(props.data.media_attachments)) let instance, accessToken;
.then(dimensions => { AsyncStorage
setState({ .multiGet([
dimensions: dimensions, "@user_instance",
loaded: true "@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 ( return (
<View> <View>
{ state.loaded ? { state.loaded ?
<RawPostJsx <RawPostJsx
data = { props.data } data = { state.data }
dimensions = { state.dimensions } dimensions = { state.dimensions }
onFavourite = { _handleFavourite }
navigation = { props.navigation }/> navigation = { props.navigation }/>
: <View></View> } : <View></View> }
</View> </View>

View File

@ -52,6 +52,17 @@ export async function postForm(url, data, token = false) {
return resp; 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) { export async function get(url, token = false, data = false) {
let completeURL; let completeURL;
if (data) { if (data) {
@ -86,6 +97,16 @@ export async function fetchStatusContext(domain, id, token) {
return resp.json(); 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) { export async function fetchFollowing(domain, id, token) {
const resp = await get(`https://${domain}/api/v1/accounts/${id}/following`, token); const resp = await get(`https://${domain}/api/v1/accounts/${id}/following`, token);
return resp.json(); return resp.json();