Skip to content

Commit 3160211

Browse files
authored
Merge pull request #290 from UW-Macrostrat/strat-data
Strat Name Page Update
2 parents 4325611 + 43a8097 commit 3160211

File tree

4 files changed

+105
-19
lines changed

4 files changed

+105
-19
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { fetchAPIData } from "~/_utils/fetch-helpers";
2+
3+
export async function data() {
4+
const res = await fetchAPIData(`/defs/strat_name_concepts`, {
5+
page_size: 20,
6+
last_id: 0,
7+
});
8+
return { res };
9+
}

pages/lex/strat-names/+Page.client.ts

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ import { Card, Switch, Spinner } from "@blueprintjs/core";
66
import { useState, useEffect, useRef } from "react";
77
import { ContentPage } from "~/layouts";
88
import { Loading, SearchBar } from "../../index";
9+
import { useData } from "vike-react/useData";
910

1011
export function Page() {
1112
return StratPage({ show: false });
1213
}
1314

1415
export function StratPage({ show }) {
16+
const { res } = useData();
17+
console.log("res", res);
18+
const startingID = show
19+
? res[res?.length - 1]?.concept_id
20+
: res[res?.length - 1]?.strat_name_id;
21+
1522
const [input, setInput] = useState("");
1623
const [showConcepts, setShowConcepts] = useState(show ?? false);
17-
const [lastID, setLastID] = useState(0);
18-
const [data, setData] = useState([]);
24+
const [lastID, setLastID] = useState(startingID);
25+
const [data, setData] = useState(res);
1926
const pageSize = 20;
2027

2128
const strat_name_vars = {
@@ -38,16 +45,40 @@ export function StratPage({ show }) {
3845

3946
const result = useStratData(lastID, input, pageSize, data_route, like);
4047

48+
console.log(lastID);
49+
console.log("data", data);
50+
51+
const prevInputRef = useRef(input);
52+
const prevShowConceptsRef = useRef(showConcepts);
53+
4154
useEffect(() => {
42-
if (result) {
43-
setData((prevData) => [...prevData, ...result]);
55+
// Only reset if input or showConcepts actually changed from previous render
56+
if (
57+
prevInputRef.current !== input ||
58+
prevShowConceptsRef.current !== showConcepts
59+
) {
60+
// Reset data and lastID to starting ID for current mode
61+
setData([]);
62+
setLastID(0);
63+
64+
prevInputRef.current = input;
65+
prevShowConceptsRef.current = showConcepts;
4466
}
45-
}, [result]);
67+
}, [input, showConcepts]);
4668

4769
useEffect(() => {
48-
setData([]);
49-
setLastID(0);
50-
}, [input, showConcepts]);
70+
if (
71+
result &&
72+
data[data.length - 1]?.[showConcepts ? "concept_id" : "strat_name_id"] !==
73+
result[result.length - 1]?.[
74+
showConcepts ? "concept_id" : "strat_name_id"
75+
]
76+
) {
77+
setData((prevData) => {
78+
return [...prevData, ...result];
79+
});
80+
}
81+
}, [result]);
5182

5283
if (data == null) return h(Loading);
5384

@@ -59,17 +90,43 @@ export function StratPage({ show }) {
5990
h(StickyHeader, { className: "header" }, [
6091
h(PageBreadcrumbs, { title }),
6192
h("div.header-description", [
62-
h("p", [
63-
h("strong", "Strat Names: "),
64-
h("span", "names of rock units, organized hierarchically"),
65-
]),
66-
h("p", [
67-
h("strong", "Strat Concepts: "),
68-
h(
69-
"span",
70-
"capture relationships between differently-named rock units"
71-
),
72-
]),
93+
h(
94+
Card,
95+
{
96+
className: !showConcepts ? "selected" : "unselected",
97+
onClick: () => {
98+
if (showConcepts) {
99+
setShowConcepts(false);
100+
setLastID(0);
101+
setData([]);
102+
}
103+
},
104+
},
105+
[
106+
h("strong", "Strat Names: "),
107+
h("span", "names of rock units, organized hierarchically"),
108+
]
109+
),
110+
h(
111+
Card,
112+
{
113+
className: showConcepts ? "selected" : "unselected",
114+
onClick: () => {
115+
if (!showConcepts) {
116+
setShowConcepts(true);
117+
setLastID(0);
118+
setData([]);
119+
}
120+
},
121+
},
122+
[
123+
h("strong", "Strat Concepts: "),
124+
h(
125+
"span",
126+
"capture relationships between differently-named rock units"
127+
),
128+
]
129+
),
73130
]),
74131
h(Card, { className: "filter" }, [
75132
h(SearchBar, {
@@ -81,6 +138,8 @@ export function StratPage({ show }) {
81138
checked: showConcepts,
82139
onChange: (e) => {
83140
setShowConcepts(e.target.checked);
141+
setLastID(0);
142+
setData([]);
84143
},
85144
}),
86145
]),

pages/lex/strat-names/+data.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { fetchAPIData } from "~/_utils/fetch-helpers";
2+
3+
export async function data() {
4+
const res = await fetchAPIData(`/defs/strat_names`, {
5+
page_size: 20,
6+
last_id: 0,
7+
});
8+
return { res };
9+
}

pages/lex/strat-names/main.module.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@
7979
flex-direction: column;
8080
gap: 1em;
8181

82+
.selected {
83+
color: var(--text-emphasized-color) !important;
84+
background-color: var(--secondary-background) !important;
85+
}
86+
87+
.unselected {
88+
cursor: pointer;
89+
}
90+
8291
p {
8392
display: flex;
8493
align-items: flex-end;

0 commit comments

Comments
 (0)