@@ -770,20 +770,49 @@ function Canvas({
770770 setUndoStack ( ( prev ) => [ ...prev , stampDrawing ] ) ;
771771 setRedoStack ( [ ] ) ;
772772
773- // Submit to backend
773+ // Use submission queue to ensure stamps are submitted in order
774774 try {
775- await submitToDatabase (
776- stampDrawing ,
777- auth ,
778- { roomId : currentRoomId , roomType } ,
779- setUndoAvailable ,
780- setRedoAvailable
781- ) ;
782- console . log ( "Stamp submitted successfully:" , stampDrawing . drawingId ) ;
775+ const submitTask = async ( ) => {
776+ try {
777+ console . log ( "Submitting queued stamp:" , {
778+ drawingId : stampDrawing . drawingId ,
779+ stampData : stampDrawing . stampData ,
780+ } ) ;
781+
782+ await submitToDatabase (
783+ stampDrawing ,
784+ auth ,
785+ { roomId : currentRoomId , roomType } ,
786+ setUndoAvailable ,
787+ setRedoAvailable
788+ ) ;
789+
790+ console . log ( "Stamp submitted successfully:" , stampDrawing . drawingId ) ;
791+
792+ if ( currentRoomId ) {
793+ checkUndoRedoAvailability (
794+ auth ,
795+ setUndoAvailable ,
796+ setRedoAvailable ,
797+ currentRoomId
798+ ) ;
799+ }
800+ } catch ( error ) {
801+ console . error ( "Error during queued stamp submission:" , error ) ;
802+ setPendingDrawings ( ( prev ) =>
803+ prev . filter ( ( d ) => d . drawingId !== stampDrawing . drawingId )
804+ ) ;
805+ handleAuthError ( error ) ;
806+ showLocalSnack ( "Failed to save stamp. Please try again." ) ;
807+ }
808+ } ;
809+
810+ submissionQueueRef . current . push ( submitTask ) ;
811+ processSubmissionQueue ( ) ;
783812 } catch ( error ) {
784- console . error ( "Failed to submit stamp:" , error ) ;
813+ console . error ( "Error preparing stamp submission :" , error ) ;
785814 handleAuthError ( error ) ;
786- showLocalSnack ( "Failed to save stamp. Please try again." ) ;
815+ showLocalSnack ( "Failed to prepare stamp. Please try again." ) ;
787816 }
788817 } ;
789818
0 commit comments