Skip to content

Commit f61c5df

Browse files
committed
Export weatherTemplate to component
1 parent 5ed8858 commit f61c5df

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed

components/weatherTemplate.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Image from "next/image";
2+
import { basePath } from "../next.config.js";
3+
4+
const icons = [
5+
"sunny.svg",
6+
"partially-sunny.svg",
7+
"cloudy.svg",
8+
"rainy.svg",
9+
"stormy.svg",
10+
];
11+
12+
export const getWeatherIndex = (stat) => {
13+
let fail_rate = 0;
14+
fail_rate = (stat["fails"] + stat["skips"]) / stat["runs"];
15+
// e.g. failing 3/9 runs is .33, or idx=1
16+
var idx = Math.floor((fail_rate * 10) / 2);
17+
if (idx == icons.length) {
18+
// edge case: if 100% failures, then we go past the end of icons[]
19+
// back the idx down by 1
20+
console.assert(fail_rate == 1.0);
21+
idx -= 1;
22+
}
23+
24+
// This error checks if there are zero runs.
25+
// Currently, will display stormy weather.
26+
if (isNaN(idx)) {
27+
idx = 4;
28+
}
29+
return idx;
30+
};
31+
32+
const getWeatherIcon = (stat) => {
33+
const idx = getWeatherIndex(stat);
34+
return icons[idx];
35+
};
36+
37+
export const weatherTemplate = (data) => {
38+
const icon = getWeatherIcon(data);
39+
return (
40+
<div>
41+
<Image
42+
src={`${basePath}/${icon}`}
43+
alt="weather"
44+
width={32}
45+
height={32}
46+
// priority
47+
/>
48+
</div>
49+
);
50+
};

pages/index.js

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import { useEffect, useState } from "react";
22
import { DataTable } from "primereact/datatable";
33
import { Column } from "primereact/column";
4-
import Image from "next/image";
5-
import { basePath } from "../next.config.js";
4+
import { weatherTemplate, getWeatherIndex } from "../components/weatherTemplate";
5+
66

77
export default function Home() {
88
const [loading, setLoading] = useState(true);
99
const [jobs, setJobs] = useState([]);
1010
const [rows, setRows] = useState([]);
1111
const [expandedRows, setExpandedRows] = useState([]);
1212

13-
const icons = [
14-
"sunny.svg",
15-
"partially-sunny.svg",
16-
"cloudy.svg",
17-
"rainy.svg",
18-
"stormy.svg",
19-
];
20-
2113
useEffect(() => {
2214
const fetchData = async () => {
2315
let data = {};
@@ -56,46 +48,6 @@ export default function Home() {
5648
setLoading(false);
5749
}, [jobs]);
5850

59-
const getWeatherIndex = (stat) => {
60-
let fail_rate = 0;
61-
fail_rate = (stat["fails"] + stat["skips"]) / stat["runs"];
62-
// e.g. failing 3/9 runs is .33, or idx=1
63-
var idx = Math.floor((fail_rate * 10) / 2);
64-
if (idx == icons.length) {
65-
// edge case: if 100% failures, then we go past the end of icons[]
66-
// back the idx down by 1
67-
console.assert(fail_rate == 1.0);
68-
idx -= 1;
69-
}
70-
71-
// This error checks if there are zero runs.
72-
// Currently, will display stormy weather.
73-
if (isNaN(idx)) {
74-
idx = 4;
75-
}
76-
return idx;
77-
};
78-
79-
const getWeatherIcon = (stat) => {
80-
const idx = getWeatherIndex(stat);
81-
return icons[idx];
82-
};
83-
84-
const weatherTemplate = (data) => {
85-
const icon = getWeatherIcon(data);
86-
return (
87-
<div>
88-
<Image
89-
src={`${basePath}/${icon}`}
90-
alt="weather"
91-
width={32}
92-
height={32}
93-
// priority
94-
/>
95-
</div>
96-
);
97-
};
98-
9951
const toggleRow = (rowData) => {
10052
const isRowExpanded = expandedRows.includes(rowData);
10153

0 commit comments

Comments
 (0)