From 78529a00feb9b018db6fe9827053adb5f2b54958 Mon Sep 17 00:00:00 2001 From: Gabor Szita Date: Thu, 18 Dec 2025 10:22:07 +0100 Subject: [PATCH] Allow creating a poll when creating a post --- src/screens/WriterScreen.jsx | 111 ++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-) diff --git a/src/screens/WriterScreen.jsx b/src/screens/WriterScreen.jsx index 5e14877..920a05f 100644 --- a/src/screens/WriterScreen.jsx +++ b/src/screens/WriterScreen.jsx @@ -13,6 +13,8 @@ import { Tooltip, IconButton, ActivityIndicator, + Button, + Divider, } from 'react-native-paper'; import { AppContext } from '../App'; import { launchImageLibrary } from 'react-native-image-picker'; @@ -38,6 +40,27 @@ function WriterScreen({ navigation, route }) { const [repost, setRepost] = React.useState( /** @type {SidechatPostOrComment} */(null), ); + const [isPoll, setIsPoll] = React.useState(false); + const [pollOptions, setPollOptions] = React.useState(['', '']); + + const addPollOption = () => { + if (pollOptions.length < 4) { + setPollOptions([...pollOptions, '']); + } + }; + + const removePollOption = (index) => { + if (pollOptions.length > 1) { + setPollOptions(pollOptions.filter((_, i) => i !== index)); + } + }; + + const updatePollOption = (index, text) => { + const newOptions = [...pollOptions]; + newOptions[index] = text; + setPollOptions(newOptions); + }; + React.useEffect(() => { if (repostID) { API.getPost(repostID).then((p) => { @@ -47,8 +70,13 @@ function WriterScreen({ navigation, route }) { }); } }, [repostID]) + + const isPollValid = !isPoll || pollOptions.every(opt => opt.trim().length > 0); + const createPostOrComment = async () => { if (mode == 'post') { + const validPollOptions = isPoll ? pollOptions : undefined; + const p = await API.createPost( textContent, groupID, @@ -56,7 +84,8 @@ function WriterScreen({ navigation, route }) { null, null, anonMode, - repostID + repostID, + validPollOptions, ); if (!p?.message) { setPostSortMethod('recent'); @@ -137,7 +166,7 @@ function WriterScreen({ navigation, route }) { @@ -175,6 +204,48 @@ function WriterScreen({ navigation, route }) { {textContent.length} / 256 chars + {mode === 'post' && isPoll && ( + + + + + Poll Options + + {pollOptions.map((option, index) => ( + + updatePollOption(index, text)} + maxLength={80} + /> + {pollOptions.length > 2 && ( + removePollOption(index)} + /> + )} + + ))} + {pollOptions.length < 4 && ( + + )} + + )} {repost && @@ -263,6 +334,42 @@ function WriterScreen({ navigation, route }) { )} + {mode === 'post' && ( + { + setIsPoll(!isPoll); + if (!isPoll) { + setPollOptions(['', '']); + } + }} + style={{ borderRadius: BORDER_RADIUS, marginLeft: 15 }} + borderless={true}> + + + + {'Create Poll'} + + + + )} setError(false)}>