-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathVotingDialog.tsx
More file actions
112 lines (103 loc) · 4.73 KB
/
Copy pathVotingDialog.tsx
File metadata and controls
112 lines (103 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Dialog } from "components/Dialog";
import { useNavigate } from "react-router";
import { useAppDispatch, useAppSelector } from "store";
import { Toggle } from "components/Toggle";
import { getNumberFromStorage, saveToStorage, getFromStorage } from "utils/storage";
import { CUMULATIVE_VOTING_DEFAULT_STORAGE_KEY, CUSTOM_NUMBER_OF_VOTES_STORAGE_KEY } from "constants/storage";
import { PlusIcon, MinusIcon } from "components/Icon";
import "./VotingDialog.scss";
import { closeVoting, createVoting, abortVoting } from "store/features";
export const VotingDialog = () => {
const dispatch = useAppDispatch();
const { t } = useTranslation();
const navigate = useNavigate();
const isAdmin = useAppSelector((state) => state.participants?.self?.role === "OWNER" || state.participants?.self?.role === "MODERATOR");
const voting = useAppSelector((state) => state.votings.open?.id);
const cumulativeVotingStorage = getFromStorage(CUMULATIVE_VOTING_DEFAULT_STORAGE_KEY);
const cumulativeVotingDefault = !(typeof cumulativeVotingStorage !== "undefined" && cumulativeVotingStorage !== null && cumulativeVotingStorage === "false");
const [allowCumulativeVoting, setAllowCumulativeVoting] = useState(cumulativeVotingDefault);
const [numberOfVotes, setNumberOfVotes] = useState(getNumberFromStorage(CUSTOM_NUMBER_OF_VOTES_STORAGE_KEY, 5));
const [isAnonymous, setIsAnonymous] = useState(true);
if (!isAdmin) {
navigate("..");
}
const startVoting = () => {
dispatch(
createVoting({
voteLimit: numberOfVotes,
showVotesOfOthers: false,
allowMultipleVotes: allowCumulativeVoting,
isAnonymous,
})
);
saveToStorage(CUSTOM_NUMBER_OF_VOTES_STORAGE_KEY, String(numberOfVotes));
saveToStorage(CUMULATIVE_VOTING_DEFAULT_STORAGE_KEY, String(allowCumulativeVoting));
navigate("..");
};
const stopVoting = () => {
dispatch(closeVoting(voting!));
navigate("..");
};
const abort_voting = () => {
dispatch(abortVoting(voting!));
navigate("..");
}
return (
<Dialog className="voting-dialog accent-color__planning-pink" title={t("VoteConfigurationButton.label")} onClose={() => navigate("..")}>
{voting ? (
<>
<button className="voting-dialog__start-button" data-testid="voting-dialog__stop-button" onClick={() => stopVoting()}>
<label>{t("VoteConfigurationButton.stopVoting")}</label>
</button>
<button className="voting-dialog__start-button voting-dialog__cancel-button" data-testid="voting-dialog__cancel-button" onClick={() => abort_voting()}>
<label>{t("VoteConfigurationButton.cancelVoting")}</label>
</button>
</>
) : (
<>
<button
className="dialog__button"
data-testid="voting-dialog__anonymous-voting-button"
onClick={() => {
setIsAnonymous((prev) => !prev);
}}
>
<label>{t("VoteConfigurationButton.isAnonymous")}</label>
<Toggle active={isAnonymous} className="voting-dialog__toggle" />
</button>
<button className="dialog__button" data-testid="voting-dialog__cumulative-voting-button" onClick={() => setAllowCumulativeVoting((state) => !state)}>
<label>{t("VoteConfigurationButton.allowMultipleVotesPerNote")}</label>
<Toggle active={allowCumulativeVoting} className="voting-dialog__toggle" />
</button>
<div className="dialog__button">
<label>{t("VoteConfigurationButton.numberOfVotes")}</label>
<div className="voting-dialog__votes-controls">
<button
onClick={() => setNumberOfVotes((prev) => Math.max(prev - 1, 1))}
className="voting-dialog__vote-button"
data-testid="voting-dialog__minus-button"
aria-label={t("VoteConfigurationButton.decreaseVotes")}
>
<MinusIcon />
</button>
<label className="voting-dialog__vote-label">{numberOfVotes}</label>
<button
onClick={() => setNumberOfVotes((prev) => Math.min(prev + 1, 99))}
className="voting-dialog__vote-button"
data-testid="voting-dialog__plus-button"
aria-label={t("VoteConfigurationButton.increaseVotes")}
>
<PlusIcon />
</button>
</div>
</div>
<button className="voting-dialog__start-button" data-testid="voting-dialog__start-button" onClick={() => startVoting()}>
<label>{t("VoteConfigurationButton.startVoting")}</label>
</button>
</>
)}
</Dialog>
);
};