/
LegislativeHistoryNode.tsx
158 lines (143 loc) · 5.29 KB
/
LegislativeHistoryNode.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React, { FC, useCallback } from "react";
import { useMediaQuery } from "react-responsive";
import { Link } from "react-router-dom";
import { useAppConfigContext, useLanguageConfigContext } from "../../../app";
import VoteService from "../../../networking/VoteService";
import EventMinutesItemFileService from "../../../networking/EventMinutesItemFileService";
import Vote from "../../../models/Vote";
import EventMinutesItem from "../../../models/EventMinutesItem";
import { VoteDistributionGraphic } from "./VoteDistributionGraphic";
import { Dot, DOT_SIZE } from "../../Shared/Dot";
import DocumentsList from "../MinutesItemsList/DocumentsList";
import { Document } from "../MinutesItemsList/types";
import useFetchData, {
initialFetchDataState,
} from "../../../containers/FetchDataContainer/useFetchData";
import LazyFetchDataContainer from "../../../containers/FetchDataContainer/LazyFetchDataContainer";
import styled from "@emotion/styled";
import Details from "../../Shared/Details";
import { strings } from "../../../assets/LocalizedStrings";
import { screenWidths } from "../../../styles/mediaBreakpoints";
import { DecisionResult } from "../../Shared";
export interface LegislativeHistoryNodeProps {
/** event in the matter's timeline */
eventMinutesItem: EventMinutesItem;
/** this node is the last index on the timeline */
isLastIndex: boolean;
}
interface LegislativeHistoryNodeData {
votes?: Vote[];
files?: Document[];
}
const LegislativeHistoryNode: FC<LegislativeHistoryNodeProps> = ({
eventMinutesItem,
isLastIndex,
}: LegislativeHistoryNodeProps) => {
const { municipality } = useAppConfigContext();
const isMobile = useMediaQuery({ query: `(max-width: ${screenWidths.tablet})` });
const MARGIN = isMobile ? 4 : 12;
const ReferenceFrame = styled.div({ position: "relative" });
const ExpandingContainer = styled.div({
margin: MARGIN,
display: "flex",
flexDirection: "row",
gap: MARGIN * 3,
});
const TextContainer = styled.div({ flex: 1 });
const SummaryInfo = styled.div({ display: "flex", flexDirection: "row" });
const VotingGraphicContainer = styled.div(() => ({
flex: 1,
marginTop: MARGIN,
marginLeft: isMobile ? 0 : MARGIN * 2,
}));
const ConnectBar = styled.div({
position: "absolute",
top: MARGIN + DOT_SIZE,
left: DOT_SIZE / 2 + MARGIN,
height: "100%",
width: 2,
zIndex: 0,
backgroundColor: "grey",
});
const { firebaseConfig } = useAppConfigContext();
const { language } = useLanguageConfigContext();
const localizedDateString = eventMinutesItem.event?.event_datetime.toLocaleDateString(language, {
timeZone: municipality.timeZone,
month: "long",
day: "numeric",
year: "numeric",
});
const fetchDataForNode = useCallback(async () => {
const data: LegislativeHistoryNodeData = {
votes: [],
files: [],
};
const eventMinutesItemFileService = new EventMinutesItemFileService(firebaseConfig);
const minutesItemFiles =
await eventMinutesItemFileService.getEventMinutesItemFilesByEventMinutesItemId(
eventMinutesItem.id
);
data.files = minutesItemFiles.map(({ name, uri }) => {
return {
label: name,
url: uri,
};
});
if (eventMinutesItem.decision) {
const votesService = new VoteService(firebaseConfig);
data.votes = await votesService.getVotesByEventMinutesItemId(eventMinutesItem.id);
}
return Promise.resolve(data);
}, [eventMinutesItem.id, eventMinutesItem.decision, firebaseConfig]);
const { state: votesDataState } = useFetchData<LegislativeHistoryNodeData>(
{ ...initialFetchDataState },
fetchDataForNode
);
return (
<ReferenceFrame>
<ExpandingContainer>
<Dot className={"cdp-bg-neutral-grey"} />
<TextContainer>
<SummaryInfo>
<Link to={`/events/${eventMinutesItem.event?.id}`}>{localizedDateString}</Link>
{eventMinutesItem.decision && (
<DecisionResult
result={eventMinutesItem.decision}
style={{
marginLeft: MARGIN,
display: "flex",
flex: 1,
alignItems: "center",
}}
/>
)}
</SummaryInfo>
<LazyFetchDataContainer
data="votes and documents associated with this event"
isLoading={votesDataState.isLoading}
error={votesDataState.error}
notFound={!votesDataState.data}
>
{votesDataState.data?.votes && votesDataState.data?.votes.length > 0 && (
<Details
summaryContent={strings.votes}
hiddenContent={
<VotingGraphicContainer>
<VoteDistributionGraphic votes={votesDataState.data?.votes} />
</VotingGraphicContainer>
}
defaultOpen={false}
hasBorderBottom={false}
/>
)}
{votesDataState.data?.files && votesDataState.data?.files.length > 0 && (
<DocumentsList documents={votesDataState.data?.files} />
)}
</LazyFetchDataContainer>
</TextContainer>
</ExpandingContainer>
{!isLastIndex && <ConnectBar />}
</ReferenceFrame>
);
};
export { LegislativeHistoryNode };