Skip to content

Commit

Permalink
fix: add localstate handling for vote for polls and quiz on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
hdz-666 committed Dec 9, 2024
1 parent d56ae09 commit 3d09fe3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@100mslive/react-sdk';
import { CheckCircleIcon, ChevronDownIcon, CrossCircleIcon } from '@100mslive/react-icons';
import { Box, Button, Flex, Text } from '../../../../';
import { isNotNullish } from '../../../../Stats/Stats';
import { checkCorrectAnswer } from '../../../common/utils';
import { MultipleChoiceOptions } from '../common/MultipleChoiceOptions';
import { SingleChoiceOptions } from '../common/SingleChoiceOptions';
Expand All @@ -30,6 +31,7 @@ export const QuestionCard = ({
answer,
localPeerResponse,
updateSavedResponses,
updateUnsavedResponses,
rolesThatCanViewResponses,
}) => {
const actions = useHMSActions();
Expand All @@ -55,7 +57,6 @@ export const QuestionCard = ({
const canRespond = isLive && !localPeerChoice;
const startTime = useRef(Date.now());
const isCorrectAnswer = checkCorrectAnswer(answer, localPeerChoice, type);

const [singleOptionAnswer, setSingleOptionAnswer] = useState();
const [multipleOptionAnswer, setMultipleOptionAnswer] = useState(new Set());
const [showOptions, setShowOptions] = useState(true);
Expand All @@ -80,7 +81,15 @@ export const QuestionCard = ({
options: Array.from(multipleOptionAnswer),
duration: Date.now() - startTime.current,
};
await actions.interactivityCenter.addResponsesToPoll(pollID, [submittedResponse]);
if (roomState === HMSRoomState.Connected) {
await actions.interactivityCenter.addResponsesToPoll(pollID, [submittedResponse]);
} else {
updateUnsavedResponses(prev => {
const prevCopy = { ...prev };
prevCopy[index] = { pollID: pollID, ...submittedResponse };
return prevCopy;
});
}
updateSavedResponses(prev => {
const prevCopy = { ...prev };
prevCopy[index] = { option: singleOptionAnswer, options: Array.from(multipleOptionAnswer) };
Expand All @@ -92,9 +101,11 @@ export const QuestionCard = ({
index,
singleOptionAnswer,
multipleOptionAnswer,
roomState,
updateSavedResponses,
actions.interactivityCenter,
pollID,
updateSavedResponses,
updateUnsavedResponses,
]);

return (
Expand Down Expand Up @@ -191,7 +202,9 @@ export const QuestionCard = ({
</Box>
{isLive && (
<QuestionActions
disableVote={roomState !== HMSRoomState.Connected}
disableVote={
isNotNullish(localPeerResponse?.[index]?.option) || localPeerResponse?.[index]?.options.length > 0
}
isValidVote={isValidVote}
onVote={handleVote}
response={localPeerChoice}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const StandardView = ({
poll,
localPeerResponses,
updateSavedResponses,
updateUnsavedResponses,
}: {
poll: HMSPoll;
localPeerResponses: Record<number, number | number[] | undefined>;
updateSavedResponses: Dispatch<SetStateAction<Record<any, any>>>;
updateUnsavedResponses: Dispatch<SetStateAction<Record<any, any>>>;
}) => {
if (!poll?.questions) {
return null;
Expand All @@ -25,6 +27,7 @@ export const StandardView = ({
{isQuiz && isStopped ? <PeerParticipationSummary quiz={poll} /> : null}
{poll.questions?.map((question, index) => (
<QuestionCard
updateUnsavedResponses={updateUnsavedResponses}
pollID={poll.id}
isQuiz={isQuiz}
startedBy={poll.startedBy}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export const TimedView = ({
poll,
localPeerResponses,
updateSavedResponses,
updateUnsavedResponses,
}: {
poll: HMSPoll;
localPeerResponses?: Record<number, number | number[] | undefined>;
updateSavedResponses: Dispatch<SetStateAction<Record<any, any>>>;
updateUnsavedResponses: Dispatch<SetStateAction<Record<any, any>>>;
}) => {
const [currentIndex, setCurrentIndex] = useState(getIndexToShow(localPeerResponses));
const activeQuestion = poll.questions?.find(question => question.index === currentIndex);
Expand All @@ -32,6 +34,7 @@ export const TimedView = ({
{poll.questions.map(question => {
return attemptedAll || activeQuestion?.index === question.index ? (
<QuestionCard
updateUnsavedResponses={updateUnsavedResponses}
key={question.index}
pollID={poll.id}
isQuiz={poll.type === 'quiz'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useEffect, useRef, useState } from 'react';
import {
HMSRoomState,
selectLocalPeer,
selectPeerNameByID,
selectPermissions,
selectPollByID,
selectRoomState,
useHMSActions,
useHMSStore,
} from '@100mslive/react-sdk';
Expand Down Expand Up @@ -31,16 +33,30 @@ export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => v
const showSingleView = poll?.type === 'quiz' && poll.state === 'started';
const fetchedInitialResponses = useRef(false);
const [savedResponses, setSavedResponses] = useState<Record<any, any>>({});
const [unsavedResponses, setUnsavedResponses] = useState<Record<any, any>>({});
const localPeer = useHMSStore(selectLocalPeer);
const localPeerId = localPeer?.id;
const customerUserId = localPeer?.customerUserId;

const roomState = useHMSStore(selectRoomState);
// To reset whenever a different poll is opened
useEffect(() => {
fetchedInitialResponses.current = false;
setSavedResponses({});
}, [id, setSavedResponses]);

// To send whenever room is connected back
useEffect(() => {
const handleDisconnectedVote = async () => {
if (unsavedResponses && Object.keys(unsavedResponses).length > 0 && roomState === HMSRoomState.Connected) {
for (const key in unsavedResponses) {
await actions.interactivityCenter.addResponsesToPoll(unsavedResponses[key].pollID, [unsavedResponses[key]]);
}
setUnsavedResponses({});
}
};
handleDisconnectedVote();
}, [actions.interactivityCenter, roomState, setUnsavedResponses, unsavedResponses]);

useEffect(() => {
const getResponses = async () => {
if (poll && actions.interactivityCenter && !fetchedInitialResponses.current) {
Expand Down Expand Up @@ -115,9 +131,19 @@ export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => v
) : null}

{showSingleView ? (
<TimedView poll={poll} localPeerResponses={savedResponses} updateSavedResponses={setSavedResponses} />
<TimedView
updateUnsavedResponses={setUnsavedResponses}
poll={poll}
localPeerResponses={savedResponses}
updateSavedResponses={setSavedResponses}
/>
) : (
<StandardView poll={poll} localPeerResponses={savedResponses} updateSavedResponses={setSavedResponses} />
<StandardView
updateUnsavedResponses={setUnsavedResponses}
poll={poll}
localPeerResponses={savedResponses}
updateSavedResponses={setSavedResponses}
/>
)}
</Flex>
<Flex
Expand Down

0 comments on commit 3d09fe3

Please sign in to comment.