@@ -77,6 +77,8 @@ const useQuantityInput: Theme.QuantityInput.Function = (
7777 } ) ;
7878} ;
7979
80+ const isValidInputNum = ( inputNum : number ) => ! Number . isNaN ( inputNum ) && Number . isInteger ( inputNum ) ;
81+
8082const changeQuantity = ( qtyInput : HTMLInputElement , change : number , keyboard = false ) => {
8183 const { mode} = qtyInput . dataset ;
8284
@@ -85,7 +87,7 @@ const changeQuantity = (qtyInput: HTMLInputElement, change: number, keyboard = f
8587 const currentValue = Number ( qtyInput . value ) ;
8688 const min = ( qtyInput . dataset . updateUrl === undefined ) ? Number ( qtyInput . getAttribute ( 'min' ) ) : 0 ;
8789 const newValue = Math . max ( currentValue + change , min ) ;
88- qtyInput . value = String ( ! isNaN ( newValue ) ? newValue : baseValue ) ;
90+ qtyInput . value = String ( isValidInputNum ( newValue ) ? newValue : baseValue ) ;
8991 }
9092} ;
9193
@@ -103,7 +105,7 @@ const updateQuantity = async (qtyInputGroup: Theme.QuantityInput.InputGroup, cha
103105 const baseValue = Number ( qtyInput . getAttribute ( 'value' ) ) ;
104106 const quantity = targetValue - baseValue ;
105107
106- if ( Number . isNaN ( targetValue ) === false && quantity !== 0 ) {
108+ if ( isValidInputNum ( targetValue ) && quantity !== 0 ) {
107109 const requestUrl = qtyInput . dataset . updateUrl ;
108110
109111 if ( requestUrl !== undefined ) {
@@ -113,8 +115,6 @@ const updateQuantity = async (qtyInputGroup: Theme.QuantityInput.InputGroup, cha
113115
114116 toggleButtonSpinner ( targetButton , targetButtonIcon , targetButtonSpinner ) ;
115117
116- const { productId} = qtyInput . dataset ;
117-
118118 try {
119119 const response = await sendUpdateCartRequest ( requestUrl , quantity ) ;
120120
@@ -123,7 +123,7 @@ const updateQuantity = async (qtyInputGroup: Theme.QuantityInput.InputGroup, cha
123123
124124 if ( data . hasError ) {
125125 const errors = data . errors as Array < string > ;
126- const productAlertSelector = resetAlertContainer ( Number ( productId ) ) ;
126+ const productAlertSelector = resetAlertContainer ( qtyInput ) ;
127127
128128 if ( errors && productAlertSelector ) {
129129 errors . forEach ( ( error : string ) => {
@@ -156,7 +156,7 @@ const updateQuantity = async (qtyInputGroup: Theme.QuantityInput.InputGroup, cha
156156
157157 if ( errorData . status !== undefined ) {
158158 const errorMsg = `${ errorData . statusText } : ${ errorData . url } ` ;
159- const productAlertSelector = resetAlertContainer ( Number ( productId ) ) ;
159+ const productAlertSelector = resetAlertContainer ( qtyInput ) ;
160160 useAlert ( errorMsg , { type : 'danger' , selector : productAlertSelector } ) . show ( ) ;
161161
162162 prestashop . emit ( events . handleError , {
@@ -170,8 +170,7 @@ const updateQuantity = async (qtyInputGroup: Theme.QuantityInput.InputGroup, cha
170170 }
171171 }
172172 } else {
173- // The input value is not a correct number so revert to the value in the DOM
174- qtyInput . value = String ( baseValue ) ;
173+ // The input value is not a correct number
175174 showSpinButtons ( qtyInputGroup ) ;
176175 }
177176 }
@@ -183,9 +182,11 @@ const getTargetButton = (qtyInputGroup: Theme.QuantityInput.InputGroup, change:
183182 return ( change > 0 ) ? incrementButton : decrementButton ;
184183} ;
185184
186- const resetAlertContainer = ( productId : number ) => {
187- if ( productId ) {
188- const productAlertSelector = quantityInputMap . alert ( productId ) ;
185+ const resetAlertContainer = ( qtyInput : HTMLInputElement ) => {
186+ const { alertId} = qtyInput . dataset ;
187+
188+ if ( alertId ) {
189+ const productAlertSelector = quantityInputMap . alert ( alertId ) ;
189190 const productAlertContainer = document . querySelector < HTMLDivElement > ( productAlertSelector ) ;
190191
191192 if ( productAlertContainer ) {
0 commit comments