This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proposal2.jsx
111 lines (103 loc) · 3.09 KB
/
Proposal2.jsx
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
import React, { useState } from "react";
import { Button, Form } from "react-bootstrap";
import VoteAPI from "../../API/vote.api.mjs";
const Proposal2 = ({ proposal, votes, setShouldRefresh, mine }) => {
const [errorMessage, setErrorMessage] = useState("");
const [changeVote, setChangeVote] = useState(false);
const [newVote, setNewVote] = useState(0);
const handleChangeVote = (e) => {
e.preventDefault();
const vote = parseInt(e.target.value);
if (
vote !== 0 &&
vote !== 1 &&
vote !== 2 &&
vote !== 3 &&
e.target.value !== ""
) {
setErrorMessage("Vote must be an integer x ≤ 3");
return;
} else {
setErrorMessage("");
setNewVote(vote);
}
};
const handleSubmit = (score) => (e) => {
e.preventDefault();
if (errorMessage !== "") return;
setChangeVote(false);
VoteAPI.insertVote(proposal, score || 0)
.then(() => setShouldRefresh(true))
.catch((err) => {
if (err.message === "Not in phase 2") setShouldRefresh(true);
else setErrorMessage(err.message ? err.message : err);
});
};
return (
<div className="col-md-4 col-sm-6 col-6">
<div className={"box" + (mine ? "" : " approved")}>
<p className="box-description">{proposal.description}</p>
<p>
<strong>Cost:</strong> ${proposal.cost}
</p>
{errorMessage !== "" ? (
<Form.Group className="mb-0">
<p style={{ color: "red" }} className="mb-0">
{errorMessage}
</p>
</Form.Group>
) : (
<></>
)}
<div className="d-flex align-items-center">
{mine ? (
<></>
) : (
<Button
onClick={() => {
setChangeVote(!changeVote);
setNewVote(0);
setErrorMessage("");
}}
className="btn-warning mt-2 mb-2"
>
<i className="bi bi-star" /> Edit
</Button>
)}
{changeVote && !mine ? (
<Form onSubmit={handleSubmit(newVote)} className="row ml-0 mt-1">
<Form.Group className="ml-3 mb-2" id="insertVoteInput">
<input
className="form-control inputText"
id="description"
rows="3"
onChange={handleChangeVote}
autoFocus
></input>
</Form.Group>
<Button type="submit" className="btn-danger ml-3 mb-2">
Vote!
</Button>
</Form>
) : !mine ? (
<p className="ml-3 mb-0 pb-0">vote: {votes}</p>
) : (
<></>
)}
{!changeVote && !mine ? (
<Button
onClick={handleSubmit(0)}
style={{ width: "4em" }}
className="btn-danger ml-3 mt-2 mb-2"
>
<i className="bi bi-trash" />
</Button>
) : (
<></>
)}
</div>
</div>
</div>
);
};
export default Proposal2;