fix(marketplace): fix product date format and improve UX
- Convert expires_at to ISO 8601 format for backend compatibility - Update local state instead of reloading list on edit/create/delete - Improve delete error message for products with related orders
This commit is contained in:
parent
00685f7b26
commit
6c0b4c4cd6
1 changed files with 29 additions and 7 deletions
|
|
@ -51,21 +51,35 @@ export function ProductsPage() {
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
try {
|
try {
|
||||||
|
// Convert date to ISO 8601 format for backend
|
||||||
|
const expiresAtISO = formData.expires_at
|
||||||
|
? new Date(formData.expires_at + 'T00:00:00Z').toISOString()
|
||||||
|
: undefined
|
||||||
|
|
||||||
if (editingProduct) {
|
if (editingProduct) {
|
||||||
await adminService.updateProduct(editingProduct.id, {
|
const updated = await adminService.updateProduct(editingProduct.id, {
|
||||||
name: formData.name,
|
name: formData.name,
|
||||||
description: formData.description,
|
description: formData.description,
|
||||||
batch: formData.batch,
|
batch: formData.batch,
|
||||||
expires_at: formData.expires_at,
|
expires_at: expiresAtISO,
|
||||||
price_cents: formData.price_cents,
|
price_cents: formData.price_cents,
|
||||||
stock: formData.stock
|
stock: formData.stock
|
||||||
})
|
})
|
||||||
|
// Update local state instead of reloading
|
||||||
|
setProducts(prev => prev.map(p =>
|
||||||
|
p.id === editingProduct.id ? { ...p, ...updated } : p
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
await adminService.createProduct(formData)
|
const newProduct = await adminService.createProduct({
|
||||||
|
...formData,
|
||||||
|
expires_at: expiresAtISO || ''
|
||||||
|
})
|
||||||
|
// Add to local state
|
||||||
|
setProducts(prev => [newProduct, ...prev])
|
||||||
|
setTotal(prev => prev + 1)
|
||||||
}
|
}
|
||||||
setShowModal(false)
|
setShowModal(false)
|
||||||
resetForm()
|
resetForm()
|
||||||
loadProducts()
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error saving product:', err)
|
console.error('Error saving product:', err)
|
||||||
alert('Erro ao salvar produto')
|
alert('Erro ao salvar produto')
|
||||||
|
|
@ -76,10 +90,18 @@ export function ProductsPage() {
|
||||||
if (!confirm('Tem certeza que deseja excluir este produto?')) return
|
if (!confirm('Tem certeza que deseja excluir este produto?')) return
|
||||||
try {
|
try {
|
||||||
await adminService.deleteProduct(id)
|
await adminService.deleteProduct(id)
|
||||||
loadProducts()
|
// Update local state instead of reloading
|
||||||
} catch (err) {
|
setProducts(prev => prev.filter(p => p.id !== id))
|
||||||
|
setTotal(prev => prev - 1)
|
||||||
|
} catch (err: unknown) {
|
||||||
console.error('Error deleting product:', err)
|
console.error('Error deleting product:', err)
|
||||||
alert('Erro ao excluir produto')
|
// Check if it's a constraint error (product has orders)
|
||||||
|
const message = (err as { response?: { data?: { error?: string } } })?.response?.data?.error
|
||||||
|
if (message?.includes('related orders')) {
|
||||||
|
alert('Não é possível excluir: este produto possui pedidos relacionados.')
|
||||||
|
} else {
|
||||||
|
alert('Erro ao excluir produto')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue