From a1a87c2eac9a222476ebe57263eece10f732cc3a Mon Sep 17 00:00:00 2001 From: Daniel Martinez Date: Sun, 6 Sep 2020 17:01:53 -0500 Subject: [PATCH] Se mejoran fetch de los services para manejar mejor el modal de error --- src/components/Modals/FailureModal.jsx | 2 +- src/components/Modals/SuccessModal.jsx | 2 +- src/components/Products/Product.jsx | 31 +++++++++++------- src/services/productsService.jsx | 45 +++++++++++++------------- src/services/userService.jsx | 38 +++++++++++----------- 5 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/components/Modals/FailureModal.jsx b/src/components/Modals/FailureModal.jsx index 91a5bcc..ac0b4ee 100644 --- a/src/components/Modals/FailureModal.jsx +++ b/src/components/Modals/FailureModal.jsx @@ -28,7 +28,7 @@ const FailureModal = ({ isShowing, hide }) =>
error -
+

ERROR!

Something went wrong, try again
diff --git a/src/components/Modals/SuccessModal.jsx b/src/components/Modals/SuccessModal.jsx index 08caa08..1d7b685 100644 --- a/src/components/Modals/SuccessModal.jsx +++ b/src/components/Modals/SuccessModal.jsx @@ -29,7 +29,7 @@ const SuccessModal = ({ isShowing, hide }) =>
error -
+

SUCCESS!

Everything went ok!, enjoy your product
diff --git a/src/components/Products/Product.jsx b/src/components/Products/Product.jsx index 52ac3ed..5bacd40 100644 --- a/src/components/Products/Product.jsx +++ b/src/components/Products/Product.jsx @@ -1,4 +1,4 @@ -import React, { useContext, useEffect } from "react"; +import React, { useContext, useEffect, useState } from "react"; import { UserContext } from "../../providers/index"; import { coin, buy } from "../../assets/index"; import { SuccessModal, FailureModal } from "../Modals/index"; @@ -17,27 +17,36 @@ const Product = ({ }) => { const user = useContext(UserContext); - const { isShowing, toggle } = useModal(); - useEffect(() => { if (!user) return; }, [user]); + const [error, setError] = useState(false); + + const { isShowing, toggle } = useModal(); + const { points } = user; const canBuy = cost <= points; - const openBuyModal = () => { - redeemProduct(_id).then((res) => { - // toggle(); - console.log(res); - }); + const showModal = () => { + redeemProduct(_id) + .then((res) => { + toggle(); + }) + .catch((err) => { + setError(true); + toggle(); + }); }; return (
- - {/* */} + {error ? ( + + ) : ( + + )}
@@ -53,7 +62,7 @@ const Product = ({

{cost}

coin - +
diff --git a/src/services/productsService.jsx b/src/services/productsService.jsx index 454f4ce..3ab7050 100644 --- a/src/services/productsService.jsx +++ b/src/services/productsService.jsx @@ -5,29 +5,28 @@ const headers = { Authorization: `Bearer ${API.AUTH_TOKEN}`, }; -export const getProducts = async () => { - try { - const response = await fetch(API.PRODUCTS, { headers }); - const json = await response.json(); - return json; - } catch (error) { - return alert(JSON.stringify(error)); - } +export const getProducts = () => { + return fetch(API.PRODUCTS, { headers }) + .then((response) => { + if (!response.ok) { + throw Error(response.statusText); + } + return response; + }) + .then((response) => response.json()); }; -export const redeemProduct = async (id) => { - try { - console.log(id); - const response = await fetch(API.REDEEEM, { - headers, - method: "POST", - body: JSON.stringify({ productId: id }), - }); - console.log("request", response); - const json = await response.json(); - return json; - } catch (error) { - console.log("Error", error); - return alert(JSON.stringify(error)); - } +export const redeemProduct = (id) => { + return fetch(API.REDEEEM, { + headers, + method: "POST", + body: JSON.stringify({ productId: id }), + }) + .then((response) => { + if (!response.ok) { + throw Error(response.statusText); + } + return response; + }) + .then((response) => response.json()); }; diff --git a/src/services/userService.jsx b/src/services/userService.jsx index 697a55a..6fb352d 100644 --- a/src/services/userService.jsx +++ b/src/services/userService.jsx @@ -6,25 +6,27 @@ const headers = { }; export const getUser = async () => { - try { - const response = await fetch(API.USER, { headers }); - const json = await response.json(); - return json; - } catch (error) { - return alert(JSON.stringify(error)); - } + return fetch(API.USER, { headers }) + .then((response) => { + if (!response.ok) { + throw Error(response.statusText); + } + return response; + }) + .then((response) => response.json()); }; export const addPoints = async (coins) => { - try { - const response = await fetch(API.ADD_POINTS, { - method: "POST", - body: JSON.stringify({ amount: coins }), - headers, - }); - const json = await response.json(); - return json; - } catch (error) { - return alert(JSON.stringify(error)); - } + return fetch(API.ADD_POINTS, { + headers, + method: "POST", + body: JSON.stringify({ amount: coins }), + }) + .then((response) => { + if (!response.ok) { + throw Error(response.statusText); + } + return response; + }) + .then((response) => response.json()); };