11"use client"
2- import { CheckIcon } from "@heroicons/react/24/solid" ;
2+ import { CheckIcon , InboxArrowDownIcon } from "@heroicons/react/24/solid" ;
33import { ChangeEvent , FormEvent , useState } from "react" ;
44import Button from "../../../components/ui/Button" ;
55import { IRecipCreate , Ingredients } from "../../../models/recipe" ;
@@ -19,7 +19,8 @@ export default function CreateRecipe() {
1919 const [ singleIngredient , setSingleIngredient ] = useState ( "" ) ;
2020 const [ ingredients , setIngredients ] = useState < Ingredients [ ] > ( [ ] ) ;
2121 const [ selectedPhoto , setSelectedPhoto ] = useState < File | null > ( null ) ;
22- const [ loading , setLoading ] = useState ( true )
22+ const [ loading , setLoading ] = useState ( false )
23+ const [ apiMessage , setApiMessage ] = useState ( "" )
2324
2425 // تابع handlePhotoChange برای مدیریت تغییرات در ورودی عکس
2526 const handlePhotoChange = ( event : ChangeEvent < HTMLInputElement > ) => {
@@ -40,7 +41,7 @@ export default function CreateRecipe() {
4041 // موارد بعد از شاخص درج:
4142 ...ingredients . slice ( insertAt ) ,
4243 ] ;
43- console . log ( "next" , nextIngredients ) ;
44+ // console.log("next", nextIngredients);
4445
4546 setIngredients ( nextIngredients ) ;
4647 setSingleIngredient ( "" ) ;
@@ -64,13 +65,23 @@ export default function CreateRecipe() {
6465 } ;
6566 try {
6667 const saveRecip = await fetch ( "/api/recipes" , options ) ;
67- setLoading ( false )
6868 if ( saveRecip . status === 200 ) {
69- console . log ( saveRecip ) ;
69+ setLoading ( false )
70+ console . log ( saveRecip . json ( ) ) ;
71+ setApiMessage ( "رسپی ساخته شد!" )
72+
73+ setTimeout ( ( ) => {
74+ setApiMessage ( "" )
75+ } , 4000 ) ;
7076 }
7177 } catch ( error ) {
7278 setLoading ( false )
7379 console . log ( error ) ;
80+ setApiMessage ( "مشکلی در ساخت رسپی به وجود آمد" )
81+
82+ setTimeout ( ( ) => {
83+ setApiMessage ( "" )
84+ } , 4000 ) ;
7485 }
7586 }
7687 } ;
@@ -183,11 +194,22 @@ export default function CreateRecipe() {
183194 < Button
184195 type = "submit"
185196 className = "w-96 py-3 px-4 inline-flex justify-center items-center gap-2 rounded-md border border-transparent font-semibold bg-cyan-700 text-white hover:bg-cyan-500 focus:outline-none focus:ring-2 focus:ring-cyan-500 focus:ring-offset-2 transition-all text-sm"
197+ disabled = { loading }
186198 >
199+ {
200+ loading &&
201+ < InboxArrowDownIcon width = { 20 } />
202+ }
187203 < span > ساخت رسپی</ span >
188204 </ Button >
189205 </ div >
190206 </ form >
207+ { apiMessage &&
208+ < div className = "flex w-96 mx-auto m-4 p-4 rounded bg-slate-50" >
209+ < p className = " text-gray-500 px-2" > پیام سیستم:</ p >
210+ < p className = " text-green-600 font-bold" > { apiMessage } </ p >
211+ </ div >
212+ }
191213 </ section >
192214 </ div >
193215 </ div >
0 commit comments