Fix: Reemplazo y Limpieza de Archivos Img de Avisos.
This commit is contained in:
@@ -38,6 +38,7 @@ export default function FormularioAviso({
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [photos, setPhotos] = useState<PhotoSource[]>([]);
|
const [photos, setPhotos] = useState<PhotoSource[]>([]);
|
||||||
|
const [deletedPhotoIds, setDeletedPhotoIds] = useState<number[]>([]);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
// Estados Maestros
|
// Estados Maestros
|
||||||
@@ -490,6 +491,16 @@ export default function FormularioAviso({
|
|||||||
|
|
||||||
if (adId) {
|
if (adId) {
|
||||||
await AdsV2Service.update(adId, adPayload);
|
await AdsV2Service.update(adId, adPayload);
|
||||||
|
|
||||||
|
// 1. Eliminar fotos que el usuario quitó
|
||||||
|
if (deletedPhotoIds.length > 0) {
|
||||||
|
for (const photoId of deletedPhotoIds) {
|
||||||
|
await AdsV2Service.deletePhoto(photoId);
|
||||||
|
}
|
||||||
|
setDeletedPhotoIds([]); // Limpiar después de borrar
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Subir fotos nuevas
|
||||||
if (newPhotos.length > 0) {
|
if (newPhotos.length > 0) {
|
||||||
await AdsV2Service.uploadPhotos(adId, newPhotos);
|
await AdsV2Service.uploadPhotos(adId, newPhotos);
|
||||||
}
|
}
|
||||||
@@ -1251,9 +1262,13 @@ export default function FormularioAviso({
|
|||||||
<div className="absolute inset-0 bg-black/60 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center backdrop-blur-[2px]">
|
<div className="absolute inset-0 bg-black/60 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center backdrop-blur-[2px]">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() =>
|
onClick={() => {
|
||||||
setPhotos(photos.filter((_, i) => i !== idx))
|
const photoToDelete = photos[idx];
|
||||||
|
if (!(photoToDelete instanceof File) && photoToDelete.id) {
|
||||||
|
setDeletedPhotoIds((prev) => [...prev, photoToDelete.id]);
|
||||||
}
|
}
|
||||||
|
setPhotos(photos.filter((_, i) => i !== idx));
|
||||||
|
}}
|
||||||
className="w-10 h-10 bg-red-500/20 hover:bg-red-500 text-red-200 hover:text-white rounded-full flex items-center justify-center transition-all border border-red-500/50"
|
className="w-10 h-10 bg-red-500/20 hover:bg-red-500 text-red-200 hover:text-white rounded-full flex items-center justify-center transition-all border border-red-500/50"
|
||||||
title="Eliminar foto"
|
title="Eliminar foto"
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user