I am trying to create a form where user can create course paid/free , If paid then choose the price but if free price=0,
This is my default state:
const [values, setValues] = useState({
name: "",
description: "",
category: "",
price: 500,
paid: true,
uploading: false,
loading: false,
});
Problem: If I click paid course my default price is 500 , which is the first price of the price list. But when I change to Free Course, state changes to "paid:false" but last selected price stayed like the picture below
Any Idea how to change! I have tried a few ways but nothing worked. Please help thank you❤️
I have tried to change it before sending it to backend, still didn't work.
const handleSubmit = async (e) => {
e.preventDefault();
try {
if(values.paid == false) {
setValues({...values, price: 0})
}
const { data } = await axios.post("/api/course/create-new-course", {
...values,
image,
});
router.push("/instructor");
toast.success("Continue adding lessions");
} catch (err) {
console.log(err);
toast.error(err.response.data);
}
};