Skip to content

Commit

Permalink
add price history api worker
Browse files Browse the repository at this point in the history
  • Loading branch information
rrusher committed Jul 16, 2024
1 parent 3f9add5 commit 57adee3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
18 changes: 17 additions & 1 deletion scripts/apis/creg/creg.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function getSchools(lat, long) {
}

/**
* Gets the market trends for .
* Gets the market trends for a listing.
*
* @param {string} listingId - The ID of the listing.
* @param {string} lat latitude
Expand All @@ -134,3 +134,19 @@ export async function getMarketTrends(listingId, lat, long, zipcode) {
});
});
}

/**
* Gets the price history for a listing.
*
* @param {string} listingId - The ID of the listing.
* @return {Promise<Object>} resolving the economic details
*/
export async function getPriceHistory(listingId) {
return new Promise((resolve) => {
const worker = new Worker(`${window.hlx.codeBasePath}/scripts/apis/creg/workers/pricehistory.js`, { type: 'module' });
worker.onmessage = (e) => resolve(e.data);
worker.postMessage({
listingId,
});
});
}
19 changes: 19 additions & 0 deletions scripts/apis/creg/workers/pricehistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Handle the Worker event. Fetches price history for a provided listing id.
*
* @param {Object} event the worker event.
* @param {string} event.data.api the URL to fetch.
* @param {string} event.data.id listing id.
*/
onmessage = async (event) => {
const { listingId } = event.data;

try {
const response = await fetch(`/v1/pricehistory/${listingId}`);
const data = response.ok ? await response.json() : undefined;

postMessage(data);
} catch (error) {
postMessage({});
}
};

0 comments on commit 57adee3

Please sign in to comment.