@@ -3,11 +3,13 @@ import { SectionTitle } from "@/components";
33import { useProductStore } from "../_zustand/store" ;
44import Image from "next/image" ;
55import { useEffect , useState } from "react" ;
6+ import { useSession } from "next-auth/react" ;
67import toast from "react-hot-toast" ;
78import { useRouter } from "next/navigation" ;
89import apiClient from "@/lib/api" ;
910
1011const CheckoutPage = ( ) => {
12+ const { data : session } = useSession ( ) ;
1113 const [ checkoutForm , setCheckoutForm ] = useState ( {
1214 name : "" ,
1315 lastname : "" ,
@@ -125,6 +127,24 @@ const CheckoutPage = () => {
125127 try {
126128 console . log ( "🚀 Starting order creation..." ) ;
127129
130+ // Get user ID if logged in
131+ let userId = null ;
132+ if ( session ?. user ?. email ) {
133+ try {
134+ console . log ( "🔍 Getting user ID for logged-in user:" , session . user . email ) ;
135+ const userResponse = await apiClient . get ( `/api/users/email/${ session . user . email } ` ) ;
136+ if ( userResponse . ok ) {
137+ const userData = await userResponse . json ( ) ;
138+ userId = userData . id ;
139+ console . log ( "✅ Found user ID:" , userId ) ;
140+ } else {
141+ console . log ( "❌ Could not find user with email:" , session . user . email ) ;
142+ }
143+ } catch ( userError ) {
144+ console . log ( "⚠️ Error getting user ID:" , userError ) ;
145+ }
146+ }
147+
128148 // Prepare the order data
129149 const orderData = {
130150 name : checkoutForm . name . trim ( ) ,
@@ -140,6 +160,7 @@ const CheckoutPage = () => {
140160 city : checkoutForm . city . trim ( ) ,
141161 country : checkoutForm . country . trim ( ) ,
142162 orderNotice : checkoutForm . orderNotice . trim ( ) ,
163+ userId : userId // Add user ID for notifications
143164 } ;
144165
145166 console . log ( "📋 Order data being sent:" , orderData ) ;
@@ -163,20 +184,29 @@ const CheckoutPage = () => {
163184 const errorData = JSON . parse ( errorText ) ;
164185 console . error ( "Parsed error data:" , errorData ) ;
165186
166- // Show specific validation errors
167- if ( errorData . details && Array . isArray ( errorData . details ) ) {
187+ // Handle different error types
188+ if ( response . status === 409 ) {
189+ // Duplicate order error
190+ toast . error ( errorData . details || errorData . error || "Duplicate order detected" ) ;
191+ return ; // Don't throw, just return to stop execution
192+ } else if ( errorData . details && Array . isArray ( errorData . details ) ) {
193+ // Validation errors
168194 errorData . details . forEach ( ( detail : any ) => {
169195 toast . error ( `${ detail . field } : ${ detail . message } ` ) ;
170196 } ) ;
197+ } else if ( typeof errorData . details === 'string' ) {
198+ // Single error message in details
199+ toast . error ( errorData . details ) ;
171200 } else {
172- toast . error ( errorData . error || "Validation failed" ) ;
201+ // Fallback error message
202+ toast . error ( errorData . error || "Order creation failed" ) ;
173203 }
174204 } catch ( parseError ) {
175205 console . error ( "Could not parse error as JSON:" , parseError ) ;
176- toast . error ( "Validation failed" ) ;
206+ toast . error ( "Order creation failed. Please try again. " ) ;
177207 }
178208
179- throw new Error ( `HTTP ${ response . status } : ${ response . statusText } ` ) ;
209+ return ; // Stop execution instead of throwing
180210 }
181211
182212 const data = await response . json ( ) ;
@@ -223,6 +253,14 @@ const CheckoutPage = () => {
223253 } ) ;
224254 clearCart ( ) ;
225255
256+ // Refresh notification count if user is logged in
257+ try {
258+ // This will trigger a refresh of notifications in the background
259+ window . dispatchEvent ( new CustomEvent ( 'orderCompleted' ) ) ;
260+ } catch ( error ) {
261+ console . log ( 'Note: Could not trigger notification refresh' ) ;
262+ }
263+
226264 toast . success ( "Order created successfully! You will be contacted for payment." ) ;
227265 setTimeout ( ( ) => {
228266 router . push ( "/" ) ;
0 commit comments