From 0fe0be2925f2f56f788ee3ce11f8b9b9e6fc3628 Mon Sep 17 00:00:00 2001 From: Jandson Vitorino Date: Fri, 8 Dec 2023 17:28:39 -0300 Subject: [PATCH] Fixes #208 - The timeout that was causing the bug when opening the product detail page was increased and the entire ProductDetail file was error handled --- frontend/components/ProductDetail.js | 65 +++++++++++++--------------- frontend/services/api.js | 2 +- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/frontend/components/ProductDetail.js b/frontend/components/ProductDetail.js index 0a7fef7..1e53e9c 100644 --- a/frontend/components/ProductDetail.js +++ b/frontend/components/ProductDetail.js @@ -65,11 +65,12 @@ export default function ProductDetail({ productId, internalName }) { setProduct(res) setLoading(false) }) - .catch(res => { + .catch(error => { setLoading(false) - if (res.response.status === 500) { - // TODO: Tratar erro - setNotFound(true) + if (error.response && error.response.status === 500) { + console.error('Internal Server Error:', error.response.data) + } else { + console.error('Error loading product by ID:', error.message) } }) }, [productId]) @@ -82,38 +83,30 @@ export default function ProductDetail({ productId, internalName }) { const loadProductByName = React.useCallback(async () => { setLoading(true) - getProducts({ filters: { internal_name: internalName } }) - .then(res => { - // Apresenta a interface de Produtos - if (res.results.length === 1) { - setLoading(false) - setProduct(res) - } - if (res.results.length === 0) { - setNotFound(true) - } - }) - .catch(res => { - setLoading(false) - if (res.response.status === 500) { - // TODO: Tratar erro - setNotFound(true) - } + + try { + const res = await getProducts({ + filters: { internal_name: internalName } }) - getProducts({ filters: { internal_name: internalName } }) - .then(res => { - // Apresenta a interface de Produtos - if (res.results.length === 1) { - setProduct(res.results[0]) - } + if (res.results.length === 1) { setLoading(false) - }) - .catch(res => { - // Retorna error 404 - // TODO: Tratar os errors e apresentar. + setProduct(res.results[0]) + } else if (res.results.length === 0) { setLoading(false) - }) + setNotFound(true) + } + } catch (error) { + setLoading(false) + + if (error.response && error.response.status === 404) { + console.error('Product not found:', error.response.data) + } else if (error.response && error.response.status === 500) { + console.error('Internal Server Error:', error.response.data) + } else { + console.error('Error loading product by name:', error.message) + } + } }, [internalName]) React.useEffect(() => { @@ -146,7 +139,9 @@ export default function ProductDetail({ productId, internalName }) { .catch(error => { setLoading(false) if (error.response && error.response.status === 500) { - // TODO: Tratar erro + console.error('Internal Server Error:', error.response.data) + } else { + console.error('Error loading product files:', error.message) } }) }, [product]) @@ -169,8 +164,8 @@ export default function ProductDetail({ productId, internalName }) { link.click() setDownloading(false) }) - .catch(() => { - // TODO: Tratar erro no download + .catch(error => { + console.error('Error downloading file:', error.message) setDownloading(false) }) } diff --git a/frontend/services/api.js b/frontend/services/api.js index 10303ae..2b4ae84 100644 --- a/frontend/services/api.js +++ b/frontend/services/api.js @@ -10,7 +10,7 @@ import { refreshToken } from './auth' export function getAPIClient(ctx) { const api = axios.create({ // baseURL: '/api', - timeout: 15000, + timeout: 30000, headers: { 'Content-Type': 'application/json', accept: 'application/json'