forked from Shopify/dawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-articles.js
93 lines (81 loc) · 2.91 KB
/
book-articles.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
// Option 1 - They manually copy slugs over to books
// Option 2 - I do this complex query shit
// NEW
// query:"title:author*",
const newsQuery = `query ($cursor: String) {
articles(first: 2, after: $cursor) {
edges {
node {
id
title
handle
image {
url
}
product: metafield(namespace: "godine", key: "product") {
value
type
}
}
}
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
}
}`;
const articlesFetchHeaders = new Headers();
articlesFetchHeaders.append('X-Shopify-Storefront-Access-Token', '0fc0056e09b62e93104f1e4d0f0b1dc1');
articlesFetchHeaders.append('Content-Type', 'application/json');
articlesFetchHeaders.append('Accept', 'application/json');
function getArticles(progress, cursor = null, articles = []) {
return new Promise((resolve, reject) =>
fetch('https://davidrgodine.myshopify.com/api/2024-04/graphql.json', {
body: JSON.stringify({
query: newsQuery,
variables: {
cursor,
},
}),
method: 'POST',
headers: articlesFetchHeaders,
redirect: 'follow',
})
.then((res) => {
if (res.status !== 200) {
throw `${res.status}: ${res.statusText}`;
}
console.log(`res`, res);
res
.text()
.then((textData) => {
const data = JSON.parse(textData).data.articles;
console.log(`data`, data);
const newsData = data.edges.map((obj) => obj.node);
articles = articles.concat(newsData);
if (data.pageInfo.hasNextPage) {
progress && progress(articles);
getArticles(progress, data.pageInfo.endCursor, articles).then(resolve).catch(reject);
} else {
resolve(articles);
}
})
.catch(reject);
})
.catch(reject)
);
}
// render progress
function progressCallback(articles) {
console.log(`${articles.length} loaded`);
}
getArticles(progressCallback)
.then((articles) => {
console.log(`articles`, articles); // all articles have been loaded
const relevantArticles = articles.filter((article) => article.product?.value === "gid://shopify/Product/{{ product.id }}")
console.log(`relevantArticles`, relevantArticles);
// https://stackoverflow.com/questions/11840858/creating-nested-tags-using-document-createelement
})
.catch(console.error);