1- import React , { useMemo } from 'react' ;
2- import { FlatList , type FlatListProps , StyleSheet , Text , View } from 'react-native' ;
1+ import React , { useCallback , useMemo , useState } from 'react' ;
2+ import { FlatList , type FlatListProps , Pressable , StyleSheet , Text , View } from 'react-native' ;
33
44import { PollAnswer , VotingVisibility } from 'stream-chat' ;
55
6- import { AnswerListAddCommentButton } from './Button' ;
6+ import { PollButtonProps } from './Button' ;
7+ import { PollInputDialog } from './PollInputDialog' ;
78
89import {
910 PollContextProvider ,
1011 PollContextValue ,
12+ usePollContext ,
1113 useTheme ,
1214 useTranslationContext ,
1315} from '../../../contexts' ;
@@ -17,6 +19,60 @@ import { Avatar } from '../../Avatar/Avatar';
1719import { usePollAnswersPagination } from '../hooks/usePollAnswersPagination' ;
1820import { usePollState } from '../hooks/usePollState' ;
1921
22+ export const AnswerListAddCommentButton = ( props : PollButtonProps ) => {
23+ const { t } = useTranslationContext ( ) ;
24+ const { message, poll } = usePollContext ( ) ;
25+ const { addComment, ownAnswer } = usePollState ( ) ;
26+ const [ showAddCommentDialog , setShowAddCommentDialog ] = useState ( false ) ;
27+ const { onPress } = props ;
28+
29+ const onPressHandler = useCallback ( ( ) => {
30+ if ( onPress ) {
31+ onPress ( { message, poll } ) ;
32+ return ;
33+ }
34+
35+ setShowAddCommentDialog ( true ) ;
36+ } , [ message , onPress , poll ] ) ;
37+
38+ const {
39+ theme : {
40+ colors : { accent_dark_blue, bg_user } ,
41+ poll : {
42+ answersList : { buttonContainer } ,
43+ button : { text } ,
44+ } ,
45+ } ,
46+ } = useTheme ( ) ;
47+
48+ return (
49+ < >
50+ < Pressable
51+ onPress = { onPressHandler }
52+ style = { ( { pressed } ) => [
53+ { opacity : pressed ? 0.5 : 1 } ,
54+ styles . addCommentButtonContainer ,
55+ { backgroundColor : bg_user } ,
56+ buttonContainer ,
57+ ] }
58+ >
59+ < Text style = { [ styles . addCommentButtonText , { color : accent_dark_blue } , text ] } >
60+ { ownAnswer ? t < string > ( 'Update your comment' ) : t < string > ( 'Add a comment' ) }
61+ </ Text >
62+ </ Pressable >
63+ { showAddCommentDialog ? (
64+ < PollInputDialog
65+ closeDialog = { ( ) => setShowAddCommentDialog ( false ) }
66+ initialValue = { ownAnswer ?. answer_text ?? '' }
67+ onSubmit = { addComment }
68+ title = { t < string > ( 'Add a comment' ) }
69+ visible = { showAddCommentDialog }
70+ />
71+ ) : null }
72+ </ >
73+ ) ;
74+ } ;
75+
2076export type PollAnswersListProps <
2177 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
2278> = PollContextValue & {
@@ -120,6 +176,13 @@ export const PollAnswersList = ({
120176) ;
121177
122178const styles = StyleSheet . create ( {
179+ addCommentButtonContainer : {
180+ alignItems : 'center' ,
181+ borderRadius : 12 ,
182+ paddingHorizontal : 16 ,
183+ paddingVertical : 18 ,
184+ } ,
185+ addCommentButtonText : { fontSize : 16 } ,
123186 container : { flex : 1 , margin : 16 } ,
124187 listItemAnswerText : { fontSize : 16 , fontWeight : '500' } ,
125188 listItemContainer : {
0 commit comments