|
@@ -14,6 +14,7 @@ import Loading from "../Loading";
|
|
|
import DoneButton from "../Buttons/DoneButton";
|
|
|
import { useLocation, useNavigate, useParams } from "react-router-dom";
|
|
|
import SavingButton from "../Buttons/SavingButton";
|
|
|
+import { deleteSingleImage } from "../Images/images.util";
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
form: {
|
|
@@ -58,12 +59,16 @@ const EditMeal = () => {
|
|
|
let { mealId } = useParams();
|
|
|
|
|
|
const [meal, setMeal] = useState();
|
|
|
+ const [originalMealImages, setOriginalMealImages] = useState([]);
|
|
|
const [imagesLoading, setImagesLoading] = useState(false);
|
|
|
const [loadingImagesTakesLong, setLoadingImagesTakesLong] = useState(false);
|
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- fetchAndUpdateMeal(mealId, setMeal);
|
|
|
+ fetchAndUpdateMeal(mealId, (mealFromDB) => {
|
|
|
+ setMeal(mealFromDB);
|
|
|
+ if(mealFromDB.images) setOriginalMealImages(Array.from(mealFromDB.images));
|
|
|
+ });
|
|
|
}, [mealId]);
|
|
|
|
|
|
// set the meal that is given in state as a temporary option while the one from the server is loaded
|
|
@@ -81,7 +86,7 @@ const EditMeal = () => {
|
|
|
const deleteMeal = () => {
|
|
|
axios.post(serverURL + '/meals/delete/' + meal._id).then((result) => {
|
|
|
const deletedMeal = result.data.meal;
|
|
|
- console.log('delete request sent for meal ', deletedMeal._id);
|
|
|
+ // console.log('delete request sent for meal ', deletedMeal._id);
|
|
|
navigate('../', {
|
|
|
state: {
|
|
|
snackbar: {
|
|
@@ -98,7 +103,7 @@ const EditMeal = () => {
|
|
|
setIsSaving(true);
|
|
|
if (meal.title) {
|
|
|
axios.post(serverURL + '/meals/edit/' + meal._id, meal).then((result) => {
|
|
|
- console.log('edit request sent', result.data);
|
|
|
+ // console.log('edit request sent', result.data);
|
|
|
setIsSaving(false);
|
|
|
navigate(-1, { state: { meal: result.data.meal } });
|
|
|
});
|
|
@@ -106,6 +111,17 @@ const EditMeal = () => {
|
|
|
}
|
|
|
|
|
|
const closeEdit = () => {
|
|
|
+ // delete images that should not be saved
|
|
|
+ const imagesToDelete = meal.images.filter(i => !originalMealImages.includes(i));
|
|
|
+ if(imagesToDelete.length > 0) {
|
|
|
+ imagesToDelete.forEach(image => {
|
|
|
+ deleteSingleImage(image, () => {console.log('deleted', image)});
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // todo right now images that get deleted do not get restored when cancel is clicked.
|
|
|
+ // might consider either doing that or deleting them only on save
|
|
|
+
|
|
|
navigate(-1, { state: { meal } });
|
|
|
}
|
|
|
|