From 3d22ed71c770001b38aab2227bc928ca1a636119 Mon Sep 17 00:00:00 2001 From: natjms Date: Sat, 22 May 2021 16:37:44 -0300 Subject: [PATCH] Enable commenting on posts and replies to posts Pixelfed seems to only return first-level descendants from its context endpoint, thus after replies-to-replies are submitted, they are not rendered. See #23 --- src/components/pages/profile.js | 1 + src/components/pages/view-comments.js | 151 +++++++++++++++++++++----- src/requests.js | 5 + 3 files changed, 131 insertions(+), 26 deletions(-) diff --git a/src/components/pages/profile.js b/src/components/pages/profile.js index 05a2dbb..de2a0e4 100644 --- a/src/components/pages/profile.js +++ b/src/components/pages/profile.js @@ -132,6 +132,7 @@ const ViewProfileJsx = ({navigation}) => { active = { navigation.getParam("originTab") } navigation = { navigation }> { } - + Reply - - + + - + @@ -221,7 +228,7 @@ const ViewCommentsJsx = (props) => { let [state, setState] = useState({ postData: props.navigation.getParam("postData", null), loaded: false, - reply: "" + reply: "", }); useEffect(() => { @@ -245,11 +252,66 @@ const ViewCommentsJsx = (props) => { profile, instance, accessToken, + inReplyTo: { + acct: state.postData.account.acct, + id: state.postData.id, + }, loaded: true, }); }); }, []); + const _onReplyFactory = (acct, id) => { + return () => { + setState({...state, + inReplyTo: { + acct, + id, + }, + }); + } + }; + + const _handleCancelSubReply = () => { + setState({...state, + inReplyTo: { + acct: state.postData.account.acct, + id: state.postData.id, + }, + }); + }; + + const _handleSubmitReply = async () => { + if(state.reply.length > 0) { + await requests.publishStatus( + state.instance, + state.accessToken, + { + status: state.reply, + in_reply_to_id: state.inReplyTo.id, + } + ); + + // Fetch the updated context to rerender the page + const newContext = await requests.fetchStatusContext( + state.instance, + state.postData.id, + state.accessToken, + ); + + setState({...state, + descendants: threadify(newContext.descendants), + + //Reset the comment form + inReplyTo: { + acct: state.postData.account.acct, + id: state.postData.id, + }, + reply: "", + }); + } + }; + return ( <> { state.loaded ? @@ -261,6 +323,7 @@ const ViewCommentsJsx = (props) => { ? @@ -270,7 +333,9 @@ const ViewCommentsJsx = (props) => { const subs = thread.slice(1); return ( - + { subs.map((sub, j) => { return ( @@ -278,6 +343,7 @@ const ViewCommentsJsx = (props) => { key = { j } style = { styles.sub }> ) @@ -297,21 +363,39 @@ const ViewCommentsJsx = (props) => { : <> } - - - setState({...state, reply: c }) }/> - - - - + + <> + { state.inReplyTo.id != state.postData.id + ? + + + +  Replying to  + + { state.inReplyTo.acct } + ... + + + + : <> + } + + + + setState({...state, reply: c }) }/> + + + + + @@ -366,13 +450,28 @@ const styles = { height: 15, }, + form: { + container: { + backgroundColor: "white", + + borderTopWidth: 1, + borderTopColor: "#CCC", + }, + inReplyTo: { + container: { + padding: 10, + flexDirection: "row", + alignItems: "center", + }, + message: { + color: "#666", + }, + }, + }, + commentForm: { flexDirection: "row", alignItems: "center", - backgroundColor: "white", - - borderTopWidth: 1, - borderTopColor: "#CCC", paddingTop: 10, paddingBottom: 10, diff --git a/src/requests.js b/src/requests.js index 3f949e4..75d06e1 100644 --- a/src/requests.js +++ b/src/requests.js @@ -92,6 +92,11 @@ export async function fetchAccountStatuses(domain, id, token) { return resp.json(); } +export async function publishStatus(domain, token, params) { + const resp = await postForm(`https://${domain}/api/v1/statuses`, params, token); + return resp.json(); +} + export async function fetchStatusContext(domain, id, token) { const resp = await get(`https://${domain}/api/v1/statuses/${id}/context`, token); return resp.json();