Skip to content

Commit 54f8fb9

Browse files
Matin Gohar FarMatin Gohar Far
authored andcommitted
added cancel feature in frontend and added the according button
1 parent b93e843 commit 54f8fb9

3 files changed

Lines changed: 46 additions & 17 deletions

File tree

src/api/votings.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ export const VotingAPI = {
2727
}
2828
},
2929

30-
changeVotingStatus: async (board: string, voting: string) => {
30+
changeVotingStatus: async (board: string, voting: string, status?: string) => {
3131
try {
32-
const response = await fetch(`${SERVER_HTTP_URL}/boards/${board}/votings/${voting}`, {
32+
const options: RequestInit = {
3333
method: "PUT",
3434
credentials: "include",
35-
});
35+
};
36+
37+
if (typeof status !== "undefined") {
38+
options.body = JSON.stringify({status});
39+
options.headers = {"Content-Type": "application/json"};
40+
}
41+
42+
const response = await fetch(`${SERVER_HTTP_URL}/boards/${board}/votings/${voting}`, options);
3643

3744
if (response.status === 200) {
3845
return await response.json();

src/components/VotingDialog/VotingDialog.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import {useState} from "react";
2-
import {useTranslation} from "react-i18next";
3-
import {Dialog} from "components/Dialog";
4-
import {useNavigate} from "react-router";
5-
import {useAppDispatch, useAppSelector} from "store";
6-
import {Toggle} from "components/Toggle";
7-
import {getNumberFromStorage, saveToStorage, getFromStorage} from "utils/storage";
8-
import {CUMULATIVE_VOTING_DEFAULT_STORAGE_KEY, CUSTOM_NUMBER_OF_VOTES_STORAGE_KEY} from "constants/storage";
9-
import {PlusIcon, MinusIcon} from "components/Icon";
1+
import { useState } from "react";
2+
import { useTranslation } from "react-i18next";
3+
import { Dialog } from "components/Dialog";
4+
import { useNavigate } from "react-router";
5+
import { useAppDispatch, useAppSelector } from "store";
6+
import { Toggle } from "components/Toggle";
7+
import { getNumberFromStorage, saveToStorage, getFromStorage } from "utils/storage";
8+
import { CUMULATIVE_VOTING_DEFAULT_STORAGE_KEY, CUSTOM_NUMBER_OF_VOTES_STORAGE_KEY } from "constants/storage";
9+
import { PlusIcon, MinusIcon } from "components/Icon";
1010
import "./VotingDialog.scss";
11-
import {closeVoting, createVoting} from "store/features";
11+
import { closeVoting, createVoting, cancelVoting } from "store/features";
1212

1313
export const VotingDialog = () => {
1414
const dispatch = useAppDispatch();
15-
const {t} = useTranslation();
15+
const { t } = useTranslation();
1616
const navigate = useNavigate();
1717
const isAdmin = useAppSelector((state) => state.participants?.self?.role === "OWNER" || state.participants?.self?.role === "MODERATOR");
1818
const voting = useAppSelector((state) => state.votings.open?.id);
@@ -46,12 +46,23 @@ export const VotingDialog = () => {
4646
navigate("..");
4747
};
4848

49+
const abortVoting = () => {
50+
dispatch(cancelVoting(voting!));
51+
navigate("..");
52+
}
53+
4954
return (
5055
<Dialog className="voting-dialog accent-color__planning-pink" title={t("VoteConfigurationButton.label")} onClose={() => navigate("..")}>
5156
{voting ? (
52-
<button className="voting-dialog__start-button" data-testid="voting-dialog__stop-button" onClick={() => stopVoting()}>
53-
<label>{t("VoteConfigurationButton.stopVoting")}</label>
54-
</button>
57+
<>
58+
<button className="voting-dialog__start-button" data-testid="voting-dialog__stop-button" onClick={() => stopVoting()}>
59+
<label>{t("VoteConfigurationButton.stopVoting")}</label>
60+
</button>
61+
62+
<button className="voting-dialog__start-button voting-dialog__cancel-button" data-testid="voting-dialog__cancel-button" onClick={() => abortVoting()}>
63+
<label>{t("VoteConfigurationButton.cancelVoting")}</label>
64+
</button>
65+
</>
5566
) : (
5667
<>
5768
<button

src/store/features/votings/thunks.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ export const closeVoting = createAsyncThunk<void, string, {state: ApplicationSta
2424
"closeVoting"
2525
);
2626
});
27+
28+
export const cancelVoting = createAsyncThunk<void, string, {state: ApplicationState}>("votings/cancelVoting", async (payload, {dispatch, getState}) => {
29+
const boardId = getState().board.data!.id;
30+
31+
await retryable(
32+
() => API.changeVotingStatus(boardId, payload, "ABORTED"),
33+
dispatch,
34+
() => cancelVoting(payload),
35+
"cancelVoting"
36+
);
37+
});

0 commit comments

Comments
 (0)