Cloudinary Image disappears in Edit #759
-
This morning, we connected our app to Cloudinary and very happily uploaded several photos. Then, when testing our app's various functions, we noticed that editing an entry/service causes the previously assigned image to disappear, which we do not want. However, we cannot find how to keep this from happening. 😠 At least one of us is convinced that We have looked into placeholder images, but are frustrated that the answers aren't more detailed (put that Cloudinary code where, exactly?!). The code in question looks like this:
Here's our current PR. And here's a link to the relevant US. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Quick guess: you are not uploading a new image on subsequent edits, this leading to send an Possible solution: on the edit field, hide the file input field by default via state, and display just if the user wants to upload a new picture - mark it as import { useState } from 'react';
function ServiceForm({ service = {}, onSubmit }) {
const [showFileInput, setShowFileInput] = useState(!service._id);
...
return (
<form onSubmit={...}>
...
{showFileInput ? (
<div className="image-uploader">
<label>
Image
<input name="image" type="file" required />
</label>
<button type="button" onClick={() => setShowFileInput(false)}>
Cancel
</button>
</div>
) : (
<button type="button" onClick={() => setShowFileInput(true)}>
Upload Image
</button>
)}
<button>Submit</button>
</form>
);
} Demo (see console for effect on submit) Don't forget to upload to Cloudinary just in the case you are providing a file, i.e.: async function onSubmit(data, file) {
let image;
if (file) {
image = await uploadImage(file);
}
await editService(id, { ...data, image });
// ...
} |
Beta Was this translation helpful? Give feedback.
Quick guess: you are not uploading a new image on subsequent edits, this leading to send an
image
object without meaningful information, overwriting the existing image.Possible solution: on the edit field, hide the file input field by default via state, and display just if the user wants to upload a new picture - mark it as
required
so they cannot leave it blank: