-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgatsby-node.ts
62 lines (60 loc) · 2.14 KB
/
gatsby-node.ts
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
import path from "path";
import slug from "slug";
exports.createPages = ({ graphql, actions: { createPage } }) => {
return fetch(
`https://data.ademe.fr/data-fair/api/v1/datasets/que-faire-de-mes-dechets-produits/lines?format=json&q_mode=simple&size=1000&sampling=neighbors`,
)
.then((res) => res.json())
.then((res) =>
res.results.filter((waste) => typeof waste["ID"] !== "undefined"),
) // handle Koumoul missing ID
.then((wasteRes) =>
fetch(
"https://data.ademe.fr/data-fair/api/v1/datasets/que-faire-de-mes-dechets-liens/lines?format=json&q_mode=simple&size=1000&sampling=neighbors",
)
.then((res) => res.json())
.then((res) => res.results)
.then((linkRes) => {
let tempWaste = [...wasteRes];
for (let result of wasteRes) {
if (result["Synonymes_existants"]) {
const synonyms = result["Synonymes_existants"].split(" / ");
for (let i = 0; i < synonyms.length; i++) {
if (!tempWaste.find((waste) => waste["Nom"] === synonyms[i])) {
tempWaste.push({
...result,
ID: result["ID"] + "_" + i,
Nom: synonyms[i],
parent: result["Nom"],
});
}
}
}
}
return tempWaste.map((waste) => ({
...waste,
slug: slug(waste[`Nom`], { locale: "fr" }),
map:
waste["Bdd"] === "sinoe" ||
waste["Bdd"] === "google" ||
waste["Bdd"] === "ocad3e" ||
waste["Code"] === "ADEME_DASRI" ||
waste["Code"] === "ADEME_SOLAIRE",
links: linkRes.filter((link) =>
link["Produits_associes"]
.split("; ")
.includes(waste["ID"].split("_")[0]),
),
}));
}),
)
.then((res) =>
res.forEach((product) => {
createPage({
path: `/dechet/${product.slug}/`,
component: path.resolve("./src/templates/product.js"),
context: { product },
});
}),
);
};