Skip to content

Commit

Permalink
Add fetching logic to API components. It will keep calling the API un…
Browse files Browse the repository at this point in the history
…til it retrieves all values
  • Loading branch information
Pipexlul committed Jan 18, 2023
1 parent a826fdc commit d0497fd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
17 changes: 13 additions & 4 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ function App() {
if (mainBlock) {
const finalApiValues = [];

const finalApiData = {
resultsField: value.resultsField,
continueField: value.continueField,
apiValues: finalApiValues,
};

if (!initialData.apidata[value.apiName]) {
initialData.apidata[value.apiName] = finalApiValues;
initialData.apidata[value.apiName] = finalApiData;
}

if (!initialData.apidata[fileName]) {
initialData.apidata[fileName] = finalApiValues;
initialData.apidata[fileName] = finalApiData;
}

value.endpoints.forEach((item) => {
Expand All @@ -121,10 +127,13 @@ function App() {

if (fileName === "General") {
initialData.howtouse.push(value);
} else if (mainBlock && initialData.apidata[fileName].length > 0) {
} else if (
mainBlock &&
initialData.apidata[fileName].apiValues.length > 0
) {
const dataCopy = structuredClone(value);

initialData.apidata[fileName].forEach((item) => {
initialData.apidata[fileName].apiValues.forEach((item) => {
if (item.enabled) {
const fieldsObjects = item.fields.map((field) => {
return {
Expand Down
56 changes: 41 additions & 15 deletions src/components/APIHandler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import Separator from "./Separator";

const APIHandler = ({ mainData, apiData }) => {
const [limitedResults, setLimitedResults] = useState(0);
const [selectedAPI, setSelectedAPI] = useState({ idx: -1, apiName: "" });
const [selectedAPI, setSelectedAPI] = useState({
idx: -1,
apiName: "",
apiResultsField: "",
apiContinueField: "",
});
const [canClick, setCanClick] = useState(true);
const [endpoints, setEndpoints] = useState([]);
const [endpointURL, setEndpointURL] = useState("");
Expand All @@ -21,7 +26,7 @@ const APIHandler = ({ mainData, apiData }) => {
const apiBlock = apiData[selectedAPI.apiName];

if (apiBlock) {
setEndpoints(apiBlock);
setEndpoints(apiBlock.apiValues);
}
}
}, [selectedAPI.idx]);
Expand All @@ -38,21 +43,41 @@ const APIHandler = ({ mainData, apiData }) => {

useEffect(() => {
if (endpointURL) {
console.log("Fetching", endpointURL);
const fetchResults = async () => {
try {
const response = await fetch(endpointURL);
console.log(response);
const data = await response.json();
console.log(data);

setResults(data.results);
} catch (error) {
console.error(error);
const urlCopy = endpointURL;

const fetchResults = async (url) => {
let finalResults = [];

while (url) {
console.log("Fetching", url);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`HTTP error! status: ${response.status} when fetching ${url}`
);
}
const data = await response.json();
console.log(data);

finalResults = finalResults.concat(
data[selectedAPI.apiResultsField]
);

if (selectedAPI.apiContinueField) {
url = data[selectedAPI.apiContinueField];
} else {
url = null;
}
} catch (error) {
throw error;
}
}

return finalResults;
};

fetchResults();
fetchResults(urlCopy).then(setResults).catch(console.error);
}
}, [endpointURL]);

Expand All @@ -68,7 +93,8 @@ const APIHandler = ({ mainData, apiData }) => {
Selecciona una API
</h2>
<APIChooser
apiData={Object.values(mainData)}
mainData={Object.values(mainData)}
apiData={apiData}
selectedAPI={selectedAPI}
setSelectedAPI={setSelectedAPI}
/>
Expand Down
7 changes: 4 additions & 3 deletions src/components/APIHandlerLogic/APIChooser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { useContext, useState, useEffect } from "react";
import { APIContext } from "../../contexts/appContexts";
import APIChooserButton from "./APIChooserButton";

const APIChooser = ({ apiData, selectedAPI, setSelectedAPI }) => {
const APIChooser = ({ mainData, apiData, selectedAPI, setSelectedAPI }) => {
return (
<ul className="mb-0 flex list-none flex-row flex-wrap gap-y-2 pt-3 pb-4">
{apiData.length > 0 &&
apiData.map((entry, index) => {
{mainData.length > 0 &&
mainData.map((entry, index) => {
return (
<APIChooserButton
key={index}
apiName={entry.name}
apiData={apiData}
selected={selectedAPI.idx === index}
clickHandler={setSelectedAPI}
idx={index}
Expand Down
17 changes: 15 additions & 2 deletions src/components/APIHandlerLogic/APIChooserButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const APIChooserButton = ({ apiName, selected, clickHandler, idx }) => {
const APIChooserButton = ({
apiName,
apiData,
selected,
clickHandler,
idx,
}) => {
return (
<li className="-mb-px mr-2 flex-auto text-center last:mr-0">
<a
Expand All @@ -9,7 +15,14 @@ const APIChooserButton = ({ apiName, selected, clickHandler, idx }) => {
}`}
onClick={(e) => {
e.preventDefault();
clickHandler({ idx, apiName });

const { resultsField, continueField } = apiData[apiName];
clickHandler({
idx,
apiName,
apiResultsField: resultsField,
apiContinueField: continueField,
});
}}
href="#"
>
Expand Down
2 changes: 2 additions & 0 deletions src/data/APIData/StarWars.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const data = {
apiName: "Star Wars",
resultsField: "results",
continueField: "next",
endpoints: [
{
name: "Peliculas",
Expand Down

0 comments on commit d0497fd

Please sign in to comment.