Skip to content

Commit

Permalink
cart page errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Oct 2, 2023
1 parent 462d8c4 commit 5dc66cf
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 7 deletions.
8 changes: 1 addition & 7 deletions _dev/js/theme/components/http/useDefaultHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ const useDefaultHttpRequest = (url, payload, options = {}) => {
.query(payload)
.post()
.json((resp) => {
if (resp.errors) {
const errors = Array.isArray(resp.errors) ? resp.errors : [resp.errors];

reject(Error(errors.join('\n')));
} else {
resolve(resp);
}
resolve(resp);
})
.catch(() => {
reject(Error(prestashop.t.alert.genericHttpError));
Expand Down
4 changes: 4 additions & 0 deletions _dev/js/theme/core/cart/cartController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import deleteFromCartHandler from './handler/cart/deleteFromCartHandler';
import quantityChangeHandler from './handler/cart/quantityChangeHandler';
import updateCartHandler from './handler/cart/updateCartHandler';
import updatedCartHandler from './handler/cart/updatedCartHandler';
import cartErrorsHandler from './handler/cart/cartErrorsHandler';
import useCustomQuantityInput from '../../components/useCustomQuantityInput';

const { on } = useEvent();
Expand Down Expand Up @@ -50,11 +51,14 @@ const cartController = () => {
prestashop.on('updatedCart', (event) => {
attachSpinnerEvents();
updatedCartHandler(event);
cartErrorsHandler();
});

prestashop.on('updateCart', (event) => {
updateCartHandler(event);
});

cartErrorsHandler();
};

return {
Expand Down
61 changes: 61 additions & 0 deletions _dev/js/theme/core/cart/handler/cart/cartErrorsHandler.js
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');
}
}

Check failure on line 59 in _dev/js/theme/core/cart/handler/cart/cartErrorsHandler.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

export default cartErrorsHandler;
12 changes: 12 additions & 0 deletions _dev/js/theme/core/cart/handler/cart/quantityChangeHandler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import prestashop from 'prestashop';
import quantityChangeRequest from '../../request/cart/quantityChangeRequest';
import useAlertToast from '../../../../components/useAlertToast';
import cartStateStore from '../../store/cartStateStore';

const { danger } = useAlertToast();

const { setIsUpdateOperation, setHasError, setErrorMsg } = cartStateStore();

/**
* @param {string} operation - increase|decrease
* @param {number} qtyDifference - quantity difference
Expand Down Expand Up @@ -31,6 +34,15 @@ const quantityChangeHandler = async (operation, qtyDifference, input) => {
try {
const resp = await getRequest();

const {
errors = '',
hasError = false,
} = resp;

setIsUpdateOperation(true);
setHasError(hasError);
setErrorMsg(errors);

if (!resp.hasError) {
prestashop.emit('updateCart', {
reason: dataset || resp,
Expand Down
78 changes: 78 additions & 0 deletions _dev/js/theme/core/cart/store/cartStateStore.js
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 = () => {

Check failure on line 12 in _dev/js/theme/core/cart/store/cartStateStore.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Block must not be padded by blank lines

/**
* Sets the error message
* @method setErrorMsg
* @public
* @param {string} value
*/
const setErrorMsg = (value) => {
state.errorMsg = value;
}

Check failure on line 22 in _dev/js/theme/core/cart/store/cartStateStore.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

/**
* 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;
}

Check failure on line 40 in _dev/js/theme/core/cart/store/cartStateStore.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

/**
* 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;
}

Check failure on line 58 in _dev/js/theme/core/cart/store/cartStateStore.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

/**
* Returns the hasError value
* @method getHasError
* @public
* @return {boolean}
*/
const getHasError = () => state.hasError;

return {
setErrorMsg,
getErrorMsg,
setIsUpdateOperation,
getIsUpdateOperation,
setHasError,
getHasError,
}

Check failure on line 75 in _dev/js/theme/core/cart/store/cartStateStore.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon
}

Check failure on line 76 in _dev/js/theme/core/cart/store/cartStateStore.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

export default cartStateStore;
12 changes: 12 additions & 0 deletions _dev/js/theme/core/product/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ const persistFormDataOnInit = () => {
}
};

/**
* Product controller
* @module productController
* @return {{init: init}}
*/
const productController = () => {

Check failure on line 33 in _dev/js/theme/core/product/productController.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Block must not be padded by blank lines

/**
* Init product controller
* @method init
* @public
* @return {void}
*/
const init = () => {
persistFormDataOnInit();
on(document, 'click', prestashop.selectors.listing.quickview, quickViewClickHandler);
Expand Down

0 comments on commit 5dc66cf

Please sign in to comment.