-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import prestashop from 'prestashop'; | ||
import cartStateStore from '../../store/cartStateStore'; | ||
|
||
const { | ||
setIsUpdateOperation, | ||
setHasError, | ||
setErrorMsg, | ||
getErrorMsg, | ||
getHasError, | ||
getIsUpdateOperation, | ||
} = cartStateStore(); | ||
|
||
/** | ||
* Handles cart page errors | ||
* Side effects: updates cart state and cart notification DOM | ||
* @returns {void} | ||
*/ | ||
const cartErrorsHandler = () => { | ||
const checkoutBtn = document.querySelector(prestashop.selectors.checkout.btn); | ||
const errorMsg = getErrorMsg(); | ||
const hasError = getHasError(); | ||
const isUpdateOperation = getIsUpdateOperation(); | ||
const notification = document.querySelector(prestashop.selectors.notifications.container); | ||
|
||
if (document.querySelector(prestashop.selectors.notifications.dangerAlert) || (errorMsg !== '' && !hasError)) { | ||
checkoutBtn.classList.add('disabled'); | ||
} | ||
|
||
if (errorMsg !== '') { | ||
const alertBlock = ` | ||
<article class="alert alert-danger" role="alert" data-alert="danger"> | ||
<ul class="mb-0"> | ||
<li>${errorMsg}</li> | ||
</ul> | ||
</article> | ||
`; | ||
|
||
if (notification) { | ||
notification.innerHTML = alertBlock; | ||
} | ||
|
||
setErrorMsg(''); | ||
setIsUpdateOperation(false); | ||
|
||
if (hasError) { | ||
// if hasError is true, quantity was not updated : allow checkout | ||
checkoutBtn.classList.remove('disabled'); | ||
} | ||
} else if (!hasError && isUpdateOperation) { | ||
setHasError(false); | ||
setIsUpdateOperation(false); | ||
|
||
if (notification) { | ||
notification.innerHTML = ''; | ||
} | ||
|
||
checkoutBtn.classList.remove('disabled'); | ||
} | ||
} | ||
|
||
export default cartErrorsHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const state = { | ||
errorMsg: '', | ||
isUpdateOperation: false, | ||
hasError: false, | ||
}; | ||
|
||
/** | ||
* Store for cart state | ||
* @module | ||
* @returns {object} | ||
*/ | ||
const cartStateStore = () => { | ||
|
||
/** | ||
* Sets the error message | ||
* @method setErrorMsg | ||
* @public | ||
* @param {string} value | ||
*/ | ||
const setErrorMsg = (value) => { | ||
state.errorMsg = value; | ||
} | ||
|
||
/** | ||
* Returns the error message | ||
* @method getErrorMsg | ||
* @public | ||
* @return {string} | ||
*/ | ||
const getErrorMsg = () => state.errorMsg; | ||
|
||
/** | ||
* Sets the isUpdateOperation value | ||
* @method setIsUpdateOperation | ||
* @public | ||
* @param {boolean} value | ||
*/ | ||
const setIsUpdateOperation = (value) => { | ||
state.isUpdateOperation = value; | ||
} | ||
|
||
/** | ||
* Returns the isUpdateOperation value | ||
* @method getIsUpdateOperation | ||
* @public | ||
* @return {boolean} | ||
*/ | ||
const getIsUpdateOperation = () => state.isUpdateOperation; | ||
|
||
/** | ||
* Sets the hasError value | ||
* @method setHasError | ||
* @public | ||
* @param {boolean} value | ||
*/ | ||
const setHasError = (value) => { | ||
state.hasError = value; | ||
} | ||
|
||
/** | ||
* Returns the hasError value | ||
* @method getHasError | ||
* @public | ||
* @return {boolean} | ||
*/ | ||
const getHasError = () => state.hasError; | ||
|
||
return { | ||
setErrorMsg, | ||
getErrorMsg, | ||
setIsUpdateOperation, | ||
getIsUpdateOperation, | ||
setHasError, | ||
getHasError, | ||
} | ||
} | ||
|
||
export default cartStateStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters