From 34501158c31a40c9a5242e81df4c2c085cf69042 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 4 Dec 2024 09:11:08 +0100 Subject: [PATCH] use fetch to get reviews --- public/campfire-commerce/script.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/public/campfire-commerce/script.js b/public/campfire-commerce/script.js index e28c347..446037d 100644 --- a/public/campfire-commerce/script.js +++ b/public/campfire-commerce/script.js @@ -1,6 +1,8 @@ // Fetch product data from JSON source // blocked by https://github.com/lightpanda-io/browsercore/issues/187 // window.addEventListener("load", () => { + + // use XHR to retrieve the product's infos. const detailsXHR = new XMLHttpRequest(); // blocked by https://github.com/lightpanda-io/browsercore/issues/186 // detailsXHR.open('GET', 'json/product.json'); @@ -13,17 +15,19 @@ detailsXHR.open('GET', document.URL + 'json/product.json'); }; detailsXHR.send(); - const reviewsXHR = new XMLHttpRequest(); - // blocked by https://github.com/lightpanda-io/browsercore/issues/186 - // reviewsXHR.open('GET', 'json/reviews.json'); - reviewsXHR.open('GET', document.URL + 'json/reviews.json'); - reviewsXHR.responseType = 'json'; - reviewsXHR.onload = function() { - if (this.status === 200) { - updateReviews(this.response); + // use fetch to retrieve reviews. + (async function () { + try { + const url = document.URL + 'json/reviews.json'; + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Response status: ${response.status}`); + } + updateReviews(await response.json()); + } catch (error) { + console.error(error.message); } - }; - reviewsXHR.send(); + }()); // blocked by https://github.com/lightpanda-io/browsercore/issues/185 //