Skip to content

Commit 0e3c26c

Browse files
committed
Update rest
1 parent ceb6e22 commit 0e3c26c

File tree

9 files changed

+44
-121
lines changed

9 files changed

+44
-121
lines changed
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const col_group_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
67

78
// Await all API calls
8-
const [resData, colData, refs1, refs2] = await Promise.all([
9+
const [resData, colData, fossilsData, refs1, refs2] = await Promise.all([
910
fetchAPIData("/defs/groups", { col_group_id }),
1011
fetchAPIData("/columns", {
1112
col_group_id,
1213
response: "long",
1314
format: "geojson",
1415
}),
16+
fetchAPIData("/fossils", { col_group_id, format: "geojson" }),
1517
fetchAPIRefs("/fossils", { col_group_id }),
1618
fetchAPIRefs("/columns", { col_group_id }),
1719
]);
@@ -20,17 +22,7 @@ export async function data(pageContext) {
2022
const refValues2 = Object.values(refs2);
2123
const refs = [...refValues1, ...refValues2];
2224

23-
const cols = colData?.features
24-
?.map((feature) => feature.properties.col_id)
25-
?.join(",");
26-
27-
let taxaData = null;
28-
if (cols) {
29-
const response = await fetch(
30-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
31-
);
32-
taxaData = await response.json();
33-
}
25+
const taxaData = await getPrevalentTaxa(fossilsData);
3426

3527
return { resData: resData[0], colData, taxaData, refs };
3628
}
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const col_group_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
67

78
// Await all API calls
8-
const [resData, colData, refs1, refs2] = await Promise.all([
9+
const [resData, colData, fossilsData,refs1, refs2] = await Promise.all([
910
fetchAPIData("/defs/groups", { col_group_id }),
1011
fetchAPIData("/columns", {
1112
col_group_id,
1213
response: "long",
1314
format: "geojson",
1415
}),
16+
fetchAPIData("/fossils", { col_group_id, format: "geojson" }),
1517
fetchAPIRefs("/fossils", { col_group_id }),
1618
fetchAPIRefs("/columns", { col_group_id }),
1719
]);
@@ -20,17 +22,7 @@ export async function data(pageContext) {
2022
const refValues2 = Object.values(refs2);
2123
const refs = [...refValues1, ...refValues2];
2224

23-
const cols = colData?.features
24-
?.map((feature) => feature.properties.col_id)
25-
?.join(",");
26-
27-
let taxaData = null;
28-
if (cols) {
29-
const response = await fetch(
30-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
31-
);
32-
taxaData = await response.json();
33-
}
25+
const taxaData = await getPrevalentTaxa(fossilsData);
3426

3527
return { resData: resData[0], colData, taxaData, refs };
3628
}

pages/lex/economics/@id/+data.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const econ_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
@@ -42,26 +43,7 @@ export async function data(pageContext) {
4243
const refValues2 = refs2 ? Object.values(refs2) : [];
4344
const refs = [...refValues1, ...refValues2];
4445

45-
// Build column ID list for PBDB taxa fetch
46-
const cols = colData?.features
47-
?.map((feature) => feature.properties.col_id)
48-
?.join(",");
49-
50-
let taxaData = null;
51-
if (cols) {
52-
try {
53-
const response = await fetch(
54-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
55-
);
56-
if (response.ok) {
57-
taxaData = await response.json();
58-
} else {
59-
console.warn("PBDB taxa fetch failed with status", response.status);
60-
}
61-
} catch (err) {
62-
console.warn("Error fetching PBDB taxa data:", err);
63-
}
64-
}
46+
const taxaData = await getPrevalentTaxa(fossilsData);
6547

6648
return {
6749
resData: resData?.[0] ?? null,

pages/lex/environments/@id/+data.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const environ_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
@@ -48,21 +49,7 @@ export async function data(pageContext) {
4849
?.join(",");
4950

5051
// Fetch PBDB fossil prevalence data
51-
let taxaData = null;
52-
if (cols) {
53-
try {
54-
const response = await fetch(
55-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
56-
);
57-
if (response.ok) {
58-
taxaData = await response.json();
59-
} else {
60-
console.warn("PBDB taxa fetch failed with status", response.status);
61-
}
62-
} catch (err) {
63-
console.warn("Error fetching PBDB taxa data:", err);
64-
}
65-
}
52+
const taxaData = await getPrevalentTaxa(fossilsData);
6653

6754
return {
6855
resData: resData?.[0] ?? null,

pages/lex/intervals/@id/+data.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const int_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
@@ -42,27 +43,7 @@ export async function data(pageContext) {
4243
const refValues2 = refs2 ? Object.values(refs2) : [];
4344
const refs = [...refValues1, ...refValues2];
4445

45-
// Extract column IDs
46-
const cols = colData?.features
47-
?.map((feature) => feature.properties.col_id)
48-
?.join(",");
49-
50-
// Optional: Fetch PBDB taxa data
51-
let taxaData = null;
52-
if (cols) {
53-
try {
54-
const response = await fetch(
55-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
56-
);
57-
if (response.ok) {
58-
taxaData = await response.json();
59-
} else {
60-
console.warn("PBDB taxa fetch failed with status", response.status);
61-
}
62-
} catch (err) {
63-
console.warn("Error fetching PBDB taxa data:", err);
64-
}
65-
}
46+
const taxaData = await getPrevalentTaxa(fossilsData);
6647

6748
return {
6849
resData: resData?.[0] ?? null,

pages/lex/lithologies/@id/+data.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const lith_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
@@ -43,25 +44,7 @@ export async function data(pageContext) {
4344
const refValues2 = refs2 ? Object.values(refs2) : [];
4445
const refs = [...refValues1, ...refValues2];
4546

46-
// Extract column IDs to fetch taxa data (PBDB)
47-
const collectionIds = fossilsData?.features
48-
?.map((feature) => feature.properties.cltn_id)
49-
50-
let taxaData = null;
51-
if (collectionIds && collectionIds.length > 0) {
52-
try {
53-
const response = await fetch(
54-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${collectionIds.join(",")}`
55-
);
56-
if (response.ok) {
57-
taxaData = await response.json();
58-
} else {
59-
console.warn("PBDB taxa fetch failed with status", response.status);
60-
}
61-
} catch (err) {
62-
console.warn("Error fetching taxa data from PBDB:", err);
63-
}
64-
}
47+
const taxaData = await getPrevalentTaxa(fossilsData);
6548

6649
return {
6750
resData: resData?.[0] ?? null,

pages/lex/strat-concepts/@id/+data.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { apiDomain, pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const concept_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
@@ -21,17 +22,7 @@ export async function data(pageContext) {
2122
const refValues2 = refs2 ? Object.values(refs2) : [];
2223
const refs = [...refValues1, ...refValues2];
2324

24-
const cols = colData?.features
25-
?.map((feature) => feature.properties.col_id)
26-
?.join(",");
27-
28-
let taxaData = null;
29-
if (cols) {
30-
const response = await fetch(
31-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
32-
);
33-
taxaData = await response.json();
34-
}
25+
const taxaData = await getPrevalentTaxa(fossilsData);
3526

3627
return { resData: resData[0], refs, fossilsData, colData, taxaData, unitsData };
3728
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pbdbDomain } from "@macrostrat-web/settings";
22
import { fetchAPIData, fetchAPIRefs } from "~/_utils";
3+
import { getPrevalentTaxa } from "~/components/lex/data-helper";
34

45
export async function data(pageContext) {
56
const strat_name_id = parseInt(pageContext.urlParsed.pathname.split("/")[3]);
@@ -24,17 +25,7 @@ export async function data(pageContext) {
2425
const refValues2 = Object.values(refs2);
2526
const refs = [...refValues1, ...refValues2];
2627

27-
const cols = colData?.features
28-
?.map((feature) => feature.properties.col_id)
29-
?.join(",");
30-
31-
let taxaData = null;
32-
if (cols) {
33-
const response = await fetch(
34-
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${cols}`
35-
);
36-
taxaData = await response.json();
37-
}
28+
const taxaData = await getPrevalentTaxa(fossilsData);
3829

3930
return {
4031
resData: resData[0],

src/components/lex/data-helper.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { pbdbDomain } from "@macrostrat-web/settings";
2+
3+
export async function getPrevalentTaxa(fossilsData) {
4+
// Extract cltn IDs to fetch taxa data (PBDB)
5+
const collectionIds = fossilsData?.features
6+
?.map((feature) => feature.properties.cltn_id)
7+
8+
let taxaData = null;
9+
if (collectionIds && collectionIds.length > 0) {
10+
try {
11+
const response = await fetch(
12+
`${pbdbDomain}/data1.2/occs/prevalence.json?limit=5&coll_id=${collectionIds.join(",")}`
13+
);
14+
if (response.ok) {
15+
taxaData = await response.json();
16+
} else {
17+
console.warn("PBDB taxa fetch failed with status", response.status);
18+
}
19+
} catch (err) {
20+
console.warn("Error fetching taxa data from PBDB:", err);
21+
}
22+
}
23+
return taxaData;
24+
}

0 commit comments

Comments
 (0)