Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initially get votation data from websocket #188

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion backend/controllers/votation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,39 @@ export async function activateVotationStatus(
},
});

// Add votaton and options to an element for sending to participants.
const optionList: LimitedOptionType[] = [];

for (const optionID of vote.options) {
const id = optionID;
const option = await Option.findById(id);
if (option) {
const newOption: LimitedOptionType = {
_id: id,
title: option.title,
};
optionList.push(newOption);
}
}

const votationResponse: LimitedVoteResponseType = {
_id: voteId,
title: vote.title,
caseNumber: vote.caseNumber,
voteText: vote.voteText,
options: optionList,
};

// Notify all active participants to fetch the activated votation.
assembly.participants.forEach((member) => {
notifyOne(member, JSON.stringify({ status: "update", group: group }));
notifyOne(
member,
JSON.stringify({
status: "update",
group: group,
votation: votationResponse,
})
);
});

await Votation.findByIdAndUpdate(voteId, {
Expand Down
21 changes: 13 additions & 8 deletions frontend/src/components/VotationBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ import { getCurrentVotationByGroup, submitVote } from "../services/votation";
export function VotationBox(state: {
groupSlug: string;
userHasVoted: () => void;
currentVotation: LimitedVoteType | undefined;
}) {
const [currentVotation, setCurrentVotation] = useState<
LimitedVoteType | undefined
>();
>(state.currentVotation);
const matches = useMediaQuery("(min-width: 501px)");
const [chosenOption, setChosenOption] = useState<string>();
const { classes } = useStyles();

useEffect(() => {
const fetch = async () => {
const currentVotationData = await getCurrentVotationByGroup(
state.groupSlug
);
setCurrentVotation(currentVotationData);
};
fetch().catch(console.error);
// The current votation are initially set by websocket/state from parent component/assemblyPage.
// Undefined if page is reloaded, and in that case the votation will be fetched again.
if (!currentVotation) {
const fetch = async () => {
const currentVotationData = await getCurrentVotationByGroup(
state.groupSlug
);
setCurrentVotation(currentVotationData);
};
fetch().catch(console.error);
}
}, []);

function submit(voteId: string) {
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/pages/AssemblyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import { checkedInState, checkedInType } from "../utils/Context";
import { Box, Image, Text } from "@mantine/core";
import Arrow from "../assets/Arrow.svg";
import { getCurrentVotationByGroup } from "../services/votation";
import { LimitedVoteType } from "../types/votes";

export function AssemblyLobby() {
const [kickedOut, setKickedOut] = useState<boolean>(false);
const [activeVotation, setActiveVotation] = useState<boolean>(false);
const [currentVotation, setCurrentVotation] = useState<
LimitedVoteType | undefined
>(undefined);
const [voted, setVoted] = useState<boolean>(false);
const { lastMessage } = useWebSocket(import.meta.env.VITE_SOCKET_URL);
const { checkedIn, setCheckedIn, groupSlug, setGroupSlug, groupName } =
Expand Down Expand Up @@ -53,6 +57,7 @@ export function AssemblyLobby() {
setCheckedIn(true);
}
if (decodedMessage.status == "update") {
setCurrentVotation(decodedMessage.votation);
setCheckedIn(true);
setActiveVotation(true);
}
Expand Down Expand Up @@ -111,6 +116,7 @@ export function AssemblyLobby() {
) : checkedIn && groupSlug && activeVotation ? (
<VotationBox
groupSlug={groupSlug}
currentVotation={currentVotation}
userHasVoted={() => userHasVoted()}
/>
) : checkedIn && groupSlug && !activeVotation ? (
Expand Down