Adding an Artists Section to the Chinook Project
NOTE THAT THERE IS A BUG!
I FOUND A BUG AFTER CREATING THE VIDEO!
When posting a new artist, the id is getting set to 0 by the react app. And the JSON server leaves it as 0, so every new artist that you add will have an id of 0.
The solution is to NOT include an id at all for inserts/POSTS.
To fix it, I updated the handleSubmit() function to look like this:
function handleSubmit(evt){
evt.preventDefault();
if(name){
// const artistObj = {id: artistId, name}; // ONLY ADD THE ID FOR UPDATES
const artistObj = {name};
if(artistId > 0){
// update
artistObj.id = artistId; // IF UPDATE, ADD THE ID HERE ONLY
updateArtist(artistObj).then(navigate("/artists"));
}else{
// insert
insertArtist(artistObj).then(navigate("/artists"));
}
}else{
console.log("form is not valid!")
}
}