Skip to content

Commit f065a6f

Browse files
committed
Strat names load more works
1 parent 14e68d5 commit f065a6f

File tree

1 file changed

+24
-70
lines changed

1 file changed

+24
-70
lines changed

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

Lines changed: 24 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,22 @@ import { useState, useEffect, useRef } from "react";
77
import { ContentPage } from "~/layouts";
88
import { SearchBar, StratTag } from "~/components/general";
99
import { useData } from "vike-react/useData";
10+
import { InfiniteScrollView } from "@macrostrat/ui-components";
1011

1112
export function Page() {
1213
const { res } = useData();
13-
const startingID = res[res.length - 1].combined_id;
1414

1515
const [input, setInput] = useState("");
16-
const [lastID, setLastID] = useState(startingID);
17-
const [data, setData] = useState(res);
1816
const pageSize = 20;
1917

20-
const result = useStratData(lastID, input, pageSize);
21-
const prevInputRef = useRef(input);
22-
23-
useEffect(() => {
24-
if (prevInputRef.current !== input) {
25-
setData([]);
26-
setLastID(0);
27-
28-
prevInputRef.current = input;
29-
}
30-
}, [input]);
31-
32-
useEffect(() => {
33-
if (
34-
result &&
35-
data[data.length - 1]?.combined_id !==
36-
result[result.length - 1]?.combined_id
37-
) {
38-
setData((prevData) => {
39-
return [...prevData, ...result];
40-
});
41-
}
42-
}, [result]);
43-
4418
const handleChange = (event) => {
4519
setInput(event.toLowerCase());
4620
};
4721

4822
return h(ContentPage, [
4923
h(StickyHeader, { className: "header" }, [
5024
h(PageBreadcrumbs, {
51-
title: "Strat Names",
25+
title: "Minerals",
5226
}),
5327
h("div.header-description", [
5428
h(
@@ -80,17 +54,28 @@ export function Page() {
8054
onChange: handleChange,
8155
}),
8256
]),
83-
h(
84-
"div.strat-list",
85-
h(
86-
"div.strat-items",
87-
data.map((data) => h(StratItem, { data, input }))
88-
)
89-
),
90-
LoadMoreTrigger({ data, setLastID, pageSize, result }),
57+
h(InfiniteScrollView, {
58+
params: {
59+
order: "combined_id.asc",
60+
all_names: `ilike.*${input}*`,
61+
combined_id: `gt.0`,
62+
limit: pageSize,
63+
},
64+
route: `${apiDomain}/api/pg/strat_combined`,
65+
getNextParams,
66+
initialData: res,
67+
itemComponent: StratItem,
68+
})
9169
]);
9270
}
9371

72+
function getNextParams(response, params) {
73+
return {
74+
...params,
75+
combined_id: "gt." + response[response.length - 1].combined_id,
76+
};
77+
}
78+
9479
function StratItem({ data, input }) {
9580
const { concept_id, id } = data;
9681
const isConcept = !id;
@@ -129,9 +114,10 @@ function ConceptBody({ data, input }) {
129114
}));
130115

131116
// only show strats that match the input
132-
if (strats?.some((s) => s.name.toLowerCase().includes(input.toLowerCase()))) {
117+
if (strats?.some((s) => s.name.toLowerCase().includes(input?.toLowerCase()))) {
118+
console.log("Filtering strats", strats, input);
133119
strats = strats.filter((s) =>
134-
s.name.toLowerCase().includes(input.toLowerCase())
120+
s.name.toLowerCase().includes(input?.toLowerCase())
135121
);
136122
}
137123

@@ -154,35 +140,3 @@ function ConceptBody({ data, input }) {
154140
]),
155141
]);
156142
}
157-
158-
function useStratData(lastID, input, pageSize) {
159-
const url = `${apiDomain}/api/pg/strat_combined?limit=${pageSize}&combined_id=gt.${lastID}&order=combined_id.asc&all_names=ilike.*${input}*`;
160-
161-
const result = useAPIResult(url);
162-
163-
return result;
164-
}
165-
166-
function LoadMoreTrigger({ data, setLastID, pageSize, result }) {
167-
const ref = useRef(null);
168-
169-
useEffect(() => {
170-
if (!ref.current) return;
171-
172-
const observer = new IntersectionObserver(([entry]) => {
173-
if (entry.isIntersecting) {
174-
if (data.length > 0) {
175-
const id = data[data.length - 1].combined_id;
176-
177-
setLastID(id);
178-
}
179-
}
180-
});
181-
182-
observer.observe(ref.current);
183-
184-
return () => observer.disconnect();
185-
}, [data, setLastID]);
186-
187-
return h.if(result?.length == pageSize)("div.load-more", { ref }, h(Spinner));
188-
}

0 commit comments

Comments
 (0)