Skip to content

Commit 39d9345

Browse files
authored
fix: poll switch inputs issue with async state (#3135)
* fix: poll switch inputs issue with async state * fix: poll switch inputs issue with async state
1 parent 1783a3e commit 39d9345

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

package/src/components/Poll/CreatePollContent.tsx

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,21 @@ import { useMessageComposer } from '../../contexts/messageInputContext/hooks/use
2424
import { useStateStore } from '../../hooks/useStateStore';
2525

2626
const pollComposerStateSelector = (state: PollComposerState) => ({
27-
allow_answers: state.data.allow_answers,
28-
allow_user_suggested_options: state.data.allow_user_suggested_options,
29-
enforce_unique_vote: state.data.enforce_unique_vote,
30-
max_votes_allowed: state.data.max_votes_allowed,
31-
name: state.data.name,
3227
options: state.data.options,
33-
voting_visibility: state.data.voting_visibility,
3428
});
3529

3630
export const POLL_OPTION_HEIGHT = 71;
3731

3832
export const CreatePollContent = () => {
33+
const [isAnonymousPoll, setIsAnonymousPoll] = React.useState<boolean>(false);
34+
const [allowUserSuggestedOptions, setAllowUserSuggestedOptions] = React.useState<boolean>(false);
35+
const [allowAnswers, setAllowAnswers] = React.useState<boolean>(false);
36+
3937
const { t } = useTranslationContext();
4038

4139
const messageComposer = useMessageComposer();
4240
const { pollComposer } = messageComposer;
43-
const { allow_answers, allow_user_suggested_options, options, voting_visibility } = useStateStore(
44-
pollComposer.state,
45-
pollComposerStateSelector,
46-
);
41+
const { options } = useStateStore(pollComposer.state, pollComposerStateSelector);
4742

4843
const { createPollOptionHeight, closePollCreationDialog, createAndSendPoll } =
4944
useCreatePollContentContext();
@@ -89,6 +84,32 @@ export const CreatePollContent = () => {
8984
await createAndSendPoll();
9085
}, [createAndSendPoll]);
9186

87+
const onAnonymousPollChangeHandler = useCallback(
88+
async (value: boolean) => {
89+
setIsAnonymousPoll(value);
90+
await pollComposer.updateFields({
91+
voting_visibility: value ? VotingVisibility.anonymous : VotingVisibility.public,
92+
});
93+
},
94+
[pollComposer],
95+
);
96+
97+
const onAllowUserSuggestedOptionsChangeHandler = useCallback(
98+
async (value: boolean) => {
99+
setAllowUserSuggestedOptions(value);
100+
await pollComposer.updateFields({ allow_user_suggested_options: value });
101+
},
102+
[pollComposer],
103+
);
104+
105+
const onAllowAnswersChangeHandler = useCallback(
106+
async (value: boolean) => {
107+
setAllowAnswers(value);
108+
await pollComposer.updateFields({ allow_answers: value });
109+
},
110+
[pollComposer],
111+
);
112+
92113
return (
93114
<>
94115
<CreatePollHeader
@@ -108,14 +129,7 @@ export const CreatePollContent = () => {
108129
<Text style={[styles.text, { color: black }, anonymousPoll.title]}>
109130
{t<string>('Anonymous poll')}
110131
</Text>
111-
<Switch
112-
onValueChange={(value) =>
113-
pollComposer.updateFields({
114-
voting_visibility: value ? VotingVisibility.anonymous : VotingVisibility.public,
115-
})
116-
}
117-
value={voting_visibility === VotingVisibility.anonymous}
118-
/>
132+
<Switch onValueChange={onAnonymousPollChangeHandler} value={isAnonymousPoll} />
119133
</View>
120134
<View
121135
style={[styles.textInputWrapper, { backgroundColor: bg_user }, suggestOption.wrapper]}
@@ -124,20 +138,15 @@ export const CreatePollContent = () => {
124138
{t<string>('Suggest an option')}
125139
</Text>
126140
<Switch
127-
onValueChange={(value) =>
128-
pollComposer.updateFields({ allow_user_suggested_options: value })
129-
}
130-
value={allow_user_suggested_options}
141+
onValueChange={onAllowUserSuggestedOptionsChangeHandler}
142+
value={allowUserSuggestedOptions}
131143
/>
132144
</View>
133145
<View style={[styles.textInputWrapper, { backgroundColor: bg_user }, addComment.wrapper]}>
134146
<Text style={[styles.text, { color: black }, addComment.title]}>
135147
{t<string>('Add a comment')}
136148
</Text>
137-
<Switch
138-
onValueChange={(value) => pollComposer.updateFields({ allow_answers: value })}
139-
value={allow_answers}
140-
/>
149+
<Switch onValueChange={onAllowAnswersChangeHandler} value={allowAnswers} />
141150
</View>
142151
</ScrollView>
143152
</>

package/src/components/Poll/components/MultipleAnswersField.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ import { useMessageComposer } from '../../../contexts/messageInputContext/hooks/
88
import { useStateStore } from '../../../hooks/useStateStore';
99

1010
const pollComposerStateSelector = (state: PollComposerState) => ({
11-
enforce_unique_vote: state.data.enforce_unique_vote,
1211
error: state.errors.max_votes_allowed,
1312
max_votes_allowed: state.data.max_votes_allowed,
1413
});
1514

1615
export const MultipleAnswersField = () => {
16+
const [allowMultipleVotes, setAllowMultipleVotes] = React.useState<boolean>(false);
1717
const { t } = useTranslationContext();
1818
const messageComposer = useMessageComposer();
1919
const { pollComposer } = messageComposer;
2020
const { handleFieldBlur, updateFields } = pollComposer;
21-
const { enforce_unique_vote, error, max_votes_allowed } = useStateStore(
22-
pollComposer.state,
23-
pollComposerStateSelector,
24-
);
21+
const { error, max_votes_allowed } = useStateStore(pollComposer.state, pollComposerStateSelector);
2522

2623
const {
2724
theme: {
@@ -34,6 +31,7 @@ export const MultipleAnswersField = () => {
3431

3532
const onEnforceUniqueVoteHandler = useCallback(
3633
async (value: boolean) => {
34+
setAllowMultipleVotes(value);
3735
await updateFields({ enforce_unique_vote: !value });
3836
},
3937
[updateFields],
@@ -58,9 +56,9 @@ export const MultipleAnswersField = () => {
5856
<Text style={[styles.text, { color: black }, multipleAnswers.title]}>
5957
{t<string>('Multiple answers')}
6058
</Text>
61-
<Switch onValueChange={onEnforceUniqueVoteHandler} value={!enforce_unique_vote} />
59+
<Switch onValueChange={onEnforceUniqueVoteHandler} value={allowMultipleVotes} />
6260
</View>
63-
{!enforce_unique_vote ? (
61+
{allowMultipleVotes ? (
6462
<View style={[styles.maxVotesWrapper, maxVotes.wrapper]}>
6563
{max_votes_allowed && error ? (
6664
<Text

0 commit comments

Comments
 (0)