diff --git a/pages/index.jsx b/pages/index.jsx index 38c6b74..5062a38 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -1,22 +1,42 @@ import Link from "next/link"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useForm } from "react-hook-form"; + export default function Home() { const [articles, setArticles] = useState({ found: "pending" }); - + const [page, setPage] = useState(1); const { register, handleSubmit, formState: { errors }, + getValues } = useForm(); + useEffect(() => { + console.log("entro") + setArticles({ found: "pending" }); + const getOtherPage = async () => { + console.log(getValues("article")) + try { + let response = await fetch( + `https://beta.mejorconsalud.com/wp-json/mc/v3/posts?search=${getValues("article")}&orderby=relevance&page=${page}` + ); + let data = await response.json(); + setArticles({ ...data, found: "found" }); + } catch (err) { + console.log(err); + } + } + getOtherPage() + }, [page]); + const onSubmit = async (dataForm) => { try { let response = await fetch( `https://beta.mejorconsalud.com/wp-json/mc/v3/posts?search=${ dataForm.article - }${dataForm.relevant ? `&relevant=${dataForm.relevant}` : ""}` + }${dataForm.relevant ? `&orderby=${dataForm.relevant}` : ""}` ); let data = await response.json(); if (data.size > 0) { @@ -88,7 +108,6 @@ export default function Home() { - {articles.found === "notfound" && ( <> @@ -108,6 +127,35 @@ export default function Home() { ); })} + + {/* */} +
+ + {page} + +
); }