Question: Hello - Please review the below code. I don't think that the state is being persisted. Picture also not working. ( : Requirements are: Complete

Hello - Please review the below code. I don't think that the state is being persisted. Picture also not working. (:
Requirements are:
Complete the post, put and delete requests using mongo to make the changes to the data persistent.
Pictures should work for the whole session.
import React, { useState } from 'react';
const DataForm =({ addNewItem, updateItem, deleteItem })=>{
const [name, setName]= useState('');
const [brand, setBrand]= useState('');
const [price, setPrice]= useState('');
const [image, setImage]= useState(null);
const [error, setError]= useState(null);
const [success, setSuccess]= useState(false);
const handleImageChange =(e)=>{
const file = e.target.files[0];
if (file){
setImage(file);
}
};
const handleSubmit = async (e)=>{
e.preventDefault();
if (!name ||!brand ||!price ||!image){
setError('All fields, including an image, are required');
setSuccess(false);
return;
}
const priceNumber = parseFloat(price);
if (isNaN(priceNumber)){
setError('Price must be a valid number');
setSuccess(false);
return;
}
const formData = new FormData();
formData.append('name', name);
formData.append('brand', brand);
formData.append('price', priceNumber);
formData.append('gear', image);
setError(null);
try {
const response = await fetch('My URL', {
method: 'POST',
body: formData,
});
if (!response.ok){
throw new Error('Server error');
}
const data = await response.json();
if (data.success){
setSuccess(true);
addNewItem(data.newItem);
// Reset form
setName('');
setBrand('');
setPrice('');
setImage(null);
} else {
setError(data.message || 'Failed to add the item');
setSuccess(false);
}
} catch (err){
setError('An error occurred while submitting the form');
setSuccess(false);
}
};
const handleUpdate = async (itemId, updatedData)=>{
try {
const response = await fetch(`My URL/${itemId}`,{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updatedData),
});
const data = await response.json();
if (data.success){
updateItem(data.updatedItem);
setSuccess(true);
} else {
setError(data.message || 'Failed to update the item');
setSuccess(false);
}
} catch (err){
setError('An error occurred while updating the item');
setSuccess(false);
}
};
const handleDelete = async (itemId)=>{
try {
const response = await fetch(`My URL/${itemId}`,{
method: 'DELETE',
});
const data = await response.json();
if (data.success){
deleteItem(itemId);
setSuccess(true);
} else {
setError(data.message || 'Failed to delete the item');
setSuccess(false);
}
} catch (err){
setError('An error occurred while deleting the item');
setSuccess(false);
}
};
return (

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!