Question: Feedback received: Data not loaded in Edit dialog ( an edit dialog should be used that imports all the current information as the input values
Feedback received: Data not loaded in Edit dialog an edit dialog should be used that imports all the current information as the input values so that you can edit rather than starting fresh as if you're creating a new one
Data not automatically updating.
All of the items in the json should be able to be edited and deleted. This would allow to quickly change things on the site without having to edit your json. For this, each item should have it's own editdelete buttons, to access their ids and change the info stored.
How can we incorporate the changes being requested? Thank you so much in advance.
import React, useState from 'react';
const DataForm addNewItem
const name setName useState;
const brand setBrand useState;
const price setPrice useState;
const image setImage useStatenull;
const error setError useStatenull;
const success setSuccess useStatefalse;
const handlelmageChange e
const file etarget.files;
if file
setImagefile; Save the selected file in state
;
const handleSubmit async e
epreventDefault;
if name brand price image
setErrorAll fields, including an image, are required';
setSuccessfalse;
return;
const priceNumber parseFloatprice;
if isNaNpriceNumber
setErrorPrice must be a valid number'; setSuccessfalse;
return;
const formData new FormData;
formData.appendname name;
formData.appendbrand brand;
formData.appendprice priceNumber;
formData.appendgear image;
setErrornull;
try
const response await fetchMy url',
method: 'POST',
body: formData,
;
if response.ok
throw new ErrorServer error';
const data await response.json;
if datasuccess
setSuccesstrue;
addNewItemdatanewItem;
Reset form
setName;
setBrand;
setPrice;
setImagenull;
document.getElementByIdfileInputvalue ; Reset file input
else
setErrordatamessage 'Failed to add the item';
setSuccessfalse;
catch err
setErrorAn error occurred while submitting the form';
setSuccessfalse;
;
const handleUpdate async itemld updatedData
try
const response await fetchMy url$itemid
method: 'POST',
headers: 'ContentType': 'applicationjson
body: JSON.stringifyupdatedData
;
const data await response.json;
if datasuccess
updateltemdataupdatedItem;
setSuccesstrue;
else
setErrordatamessage 'Failed to update the item';
setSuccessfalse;
catch err
setErrorAn error occurred while updating the item';
setSuccessfalse;
;
handleDelete function
const handleDelete async itemld
try
const response await fetchMy url$itemId
method: 'DELETE',
;
const data await response.json;
if datasuccess
deleteltemitemld;
setSuccesstrue;
else
setErrordatamessage 'Failed to delete the item';
setSuccessfalse;
catch err
setErrorAn error occurred while deleting the item';
setSuccessfalse;
;
return
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
