|
| 1 | +import { useForm } from "react-hook-form"; |
| 2 | +import moment from "moment"; |
| 3 | +import useAxiosSecqur from "../../Hooks/useAxiosSecqur"; |
| 4 | +import useAxiosCommon from "../../Hooks/useAxiosCommon"; |
| 5 | +import { useParams } from "react-router-dom"; |
| 6 | +import { useQuery } from "@tanstack/react-query"; |
| 7 | +import LogingSpiner from "../../Sheare/LogingSpiner"; |
| 8 | +import toast from "react-hot-toast"; |
| 9 | +const imgHosting_api = `https://api.imgbb.com/1/upload?key=${ |
| 10 | + import.meta.env.VITE_IMGBB_key |
| 11 | +}`; |
| 12 | + |
| 13 | +const Updatemeaks = () => { |
| 14 | + const axiosSec = useAxiosSecqur(); |
| 15 | + const axiospub = useAxiosCommon(); |
| 16 | + const { id } = useParams(); |
| 17 | + const { |
| 18 | + data: meal = [], |
| 19 | + isLoading, |
| 20 | + refetch, |
| 21 | + } = useQuery({ |
| 22 | + queryKey: ["meal", id], |
| 23 | + queryFn: async () => { |
| 24 | + const { data } = await axiospub.get(`/meal/${id}`); |
| 25 | + return data; |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + const { register, handleSubmit } = useForm(); |
| 30 | + |
| 31 | + const onSubmit = async (data) => { |
| 32 | + const { |
| 33 | + title, |
| 34 | + catagory, |
| 35 | + price, |
| 36 | + rating, |
| 37 | + likes, |
| 38 | + itm1, |
| 39 | + itm2, |
| 40 | + itm3, |
| 41 | + itm4, |
| 42 | + image, |
| 43 | + description, |
| 44 | + } = data; |
| 45 | + |
| 46 | + const imageFile = { image: data.image[0] }; |
| 47 | + const res = await axiospub.post(imgHosting_api, imageFile, { |
| 48 | + headers: { |
| 49 | + "Content-Type": "multipart/form-data", |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + if (res.data.success) { |
| 54 | + const mealItem = { |
| 55 | + title, |
| 56 | + catagory, |
| 57 | + price: parseFloat(price), |
| 58 | + rating: parseFloat(rating), |
| 59 | + likes: parseFloat(likes), |
| 60 | + ingredients: { itm1, itm2, itm3, itm4 }, |
| 61 | + image: res.data.data.display_url, |
| 62 | + description, |
| 63 | + post_time: moment().format("LLLL"), |
| 64 | + }; |
| 65 | + // const menuRes = await axiosSec.put(`/updatemeals/${id}`, mealItem); |
| 66 | + // console.log(mealItem); |
| 67 | + |
| 68 | + refetch(); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + if (isLoading) return <LogingSpiner></LogingSpiner>; |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + <div> |
| 76 | + <h2 className="text-3xl font-bold my-5 text-center">Add Meal</h2> |
| 77 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 78 | + <div className="grid md:grid-cols-2 gap-3"> |
| 79 | + <div> |
| 80 | + <label>Meal Title</label> |
| 81 | + <input |
| 82 | + type="text" |
| 83 | + className=" input input-disabled w-full" |
| 84 | + name="" |
| 85 | + defaultValue={meal.title} |
| 86 | + {...register("title")} |
| 87 | + id="" |
| 88 | + /> |
| 89 | + </div> |
| 90 | + |
| 91 | + <div> |
| 92 | + <label>Catagory</label> |
| 93 | + <select |
| 94 | + className=" input input-disabled w-full" |
| 95 | + id="meal" |
| 96 | + name="catagory" |
| 97 | + defaultValue={meal.catagory} |
| 98 | + {...register("catagory")} |
| 99 | + > |
| 100 | + <option value="breakfast">Breakfast</option> |
| 101 | + <option value="lunch">Lunch</option> |
| 102 | + <option value="dinner">Dinner</option> |
| 103 | + </select> |
| 104 | + </div> |
| 105 | + |
| 106 | + <div> |
| 107 | + <label>Meal Price</label> |
| 108 | + <input |
| 109 | + type="number" |
| 110 | + className=" input input-disabled w-full" |
| 111 | + name="price" |
| 112 | + defaultValue={meal.price} |
| 113 | + {...register("price")} |
| 114 | + id="" |
| 115 | + /> |
| 116 | + </div> |
| 117 | + |
| 118 | + <div> |
| 119 | + <label>Meal Rating</label> |
| 120 | + <input |
| 121 | + type="number" |
| 122 | + className=" input input-disabled w-full" |
| 123 | + name="rating" |
| 124 | + defaultValue={meal.rating} |
| 125 | + {...register("rating")} |
| 126 | + id="" |
| 127 | + /> |
| 128 | + </div> |
| 129 | + |
| 130 | + <div> |
| 131 | + <label>Meal likes</label> |
| 132 | + <input |
| 133 | + type="number" |
| 134 | + className=" input input-disabled w-full" |
| 135 | + name=" likes" |
| 136 | + defaultValue={meal.likes} |
| 137 | + {...register("likes")} |
| 138 | + id="" |
| 139 | + /> |
| 140 | + </div> |
| 141 | + |
| 142 | + <div> |
| 143 | + <label>ingredients</label> |
| 144 | + <div className="grid md:grid-cols-2 gap-2"> |
| 145 | + <input |
| 146 | + type="text" |
| 147 | + className=" input input-disabled w-full" |
| 148 | + name=" itm1" |
| 149 | + defaultValue={meal.ingredients.itm1} |
| 150 | + {...register("itm1")} |
| 151 | + id="" |
| 152 | + required |
| 153 | + /> |
| 154 | + <input |
| 155 | + type="text" |
| 156 | + className=" input input-disabled w-full" |
| 157 | + name=" itm2" |
| 158 | + defaultValue={meal.ingredients.itm2} |
| 159 | + {...register("itm2")} |
| 160 | + id="" |
| 161 | + /> |
| 162 | + <input |
| 163 | + type="text" |
| 164 | + className=" input input-disabled w-full" |
| 165 | + defaultValue={meal.ingredients.itm3} |
| 166 | + name=" itm3" |
| 167 | + {...register("itm3")} |
| 168 | + id="" |
| 169 | + /> |
| 170 | + <input |
| 171 | + type="text" |
| 172 | + className=" input input-disabled w-full" |
| 173 | + defaultValue={meal.ingredients.itm4} |
| 174 | + name=" itm4" |
| 175 | + {...register("itm4")} |
| 176 | + id="" |
| 177 | + /> |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + |
| 181 | + <div> |
| 182 | + <img className="w-14 h-14" src={meal.image} alt="" /> |
| 183 | + <input |
| 184 | + type="file" |
| 185 | + className="file-input file-input-bordered file-input-secondary w-full max-w-xs" |
| 186 | + {...register("image")} |
| 187 | + /> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + <div> |
| 191 | + <label>description </label> |
| 192 | + <textarea |
| 193 | + className=" input input-disabled w-full" |
| 194 | + name="" |
| 195 | + {...register("description")} |
| 196 | + defaultValue={meal.description} |
| 197 | + id="" |
| 198 | + cols="30" |
| 199 | + rows="50" |
| 200 | + ></textarea> |
| 201 | + </div> |
| 202 | + <input |
| 203 | + className=" btn btn-primary w-full" |
| 204 | + type="submit" |
| 205 | + disabled |
| 206 | + value="Add Meal" |
| 207 | + /> |
| 208 | + </form> |
| 209 | + </div> |
| 210 | + </div> |
| 211 | + ); |
| 212 | +}; |
| 213 | + |
| 214 | +export default Updatemeaks; |
0 commit comments