Skip to content

Commit

Permalink
Fixes #208 - The timeout that was causing the bug when opening the pr…
Browse files Browse the repository at this point in the history
…oduct detail page was increased and the entire ProductDetail file was error handled
  • Loading branch information
jandsonrj committed Dec 8, 2023
1 parent 4f63d5c commit 0fe0be2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
65 changes: 30 additions & 35 deletions frontend/components/ProductDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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])
Expand All @@ -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)
})
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 0fe0be2

Please sign in to comment.