From bdd874df1e48e7af22660c4b7465268629c23a96 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 3 May 2022 18:49:14 -0300 Subject: [PATCH] refactor --- components/card/index.jsx | 76 ++++++++++++++++++------------------- components/search/index.jsx | 41 ++++++++------------ helper/fetchs.js | 50 ++++++++++++++++++++++++ helper/useFetch.js | 41 ++++++++++++++++++++ package.json | 7 +++- 5 files changed, 150 insertions(+), 65 deletions(-) create mode 100644 helper/fetchs.js create mode 100644 helper/useFetch.js diff --git a/components/card/index.jsx b/components/card/index.jsx index d69e697..c6b8561 100644 --- a/components/card/index.jsx +++ b/components/card/index.jsx @@ -2,10 +2,11 @@ import Image from "next/image"; import Link from "next/link"; import { statusFetch } from "../../helper/dictionary"; -export default function Card({ articles }) { +export default function Card({ articles: { data, found } }) { + const { notFound } = statusFetch; return ( <> - {articles.found === statusFetch.notFound && ( + {found === notFound && ( <>

No se encontro el articulo... pero estos son los ultimos articulos @@ -15,44 +16,43 @@ export default function Card({ articles }) { )}
- {articles?.data && - articles.data.map((article) => { - return ( -
- - - -
-
- {article.title} -
-

- {article.categories[0].description} -

-
- - Leer mas - + {data.length > 0 && + data.map( + ({ featured_media: { medium_large }, title, id, categories }) => { + const {description}=categories[0]; + return ( +
+ + + +
+
+ {title} +
+

+ {description} +

+
+ + Leer mas + +
-
- ); - })} + ); + } + )}
); diff --git a/components/search/index.jsx b/components/search/index.jsx index 5cec84b..4f8bf71 100644 --- a/components/search/index.jsx +++ b/components/search/index.jsx @@ -1,4 +1,5 @@ import { statusFetch } from "../../helper/dictionary"; +import { getAllLastArticles, searchForArticles } from "../../helper/fetchs"; export default function Search({ setArticles, @@ -7,30 +8,22 @@ export default function Search({ register, reset, }) { + const { pending, found, notFound } = statusFetch; + const onSubmit = async (dataForm) => { - if (statusFetch.pending) { - setArticles({ data: [], found: statusFetch.pending }); - try { - let response = await fetch( - `https://beta.mejorconsalud.com/wp-json/mc/v3/posts?search=${ - dataForm.article - }${dataForm.relevant ? `&orderby=${dataForm.relevant}` : ""}` - ); - let data = await response.json(); - if (data.size > 0) { - setArticles({ ...data, found: statusFetch.found }); - } else { - response = await fetch( - "https://beta.mejorconsalud.com/wp-json/mc/v3/posts?orderby=date&order=desc" - ); - data = await response.json(); - setArticles({ ...data, found: statusFetch.notFound }); - } - reset(); - } catch (err) { - console.log(err); - } + if (pending) { + setArticles({ data: [], found: pending }); + } + const data = await searchForArticles(dataForm); + const { data: response } = data; + if (response.data.length > 0) { + setArticles({ ...response, found: found }); + } else { + data = await getAllLastArticles(); + const { data: response } = data; + setArticles({ ...response, found: notFound }); } + reset(); }; return ( <> @@ -87,9 +80,7 @@ export default function Search({
- {articles.found === statusFetch.pending && ( -

cargando...

- )} + {articles.found === pending &&

cargando...

} ); } diff --git a/helper/fetchs.js b/helper/fetchs.js new file mode 100644 index 0000000..ccba5ee --- /dev/null +++ b/helper/fetchs.js @@ -0,0 +1,50 @@ +import axios from "axios"; + +const searchForArticles = async ({ article, relevant }) => { + const data = {}; + const params = {}; + try { + if (relevant) { + params = { + search: article, + orderby: relevant, + }; + } else { + params = { + search: article, + }; + } + 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; +}; + +const getAllLastArticles = async () => { + const data = {}; + try { + data = await axios({ + method: "get", + url: `https://beta.mejorconsalud.com/wp-json/mc/v3/posts?`, + params: { + orderby: "date", + order: "desc", + }, + responseType: "json", + }); + } catch (err) { + console.log(err); + } + return data; +}; + +export { searchForArticles, getAllLastArticles }; diff --git a/helper/useFetch.js b/helper/useFetch.js new file mode 100644 index 0000000..49566d4 --- /dev/null +++ b/helper/useFetch.js @@ -0,0 +1,41 @@ +import { useState, useEffect, useRef } from "react"; + +export const useFetch = (url) => { + const isMounted = useRef(true); + const [state, setState] = useState({ + data: null, + loading: true, + error: null, + }); + + useEffect(() => { + return () => { + isMounted.current = false; + }; + }, []); + + useEffect(() => { + setState({ data: null, loading: true, error: null }); + + fetch(url) + .then((resp) => resp.json()) + .then((data) => { + if (isMounted.current) { + setState({ + loading: false, + error: null, + data, + }); + } + }) + .catch(() => { + setState({ + loading: false, + error: "no hay datos", + data: null, + }); + }); + }, [url]); + + return state; +}; diff --git a/package.json b/package.json index 5a26dac..a0d77c5 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,15 @@ "version": "0.1.0", "private": true, "scripts": { - "dev": "next dev -p 9045", + "dev": "npx next dev -p 9045", "build": "next build", "start": "next start", - "lint": "next lint" + "lint": "next lint", + "cypress": "cypress open", + "lint-fix": "eslint --fix --ext .js,.jsx ." }, "dependencies": { + "axios": "^0.27.2", "html-react-parser": "^1.4.10", "moment": "^2.29.2", "next": "12.1.4",