Skip to content

Commit

Permalink
fix: poll result was not updating
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Oct 16, 2024
1 parent 56cea2c commit c94cae1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/components/polls/voteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { Dialog, Transition } from '@headlessui/react';
import { useTranslation } from 'react-i18next';
import { toast } from 'react-toastify';
import { create } from '@bufbuild/protobuf';
import { SubmitPollResponseReqSchema } from 'plugnmeet-protocol-js';
import {
DataMsgBodyType,
SubmitPollResponseReqSchema,
} from 'plugnmeet-protocol-js';

import {
useAddResponseMutation,
useGetPollListsQuery,
} from '../../store/services/pollsApi';
import { store } from '../../store';
import { getNatsConn } from '../../helpers/nats';

interface IVoteFormProps {
onCloseForm(): void;
Expand All @@ -20,6 +24,7 @@ const VoteForm = ({ onCloseForm, pollId }: IVoteFormProps) => {
const { t } = useTranslation();
const [isOpen, setIsOpen] = useState<boolean>(true);
const [selectedOption, setSelectedOption] = useState<number>(0);
const conn = getNatsConn();

const [addResponse, { isLoading, data }] = useAddResponseMutation();
const { post: poll } = useGetPollListsQuery(undefined, {
Expand Down Expand Up @@ -49,7 +54,7 @@ const VoteForm = ({ onCloseForm, pollId }: IVoteFormProps) => {
onCloseForm();
};

const onSubmit = (e) => {
const onSubmit = (e: any) => {
e.preventDefault();
if (selectedOption === 0) {
return;
Expand All @@ -62,6 +67,9 @@ const VoteForm = ({ onCloseForm, pollId }: IVoteFormProps) => {
selectedOption: `${selectedOption}`,
}),
);

// notify to everyone
conn.sendDataMessage(DataMsgBodyType.NEW_POLL_RESPONSE, pollId);
};

const renderForm = () => {
Expand Down
17 changes: 16 additions & 1 deletion src/helpers/nats/HandleDataMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,22 @@ export default class HandleDataMessage {
if (payload.fromUserId === this._that.userId) {
return;
}
store.dispatch(pollsApi.util.invalidateTags(['List', 'PollsStats']));
store.dispatch(
pollsApi.util.invalidateTags([
{
type: 'Count',
id: payload.message,
},
{
type: 'Selected',
id: payload.message,
},
{
type: 'PollDetails',
id: payload.message,
},
]),
);
break;
case DataMsgBodyType.SPEECH_SUBTITLE_TEXT:
this.handleSpeechSubtitleText(payload.message);
Expand Down
23 changes: 22 additions & 1 deletion src/helpers/nats/HandleSystemData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,28 @@ export default class HandleSystemData {
store.dispatch(pollsApi.util.invalidateTags(['List', 'PollsStats']));
break;
case NatsMsgServerToClientEvents.POLL_CLOSED:
store.dispatch(pollsApi.util.invalidateTags(['List', 'PollsStats']));
store.dispatch(
pollsApi.util.invalidateTags([
'List',
'PollsStats',
{
type: 'Count',
id: payload.msg,
},
{
type: 'Selected',
id: payload.msg,
},
{
type: 'PollResult',
id: payload.msg,
},
{
type: 'PollDetails',
id: payload.msg,
},
]),
);
break;
}
};
Expand Down

0 comments on commit c94cae1

Please sign in to comment.