Skip to content

Commit

Permalink
fix: simplify state mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
KaustubhKumar05 committed May 21, 2024
1 parent 0cd3a6e commit 77d0730
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
1 change: 0 additions & 1 deletion packages/roomkit-react/src/Prebuilt/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const APP_DATA = {
sheet: 'sheet',
caption: 'caption',
loadingEffects: 'loadingEffects',
savedPollResponses: 'savedPollResponses',
};

export const UI_SETTINGS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const initialAppData = {
},
// by default off, so it will not appear in beam bots
[APP_DATA.caption]: false,
[APP_DATA.savedPollResponses]: {},
};

export const AppData = React.memo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export const QuestionCard = ({
duration: Date.now() - startTime.current,
};
await actions.interactivityCenter.addResponsesToPoll(pollID, [submittedResponse]);
updateSavedResponses(index, singleOptionAnswer, Array.from(multipleOptionAnswer));
updateSavedResponses(prev => {
const prevCopy = { ...prev };
prevCopy[index] = { option: singleOptionAnswer, options: Array.from(multipleOptionAnswer) };
return prevCopy;
});
startTime.current = Date.now();
}, [
isValidVote,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Dispatch, SetStateAction } from 'react';
import { HMSPoll } from '@100mslive/react-sdk';
import { PeerParticipationSummary } from './PeerParticipationSummary';
// @ts-ignore
Expand All @@ -11,7 +11,7 @@ export const StandardView = ({
}: {
poll: HMSPoll;
localPeerResponses: Record<number, number | number[] | undefined>;
updateSavedResponses: (questionIndex: number, option?: number, options?: number[]) => void;
updateSavedResponses: Dispatch<SetStateAction<Record<any, any>>>;
}) => {
if (!poll?.questions) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { Dispatch, SetStateAction, useEffect, useState } from 'react';
import { HMSPoll } from '@100mslive/react-sdk';
// @ts-ignore
import { QuestionCard } from './QuestionCard';
Expand All @@ -12,7 +12,7 @@ export const TimedView = ({
}: {
poll: HMSPoll;
localPeerResponses?: Record<number, number | number[] | undefined>;
updateSavedResponses: (questionIndex: number, option?: number, options?: number[]) => void;
updateSavedResponses: Dispatch<SetStateAction<Record<any, any>>>;
}) => {
const [currentIndex, setCurrentIndex] = useState(getIndexToShow(localPeerResponses));
const activeQuestion = poll.questions?.find(question => question.index === currentIndex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import {
selectLocalPeerID,
selectPeerNameByID,
Expand All @@ -14,11 +14,11 @@ import { Container } from '../../Streaming/Common';
import { StandardView } from './StandardVoting';
import { TimedView } from './TimedVoting';
// @ts-ignore
import { usePollViewState, useSetAppDataByKey } from '../../AppData/useUISettings';
import { usePollViewState } from '../../AppData/useUISettings';
// @ts-ignore
import { getPeerResponses } from '../../../common/utils';
import { StatusIndicator } from '../common/StatusIndicator';
import { APP_DATA, POLL_VIEWS } from '../../../common/constants';
import { POLL_VIEWS } from '../../../common/constants';

export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => void }) => {
const actions = useHMSActions();
Expand All @@ -30,14 +30,14 @@ export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => v
// Sets view - linear or vertical, toggles timer indicator
const showSingleView = poll?.type === 'quiz' && poll.state === 'started';
const fetchedInitialResponses = useRef(false);
const [savedPollResponses, setSavedPollResponses] = useSetAppDataByKey(APP_DATA.savedPollResponses);
const [savedResponses, setSavedResponses] = useState<Record<any, any>>({});
const localPeerId = useHMSStore(selectLocalPeerID);

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

useEffect(() => {
const getResponses = async () => {
Expand All @@ -47,28 +47,22 @@ export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => v
}
};
getResponses();
}, [poll, actions.interactivityCenter, setSavedPollResponses]);

const updateSavedResponses = (questionIndex: number, option?: number, options?: number[]) => {
if (!option && !options) {
return;
}
const savedPollResponsesCopy = { ...savedPollResponses };
savedPollResponsesCopy[questionIndex] = { option, options };
setSavedPollResponses(savedPollResponsesCopy);
};
}, [poll, actions.interactivityCenter]);

useEffect(() => {
if (poll?.questions) {
const localPeerResponses = getPeerResponses(poll.questions, localPeerId);
// @ts-ignore
localPeerResponses?.forEach(response => {
if (response) {
updateSavedResponses(response[0]?.questionIndex, response[0]?.option, response[0]?.options);
setSavedResponses(prev => {
const prevCopy = { ...prev };
prevCopy[response[0]?.questionIndex] = { option: response[0]?.option, options: response[0]?.options };
return prevCopy;
});
}
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [localPeerId, poll?.questions, id]);

if (!poll) {
Expand Down Expand Up @@ -119,13 +113,9 @@ export const Voting = ({ id, toggleVoting }: { id: string; toggleVoting: () => v
) : null}

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

0 comments on commit 77d0730

Please sign in to comment.