From ec4f910562cb20a74bddb98565a92c896cba0b07 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Sat, 4 Jun 2022 03:19:18 -0400 Subject: [PATCH] Update state information to prefill edit forms with current values --- exercises-ui/src/pages/EditPage.js | 44 ++++++++++-------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/exercises-ui/src/pages/EditPage.js b/exercises-ui/src/pages/EditPage.js index 4a76842..994d7c5 100644 --- a/exercises-ui/src/pages/EditPage.js +++ b/exercises-ui/src/pages/EditPage.js @@ -2,20 +2,25 @@ import "../App.css"; import React, { useState } from "react"; import { useHistory } from "react-router-dom"; -export const EditPage = () => { - const [name, setName] = useState(""); - const [reps, setReps] = useState(""); - const [weight, setWeight] = useState(""); - const [unit, setUnit] = useState(""); - const [date, setDate] = useState(""); +export const EditPage = ({ exerciseToEdit }) => { + const [name, setName] = useState(exerciseToEdit.name); + const [reps, setReps] = useState(exerciseToEdit.reps); + const [weight, setWeight] = useState(exerciseToEdit.weight); + const [unit, setUnit] = useState(exerciseToEdit.unit); + const [date, setDate] = useState(exerciseToEdit.date); const history = useHistory(); const editExercise = async () => { - const updatedExercise = { name, reps, weight, unit, date }; - const response = await fetch(`/exercises/id`, { + const response = await fetch(`/exercises/${exerciseToEdit._id}`, { method: "PUT", - body: JSON.stringify(updatedExercise), + body: JSON.stringify({ + name: name, + reps: reps, + weight: weight, + unit: unit, + date: date, + }), headers: { "Content-Type": "application/json" }, }); if (response.status === 200) { @@ -31,51 +36,32 @@ export const EditPage = () => {

Edit Exercise

setName(e.target.value)} /> setReps(e.target.value)} /> setWeight(e.target.value)} /> setUnit(e.target.value)} /> setDate(e.target.value)} /> - + ); }; -// const response = await fetch(`/exercises/${exerciseToEdit._id}`, { -// method: 'PUT', -// body: JSON.stringify({ name: name, reps: reps, weight: weight, unit: unit, date: date }), -// headers: { -// 'Content-Type': 'application/json', -// }, -// }); -// if(response.status === 200){ -// alert("Successfully edited the exercise!"); -// } else { -// alert(`Failed to edit the exercise, status code = ${response.status}`); -// } -// }; - export default EditPage;