Skip to content

Commit

Permalink
refactorizo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignaherrero committed May 3, 2022
1 parent 6a7944e commit 64e1376
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
32 changes: 15 additions & 17 deletions components/pagination/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from "react";
import { statusFetch } from "../../helper/dictionary";
import { getOtherPage } from "../../helper/fetchs";

export default function Pagination({
articles,
Expand All @@ -9,27 +10,24 @@ export default function Pagination({
increment,
decrement,
}) {
const { found, pending } = statusFetch;

useEffect(() => {
const getOtherPage = async () => {
const getData = async () => {
if (getValues("article")?.length > 0) {
setArticles({ data: [], found: statusFetch.pending });
try {
let response = await fetch(
`https://beta.mejorconsalud.com/wp-json/mc/v3/posts?search=${getValues(
"article"
)}${
getValues("relevant") ? `&orderby=${getValues("relevant")}` : ""
}&page=${counter}`
);
let data = await response.json();
setArticles({ ...data, found: statusFetch.found });
} catch (err) {
console.log(err);
}
setArticles({ data: [], found: pending });
const args = {
relevant: getValues("relevant"),
article: getValues("article"),
counter,
};
const data = await getOtherPage(args);
const { data: response } = data;
setArticles({ ...response, found: found });
}
};

getOtherPage();
getData();
}, [counter, getValues, setArticles]);

return (
Expand All @@ -42,7 +40,7 @@ export default function Pagination({
data-mdb-ripple="true"
data-mdb-ripple-color="light"
className="inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs leading-tight uppercase rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out"
disabled={articles.found === statusFetch.pending}
disabled={articles.found === pending}
onClick={() => {
decrement();
}}
Expand Down
32 changes: 31 additions & 1 deletion helper/fetchs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,34 @@ const getAllLastArticles = async () => {
return data;
};

export { searchForArticles, getAllLastArticles };
const getOtherPage = async ({ article, counter, relevant }) => {
const data = {};
const params = {};
try {
if (relevant) {
params = {
search: article,
orderby: relevant,
page: counter,
};
} else {
params = {
search: article,
page: counter,
};
}
data = await axios({
method: "get",
url: `https://beta.mejorconsalud.com/wp-json/mc/v3/posts?`,
params: {
...params,
},
responseType: "json",
});
} catch (err) {
console.log(err);
}
return data;
};

export { searchForArticles, getAllLastArticles, getOtherPage };

1 comment on commit 64e1376

@vercel
Copy link

@vercel vercel bot commented on 64e1376 May 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

challenge-atomik – ./

challenge-atomik.vercel.app
challenge-atomik-git-main-nacho93.vercel.app
challenge-atomik-nacho93.vercel.app

Please sign in to comment.