Skip to content

Commit 3992fcc

Browse files
committed
fix: move vote button to correct file too
1 parent ca873e5 commit 3992fcc

File tree

2 files changed

+76
-77
lines changed

2 files changed

+76
-77
lines changed
Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import React, { useCallback } from 'react';
1+
import React from 'react';
22
import { Pressable, StyleSheet, Text } from 'react-native';
33

44
import { Poll, PollOption } from 'stream-chat';
55

6-
import { useOwnCapabilitiesContext, usePollContext, useTheme } from '../../../contexts';
7-
import { Check } from '../../../icons';
6+
import { useTheme } from '../../../contexts';
87
import type { DefaultStreamChatGenerics } from '../../../types/types';
98
import { MessageType } from '../../MessageList/hooks/useMessageList';
10-
import { usePollState } from '../hooks/usePollState';
119

1210
export type PollButtonProps<
1311
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -45,80 +43,11 @@ export const GenericPollButton = ({ onPress, title }: { onPress?: () => void; ti
4543
);
4644
};
4745

48-
export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
49-
const { message, poll } = usePollContext();
50-
const { isClosed, ownVotesByOptionId } = usePollState();
51-
const ownCapabilities = useOwnCapabilitiesContext();
52-
53-
const {
54-
theme: {
55-
colors: { accent_dark_blue, disabled },
56-
poll: {
57-
message: {
58-
option: { voteButtonActive, voteButtonContainer, voteButtonInactive },
59-
},
60-
},
61-
},
62-
} = useTheme();
63-
64-
const toggleVote = useCallback(async () => {
65-
if (ownVotesByOptionId[option.id]) {
66-
await poll.removeVote(ownVotesByOptionId[option.id]?.id, message.id);
67-
} else {
68-
await poll.castVote(option.id, message.id);
69-
}
70-
}, [message.id, option.id, ownVotesByOptionId, poll]);
71-
72-
const onPressHandler = useCallback(() => {
73-
if (onPress) {
74-
onPress({ message, poll });
75-
return;
76-
}
77-
78-
toggleVote();
79-
}, [message, onPress, poll, toggleVote]);
80-
81-
return ownCapabilities.castPollVote && !isClosed ? (
82-
<Pressable
83-
onPress={onPressHandler}
84-
style={({ pressed }) => [
85-
{ opacity: pressed ? 0.5 : 1 },
86-
styles.voteContainer,
87-
{
88-
backgroundColor: ownVotesByOptionId[option.id]
89-
? voteButtonActive || accent_dark_blue
90-
: 'transparent',
91-
borderColor: ownVotesByOptionId[option.id]
92-
? voteButtonActive || accent_dark_blue
93-
: voteButtonInactive || disabled,
94-
},
95-
voteButtonContainer,
96-
]}
97-
>
98-
{ownVotesByOptionId[option.id] ? <Check height={15} pathFill='white' width={20} /> : null}
99-
</Pressable>
100-
) : null;
101-
};
102-
10346
const styles = StyleSheet.create({
104-
answerListAddCommentContainer: {
105-
alignItems: 'center',
106-
borderRadius: 12,
107-
paddingHorizontal: 16,
108-
paddingVertical: 18,
109-
},
11047
container: {
11148
alignItems: 'center',
11249
marginHorizontal: 16,
11350
paddingVertical: 11,
11451
},
11552
text: { fontSize: 16 },
116-
voteContainer: {
117-
alignItems: 'center',
118-
borderRadius: 18,
119-
borderWidth: 1,
120-
height: 18,
121-
justifyContent: 'center',
122-
width: 18,
123-
},
12453
});

package/src/components/Poll/components/PollOption.tsx

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useCallback, useMemo } from 'react';
22

3-
import { ScrollViewProps, StyleSheet, Text, View } from 'react-native';
3+
import { Pressable, ScrollViewProps, StyleSheet, Text, View } from 'react-native';
44

55
import { ScrollView } from 'react-native-gesture-handler';
66

77
import { PollOption as PollOptionClass, PollVote } from 'stream-chat';
88

9-
import { VoteButton } from './Button';
9+
import { PollVoteButtonProps } from './Button';
1010

11-
import { PollContextProvider, PollContextValue, useTheme } from '../../../contexts';
11+
import {
12+
PollContextProvider,
13+
PollContextValue,
14+
useOwnCapabilitiesContext,
15+
usePollContext,
16+
useTheme,
17+
} from '../../../contexts';
1218

19+
import { Check } from '../../../icons';
1320
import { Avatar } from '../../Avatar/Avatar';
1421
import { usePollState } from '../hooks/usePollState';
1522

@@ -141,6 +148,61 @@ export const PollOption = ({ option, showProgressBar = true }: PollOptionProps)
141148
);
142149
};
143150

151+
export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
152+
const { message, poll } = usePollContext();
153+
const { isClosed, ownVotesByOptionId } = usePollState();
154+
const ownCapabilities = useOwnCapabilitiesContext();
155+
156+
const {
157+
theme: {
158+
colors: { accent_dark_blue, disabled },
159+
poll: {
160+
message: {
161+
option: { voteButtonActive, voteButtonContainer, voteButtonInactive },
162+
},
163+
},
164+
},
165+
} = useTheme();
166+
167+
const toggleVote = useCallback(async () => {
168+
if (ownVotesByOptionId[option.id]) {
169+
await poll.removeVote(ownVotesByOptionId[option.id]?.id, message.id);
170+
} else {
171+
await poll.castVote(option.id, message.id);
172+
}
173+
}, [message.id, option.id, ownVotesByOptionId, poll]);
174+
175+
const onPressHandler = useCallback(() => {
176+
if (onPress) {
177+
onPress({ message, poll });
178+
return;
179+
}
180+
181+
toggleVote();
182+
}, [message, onPress, poll, toggleVote]);
183+
184+
return ownCapabilities.castPollVote && !isClosed ? (
185+
<Pressable
186+
onPress={onPressHandler}
187+
style={({ pressed }) => [
188+
{ opacity: pressed ? 0.5 : 1 },
189+
styles.voteContainer,
190+
{
191+
backgroundColor: ownVotesByOptionId[option.id]
192+
? voteButtonActive || accent_dark_blue
193+
: 'transparent',
194+
borderColor: ownVotesByOptionId[option.id]
195+
? voteButtonActive || accent_dark_blue
196+
: voteButtonInactive || disabled,
197+
},
198+
voteButtonContainer,
199+
]}
200+
>
201+
{ownVotesByOptionId[option.id] ? <Check height={15} pathFill='white' width={20} /> : null}
202+
</Pressable>
203+
) : null;
204+
};
205+
144206
const styles = StyleSheet.create({
145207
allOptionsListContainer: {
146208
borderRadius: 12,
@@ -162,6 +224,14 @@ const styles = StyleSheet.create({
162224
fontSize: 16,
163225
marginLeft: 4,
164226
},
227+
voteContainer: {
228+
alignItems: 'center',
229+
borderRadius: 18,
230+
borderWidth: 1,
231+
height: 18,
232+
justifyContent: 'center',
233+
width: 18,
234+
},
165235
votesContainer: { flexDirection: 'row', marginLeft: 4 },
166236
wrapper: { marginTop: 8, paddingVertical: 8 },
167237
});

0 commit comments

Comments
 (0)