From d42d14df047bb6aace701cb7c215dfa0a6ba9b1f Mon Sep 17 00:00:00 2001 From: sujal Date: Fri, 31 May 2024 14:37:42 +0530 Subject: [PATCH 1/2] updated fetchprojects --- src/pages/NasaProjects/NasaProjects.jsx | 56 ++++++++++++++++--------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/pages/NasaProjects/NasaProjects.jsx b/src/pages/NasaProjects/NasaProjects.jsx index dd033b6..b38c177 100644 --- a/src/pages/NasaProjects/NasaProjects.jsx +++ b/src/pages/NasaProjects/NasaProjects.jsx @@ -100,33 +100,51 @@ function TechNews() { } } + //breaking array into chunks + function chunkArray(array, size) { + const result = []; + for (let i = 0; i < array.length; i += size) { + result.push(array.slice(i, i + size)); + } + return result; + } // fetches projects by the projects Id's. async function fetchProjects(projectsIds) { try { - const temp = [] - for (const id of projectsIds) { - let response = await fetch(`https://inplace-ghib.onrender.com/inplace`, { - method: "POST", - body: - JSON.stringify({ + const chunkSize = 20; //It is the batch size + const projectChunks = chunkArray(projectsIds, chunkSize); + const allProjects = []; + + for (const chunk of projectChunks) { + const projectPromises = chunk.map(async (id) => { + let response = await fetch(`https://inplace-ghib.onrender.com/inplace`, { + method: "POST", + body: JSON.stringify({ url: `https://techport.nasa.gov/api/projects/${id}?api_key=${import.meta.env.VITE_API_KEY}` }), - headers: { - "Content-Type": "application/json", - }, - }) - const data = await ReadableStreamDecoder(response.body); - const { projectId, title, acronym, description, startDateString, endDateString, lastUpdated, statusDescription } = data.project; - temp.push({ projectId, title, acronym, description, startDateString, endDateString, lastUpdated, statusDescription }); + headers: { + "Content-Type": "application/json", + }, + }); + const data = await ReadableStreamDecoder(response.body); + const { projectId, title, acronym, description, startDateString, endDateString, lastUpdated, statusDescription } = data.project; + return { projectId, title, acronym, description, startDateString, endDateString, lastUpdated, statusDescription }; + }); + + const projects = await Promise.all(projectPromises); + allProjects.push(...projects); + + // Update state after processing each chunk + setPaginationProjects((prev) => [...prev, ...projects]); + setProjects((prev) => [...prev, ...projects]); + localStorage.setItem("projects", JSON.stringify(allProjects)); } - setPaginationProjects((prev) => [...temp]); - setProjects((prev) => [...temp]); - localStorage.setItem("projects", JSON.stringify(temp)); - setTimeout(() => setIsLoading(false), 2000) - + + setTimeout(() => setIsLoading(false), 2000); + } catch (error) { - setTimeout(() => setIsLoading(false), 2000) + setTimeout(() => setIsLoading(false), 2000); console.log(error.message); } } From f34f88367b6b9c2ae10dfa1dc1c6b3ca851ebe7d Mon Sep 17 00:00:00 2001 From: sujal <117587914+sujal-98@users.noreply.github.com> Date: Fri, 31 May 2024 15:21:09 +0530 Subject: [PATCH 2/2] Update NasaProjects.jsx --- src/pages/NasaProjects/NasaProjects.jsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pages/NasaProjects/NasaProjects.jsx b/src/pages/NasaProjects/NasaProjects.jsx index b38c177..9127ca1 100644 --- a/src/pages/NasaProjects/NasaProjects.jsx +++ b/src/pages/NasaProjects/NasaProjects.jsx @@ -117,7 +117,7 @@ function TechNews() { const allProjects = []; for (const chunk of projectChunks) { - const projectPromises = chunk.map(async (id) => { + const projectPromises = chunk.map(async (id) => { //using map for concurrent execution of data let response = await fetch(`https://inplace-ghib.onrender.com/inplace`, { method: "POST", body: JSON.stringify({ @@ -132,7 +132,7 @@ function TechNews() { return { projectId, title, acronym, description, startDateString, endDateString, lastUpdated, statusDescription }; }); - const projects = await Promise.all(projectPromises); + const projects = await Promise.all(projectPromises); //waits for all promise to resolve allProjects.push(...projects); // Update state after processing each chunk @@ -268,4 +268,4 @@ function TechNews() { ) } -export default TechNews \ No newline at end of file +export default TechNews