Skip to content

Commit

Permalink
added product update consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
edmdz committed Jun 22, 2024
1 parent 78e1568 commit c908e48
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/models/Price.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default class Price {
constructor({description, cost}) {
constructor({id, description, cost}) {
this.id = id;
this.description = description;
this.cost = cost;
}
Expand Down
41 changes: 28 additions & 13 deletions src/pages/Products/ProductDialog/ProductDialogContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useProductsContext } from '../ProductsContext';
import { getBase64ImgFromURL, getImageURLFromStorage, uploadImageToStorage } from '../../../services/Firebase/storage';
import { base64ToBlob, getMimeType, modifyAndClone } from '../../../util/generalUtils';
import File from '../../../models/File';
import { createProduct, getProductById } from '../../../services/ProductService';
import { createProduct, getProductById, updateProduct } from '../../../services/ProductService';
import { useSnackbar } from '../../../components/SnackbarContext';
import Product from '../../../models/Product';
import { FileTypes, ProductTypes } from '../ProductsConstants';
Expand Down Expand Up @@ -100,22 +100,37 @@ export const ProductDialogProvider = ({ children }) => {
try {
setIsLoading(true);

const productToCreate = {
...product,
productTypeId: productType,
files: product.files.map(file => ({ ...file, fileData: null }))
};

await createProduct(productToCreate);

product.files.forEach((file) => {
uploadImageToStorage(base64ToBlob(file.fileData), file.storagePath);
});
if (product.id) {
console.log(product)
const productToUpdate = {
...product,
productTypeId: productType,
files: product.files.map(file => ({ ...file, fileData: null }))
};
product.files.forEach((file) => {
if (!file.id) {
uploadImageToStorage(base64ToBlob(file.fileData), file.storagePath);
}
});
await updateProduct(productToUpdate.id, productToUpdate);
} else {
const productToCreate = {
...product,
productTypeId: productType,
files: product.files.map(file => ({ ...file, fileData: null }))
};

await createProduct(productToCreate);

product.files.forEach((file) => {
uploadImageToStorage(base64ToBlob(file.fileData), file.storagePath);
});
}

setIsLoading(false);
handleCloseDialog();
resetState();
openSnackbar('Producto creado exitosamente', 'success');
openSnackbar('Producto procesado correctamente', 'success');
} catch (error) {
setIsLoading(false);
console.log(error)
Expand Down
7 changes: 6 additions & 1 deletion src/services/ProductService.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ const deleteProduct = async (productId) => {
return response.statusCode === 204;
}

export { createProductVehicles, createProductPrices, getProductPrices,
const updateProduct = async (productId, product) => {
const response = await axiosInstance.put(`/product/${productId}`, product);
return response.data;
}

export { createProductVehicles, createProductPrices, getProductPrices, updateProduct,
getProductVehicleModels, createProductFiles, createProduct, getProductById, deleteProduct }

0 comments on commit c908e48

Please sign in to comment.