Skip to content

Commit e1ba587

Browse files
matthewsmawfieldfrozenhelium
authored andcommitted
Implement PER dashboard
1 parent dac73f5 commit e1ba587

100 files changed

Lines changed: 10647 additions & 26 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/src/views/PERDashboard/PERPerformanceDashboard/dataHandler.ts

Lines changed: 692 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"namespace": "pERDashboard",
3+
"strings": {
4+
"performanceTitle": "PER Performance Dashboard",
5+
"performanceLastUpdate": "Last updated:",
6+
"performanceResetFilter": "Clear Filter",
7+
"performanceResetFilterAriaLabel": "Reset all active filters",
8+
"performanceRegionToggleAriaLabel": "Toggle region filter for {region}",
9+
"performanceContainerAriaLabel": "PER Performance Dashboard",
10+
"performanceHeaderDescription": "This dashboard contains a summary of the overall preparedness and response capacity among National Societies engaged in the PER Approach. The values presented represent the ratings for each component within National Societies' PER Mechanism aggregated at global and regional levels. The visualisations show average rating, the changes in capacity over time, as well as top and bottom-rated components. You can filter the components by region.",
11+
"overviewHeading": "Performance Overview",
12+
"overviewDescription": "Click on a PER assessment cycle to filter",
13+
"globalRatingsHeading": "Global Performance Ratings",
14+
"globalRatingsDescription": "Overview of PER ratings and performance metrics",
15+
"performanceFetchFailedError": "Failed to load dashboard data"
16+
}
17+
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import {
2+
type Component,
3+
useEffect,
4+
useState,
5+
} from 'react';
6+
import {
7+
BlockLoading,
8+
Button,
9+
Container,
10+
PERAnalysis,
11+
PERRatingAnalysis,
12+
PERRegionToggle,
13+
} from '@ifrc-go/ui';
14+
import { useTranslation } from '@ifrc-go/ui/hooks';
15+
import { _cs } from '@togglecorp/fujs';
16+
17+
import {
18+
getComponentRatings,
19+
getCycles,
20+
getLastUpdateDate,
21+
groupDataByRegion,
22+
initializeData,
23+
summarizeData,
24+
} from './dataHandler';
25+
import { type AssessmentRecord } from './types';
26+
27+
import i18n from './i18n.json';
28+
import styles from './styles.module.css';
29+
30+
const PER_DASHBOARD_DATA_URL = 'https://api.github.com/repos/matthewsmawfield/ifrc-per-data-fetcher/contents/data/per-dashboard-data.json';
31+
const LAST_UPDATE_DATA_URL = 'https://api.github.com/repos/matthewsmawfield/ifrc-per-data-fetcher/contents/data/last-update.json';
32+
const GITHUB_TOKEN = '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';
33+
34+
interface ActiveFilters {
35+
id: number | null;
36+
region: string | null;
37+
year: number | null;
38+
cycle: number | undefined;
39+
}
40+
41+
function PERPerformanceDashboard() {
42+
const strings = useTranslation(i18n);
43+
const [activeFilters, setActiveFilters] = useState<ActiveFilters>({
44+
id: null,
45+
region: null,
46+
year: null,
47+
cycle: undefined,
48+
});
49+
50+
const [isLoading, setIsLoading] = useState(true);
51+
const [error, setError] = useState<string | null>(null);
52+
interface DashboardData {
53+
assessments: Record<string, Component>;
54+
}
55+
const [dashboardData, setDashboardData] = useState<DashboardData | null>(null);
56+
const [lastUpdateData, setLastUpdateData] = useState<AssessmentRecord[]| null>(null);
57+
58+
useEffect(() => {
59+
async function fetchData() {
60+
setIsLoading(true);
61+
setError(null);
62+
try {
63+
const [dashboardResponse, lastUpdateResponse] = await Promise.all([
64+
fetch(PER_DASHBOARD_DATA_URL, {
65+
headers: {
66+
Authorization: `Bearer ${GITHUB_TOKEN}`,
67+
Accept: 'application/vnd.github.v3.raw',
68+
},
69+
}),
70+
fetch(LAST_UPDATE_DATA_URL, {
71+
headers: {
72+
Authorization: `Bearer ${GITHUB_TOKEN}`,
73+
Accept: 'application/vnd.github.v3.raw',
74+
},
75+
}),
76+
]);
77+
78+
if (!dashboardResponse.ok || !lastUpdateResponse.ok) {
79+
throw new Error(strings.performanceFetchFailedError);
80+
}
81+
82+
const [dashboardResponseData, lastUpdateResponseData] = await Promise.all([
83+
dashboardResponse.json(),
84+
lastUpdateResponse.json(),
85+
]);
86+
87+
setDashboardData(dashboardResponseData);
88+
setLastUpdateData(lastUpdateResponseData);
89+
initializeData(dashboardResponseData, lastUpdateResponseData);
90+
} catch {
91+
setError(strings.performanceFetchFailedError);
92+
} finally {
93+
setIsLoading(false);
94+
}
95+
}
96+
97+
fetchData();
98+
}, [strings.performanceFetchFailedError]);
99+
100+
if (isLoading) {
101+
return (
102+
<Container
103+
className={styles.perPerformanceDashboard}
104+
childrenContainerClassName={styles.loadingContainer}
105+
aria-label={strings.performanceContainerAriaLabel}
106+
>
107+
<BlockLoading />
108+
</Container>
109+
);
110+
}
111+
112+
if (error) {
113+
return (
114+
<Container
115+
className={styles.perPerformanceDashboard}
116+
childrenContainerClassName={styles.content}
117+
aria-label={strings.performanceContainerAriaLabel}
118+
>
119+
{error}
120+
</Container>
121+
);
122+
}
123+
124+
if (!dashboardData || !lastUpdateData) {
125+
return null;
126+
}
127+
128+
const updateFilter = (
129+
filterType: keyof ActiveFilters,
130+
value: ActiveFilters[keyof ActiveFilters],
131+
): void => {
132+
setActiveFilters((prev) => ({
133+
...prev,
134+
[filterType]: prev[filterType] === value ? null : value,
135+
}));
136+
};
137+
138+
const handleCycleClick = (cycle: number | null): void => {
139+
updateFilter('cycle', cycle);
140+
};
141+
142+
const handleRegionClick = (region: string | null): void => {
143+
updateFilter('region', region);
144+
};
145+
146+
const ratings = getComponentRatings(activeFilters);
147+
148+
return (
149+
<>
150+
<div className={styles.lastUpdate}>
151+
{strings.performanceLastUpdate}
152+
{' '}
153+
{new Date(getLastUpdateDate()).toLocaleString()}
154+
</div>
155+
<div className={styles.headerDescription}>
156+
{strings.performanceHeaderDescription}
157+
</div>
158+
<div className={styles.content}>
159+
<PERRegionToggle
160+
activeRegion={activeFilters?.region}
161+
onRegionClick={handleRegionClick}
162+
regions={groupDataByRegion()}
163+
precision={1}
164+
showCount={false}
165+
/>
166+
<Container
167+
heading={strings.overviewHeading}
168+
headerDescription={
169+
strings.overviewDescription
170+
}
171+
className={_cs(styles.container, styles.perAnalysis)}
172+
withHeaderBorder
173+
actions={activeFilters.cycle !== undefined ? (
174+
<Button
175+
name={undefined}
176+
onClick={() => updateFilter('cycle', undefined)}
177+
aria-label={
178+
strings.performanceResetFilterAriaLabel
179+
}
180+
>
181+
{strings.performanceResetFilter}
182+
</Button>
183+
) : undefined}
184+
>
185+
<PERAnalysis
186+
data={getCycles(activeFilters)}
187+
summary={summarizeData(activeFilters, true)}
188+
onCycleClick={handleCycleClick}
189+
activeCycle={activeFilters.cycle}
190+
/>
191+
</Container>
192+
<Container
193+
heading={strings.globalRatingsHeading}
194+
headerDescription={
195+
strings.globalRatingsDescription
196+
}
197+
withHeaderBorder
198+
className={_cs(styles.container, styles.ratingAnalysis)}
199+
>
200+
<PERRatingAnalysis
201+
overallRating={ratings.overallRating}
202+
areaData={ratings.areaData}
203+
componentData={ratings.componentData}
204+
/>
205+
</Container>
206+
</div>
207+
</>
208+
);
209+
}
210+
211+
export default PERPerformanceDashboard;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
.per-performance-dashboard {
2+
display: flex;
3+
flex-direction: column;
4+
gap: var(--go-ui-spacing-md);
5+
margin: 0;
6+
/* background-color: var(--go-ui-color-background); */
7+
padding: var(--go-ui-spacing-2xl) 0;
8+
overflow-x: hidden;
9+
}
10+
11+
.loading-container {
12+
display: flex;
13+
align-items: center;
14+
justify-content: center;
15+
margin: 0;
16+
padding: 0;
17+
min-height: 500px;
18+
}
19+
20+
.lastUpdate {
21+
position: absolute;
22+
top: 20px;
23+
margin-bottom: var(--go-ui-spacing-md);
24+
padding: 0 var(--go-ui-spacing-md);
25+
color: var(--go-ui-color-gray-60);
26+
font-size: var(--go-ui-font-size-sm);
27+
}
28+
29+
.header-body {
30+
line-height: var(--go-ui-line-height-md);
31+
color: var(--go-ui-color-text);
32+
font-size: var(--go-ui-font-size-md);
33+
}
34+
35+
.headerDescription {
36+
margin-bottom: var(--go-ui-spacing-md);
37+
background-color: var(--go-ui-color-background);
38+
padding: var(--go-ui-spacing-xl);
39+
width: auto;
40+
text-align: center;
41+
font-size: var(--go-ui-font-size-md);
42+
43+
}
44+
45+
.content {
46+
display: flex;
47+
flex-direction: column;
48+
gap: var(--go-ui-spacing-md);
49+
margin: 0;
50+
background-color: var(--go-ui-color-white);
51+
padding: var(--go-ui-spacing-md);
52+
}
53+
54+
.container {
55+
width: 100%;
56+
max-width: 100%;
57+
}
58+
59+
.perAnalysis {
60+
margin-bottom: var(--go-ui-spacing-md);
61+
}
62+
63+
.ratingAnalysis {
64+
width: 100%;
65+
max-width: 100%;
66+
}
67+
68+
.ratingAnalysis > div {
69+
padding: 0px !important;
70+
max-width: 100% !important;
71+
--max-width: 100% !important;
72+
}
73+
74+
.ratingAnalysis :global(.content) {
75+
width: 100%;
76+
max-width: 100%;
77+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Base interfaces
2+
export interface Assessment {
3+
assessment_id: number;
4+
assessment_number: number;
5+
country_id: number;
6+
country_name: string;
7+
region_id: number;
8+
region_name: string;
9+
date_of_assessment: string;
10+
rating_value: number;
11+
rating_title: string;
12+
}
13+
14+
export interface ComponentSummary {
15+
name: string;
16+
value: number;
17+
children?: ComponentSummary[];
18+
}
19+
20+
export interface AssessmentRecord {
21+
id: number;
22+
country_id: number;
23+
country_name: string;
24+
region_name: string;
25+
date_of_assessment: string;
26+
phase: number;
27+
phase_display: string;
28+
assessment_number: number;
29+
type_of_assessment_name: string;
30+
prioritized_components: {
31+
areaTitle: string;
32+
componentTitle: string;
33+
}[];
34+
epi_considerations: boolean;
35+
climate_environmental_considerations: boolean;
36+
urban_considerations: boolean;
37+
migration_considerations: boolean;
38+
}
39+
40+
export interface Filters {
41+
region?: string | null;
42+
year?: number | null;
43+
phase?: number | null;
44+
id?: number | null;
45+
perConsiderations?: string | null;
46+
completedAssessment?: boolean | null;
47+
highPriorityComponent?: string | null;
48+
assessmentType?: string | null;
49+
numberOfCycles?: number | null;
50+
}

0 commit comments

Comments
 (0)