forked from CouncilDataProject/cdp-frontend
/
MeetingVotesTableRow.tsx
135 lines (131 loc) · 4.28 KB
/
MeetingVotesTableRow.tsx
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useState } from "react";
import DecisionResult from "../../Shared/DecisionResult";
import { EVENT_MINUTES_ITEM_DECISION, VOTE_DECISION } from "../../../models/constants";
import { TAG_CONNECTOR } from "../../../constants/StyleConstants";
import { IndividualMeetingVote } from "../../Shared/Types/IndividualMeetingVote";
import { ReactiveTableRow } from "../ReactiveTableRow";
import { useMediaQuery } from "react-responsive";
import { screenWidths } from "../../../styles/mediaBreakpoints";
import { strings } from "../../../assets/LocalizedStrings";
import { Link } from "react-router-dom";
type MeetingVotesTableRowProps = {
/** the name of the matter that was voted on */
legislationName: string;
/** the description of the matter that was voted on */
legislationDescription: string;
/** the index of the row */
index: number;
/** the voting body decision */
councilDecision: EVENT_MINUTES_ITEM_DECISION;
/** link to the detail page of the matter being voted on */
legislationLink: string;
/** vote results by individual members on the matter in this row */
votes: IndividualMeetingVote[];
/** name of the columns this row displays */
columnNames: string[];
/** sizes of each column from left to right */
columnDistribution: string[];
};
function VoteCell(isExpanded: boolean, votes: IndividualMeetingVote[], isMobile: boolean) {
if (isExpanded) {
return (
<React.Fragment>
{votes.map((vote, index) => {
const personLink = `/people/${vote.personId}`;
return (
<div
key={`individual-vote-${index}-${vote.id}`}
style={{
display: "flex",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
}}
>
<Link style={{ flex: 1 }} to={personLink}>
{vote.name}
</Link>
<DecisionResult result={vote.decision} />
</div>
);
})}
</React.Fragment>
);
} else {
let votesFor = 0;
let votesAgainst = 0;
let nonVotes = 0;
votes.forEach((vote) => {
if (
[
VOTE_DECISION.ABSENT_APPROVE,
VOTE_DECISION.ABSTAIN_APPROVE,
VOTE_DECISION.APPROVE,
].includes(vote.decision)
)
votesFor++;
if (
[VOTE_DECISION.ABSENT_REJECT, VOTE_DECISION.ABSTAIN_REJECT, VOTE_DECISION.REJECT].includes(
vote.decision
)
)
votesAgainst++;
if (
[VOTE_DECISION.ABSENT_NON_VOTING, VOTE_DECISION.ABSTAIN_NON_VOTING].includes(vote.decision)
)
nonVotes++;
});
const votesForLabel = strings.number_approved.replace("{number}", `${votesFor}`);
const votesAgainstLabel = strings.number_rejected.replace("{number}", `${votesAgainst}`);
const votesAbstainedLabel = strings.number_non_voting.replace("{number}", `${nonVotes}`);
if (isMobile) {
return (
<div style={{ display: "flex", flexDirection: "column" }}>
<p>{votesForLabel}</p>
<p>{votesAgainstLabel}</p>
<p>{votesAbstainedLabel}</p>
</div>
);
} else {
return (
<p>{`${votesForLabel} ${TAG_CONNECTOR} ${votesAgainstLabel} ${TAG_CONNECTOR} ${votesAbstainedLabel}`}</p>
);
}
}
}
const MeetingVotesTableRow = ({
index,
legislationLink,
legislationName,
legislationDescription,
councilDecision,
votes,
columnNames,
columnDistribution,
}: MeetingVotesTableRowProps) => {
const [expanded, setExpanded] = useState(false);
const isMobile = useMediaQuery({ query: `(max-width: ${screenWidths.tablet})` });
return (
<ReactiveTableRow
key={`meeting-table-row-${index}`}
index={index}
columnNames={columnNames}
columnDistribution={columnDistribution}
onClick={() => {
setExpanded(!expanded);
}}
>
<div>
<Link to={legislationLink}>
<p className="mzp-c-card-desc" style={{ fontWeight: 600, marginBottom: 0 }}>
{legislationName}
</p>
</Link>
{!isMobile && <p>{legislationDescription}</p>}
</div>
<DecisionResult result={councilDecision} />
{VoteCell(expanded, votes, isMobile)}
</ReactiveTableRow>
);
};
export default MeetingVotesTableRow;