-
Notifications
You must be signed in to change notification settings - Fork 2
/
CityListItems.js
132 lines (121 loc) · 3.59 KB
/
CityListItems.js
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
import { CityContext } from "../contexts/CityContext";
import {
Suspense,
useContext,
useState,
useTransition,
useEffect,
} from "react";
import useSwr from "swr";
import { CityLinkButton, CityLinkButtonFallback } from "./CityLinkButton";
export const CityListItemsFallback = () => {
const dummyArray = [1, 2, 3, 4, 5];
// [new Array(DEFAULTCITYCOUNT)]
return (
<div>
{dummyArray.map(function (rec, index) {
return (
<ul key={index} className="list-group">
<li className="list-group-item list-group-item-city-list-height mt-1 mb-1 bg-light">
<CityLinkButtonFallback />
</li>
</ul>
);
})}
</div>
);
};
function ProcessDataAndRender() {
const {
setSelectedCityId,
setSelectedCityName,
setSelectedStateName,
selectedCityId,
cityMax,
setHoursAgo,
} = useContext(CityContext);
const [tempId, setTempId] = useState();
const [isPending, startTransition] = useTransition();
const { data, error } = useSwr(`/api/data/cities?count=${cityMax}`, {
suspense: true,
});
useEffect(() => {
if (data && data?.length > 0) {
setTempId(data[0].id);
startTransition(() => {
setSelectedCityId(data[0].id);
setSelectedCityName(data[0].city);
setSelectedStateName(data[0].state);
});
}
}, [data, setSelectedCityId, setSelectedCityName, setSelectedStateName]);
return (
<div>
{data
?.sort((a, b) => {
if (a.pm25 > b.pm25) return -1;
if (a.pm25 < b.pm25) return 1;
return 0;
})
.map(function (rec, i) {
return (
<ul key={rec.id} className="list-group">
<li
className={
// highlight the first element by default
// change after user interaction
(!selectedCityId && i === 0) || rec.id === selectedCityId
? "list-group-item list-group-item-city-list-height mt-1 mb-1 border-dark"
: "list-group-item list-group-item-city-list-height mt-1 mb-1 bg-light"
}
>
<a
href="#"
onClick={(e) => {
e.preventDefault();
setTempId(rec.id);
startTransition(() => {
setSelectedCityId(rec.id);
setSelectedCityName(rec.city);
setSelectedStateName(rec.state);
setHoursAgo(0);
});
}}
>
<CityLinkButton
cityRec={rec}
isPending={
rec.id === tempId && isPending === true ? true : false
}
/>
</a>
</li>
</ul>
);
})}
</div>
);
}
function CityListItems() {
return (
<Suspense fallback={<CityListItemsFallback />}>
<ProcessDataAndRender />
</Suspense>
);
}
export default CityListItems;
// const CityListItemsFallback1 = () => {
// return <div>lll</div>;
// };
// const ProcessDataAndRender1 = () => {
// const fetcherDelay = () =>
// new Promise((resolve) => {
// setTimeout(() => resolve("test"), 5000);
// });
// const { data } = useSwr("/api/suspense", fetcherDelay);
//
// return <div>lll: {data}</div>;
// };
// https://github.com/reactwg/react-18/discussions/41#discussioncomment-841339
//const [isPending, startTransition] = useTransition();
//const [tempId, setTempId] = useState();