Skip to content

Commit

Permalink
delete from cart action, minor jsdocs fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Sep 19, 2023
1 parent 92211e4 commit ee7a7f5
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 101 deletions.
4 changes: 2 additions & 2 deletions _dev/js/theme/components/password/usePasswordPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import prestashop from 'prestashop';
/**
* Verify password score.
* Estimate guesses needed to crack the password.
* @param {String} password
* @returns {Promise}
* @param {string} password
* @returns {promise}
*/
window.prestashop.checkPasswordScore = async (password) => {
const zxcvbn = (await import('zxcvbn')).default;
Expand Down
2 changes: 2 additions & 0 deletions _dev/js/theme/core/cart/cartController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import submitVoucherHandler from './handler/voucher/submitVoucherHandler';
import codeLinkSubmitHandler from './handler/voucher/codeLinkSubmitHandler';
import deleteVoucherHandler from './handler/voucher/deleteVoucherHandler';
import addToCartHandler from './handler/cart/addToCartHandler';
import deleteFromCartHandler from './handler/cart/deleteFromCartHandler';

const { on } = useEvent();

Expand All @@ -22,6 +23,7 @@ const cartController = () => {
on(document, 'click', discountCode, codeLinkSubmitHandler);
on(document, 'click', '.js-voucher-delete', deleteVoucherHandler);
on(document, 'click', '[data-button-action="add-to-cart"]', addToCartHandler);
on(document, 'click', '.js-remove-from-cart', deleteFromCartHandler);
};

return {
Expand Down
37 changes: 0 additions & 37 deletions _dev/js/theme/core/cart/cartDelete.js

This file was deleted.

45 changes: 45 additions & 0 deletions _dev/js/theme/core/cart/handler/cart/deleteFromCartHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import useAlertToast from '../../../../components/useAlertToast';
import deleteFromCartRequest from '../../request/deleteFromCartRequest';

const { danger } = useAlertToast();

/**
* Delete product from cart handler
* @param event
* @returns {Promise<void>}
*/
const deleteFromCartHandler = async (event) => {
event.preventDefault();

const button = event.currentTarget;
const { dataset } = button;
const { idProduct, idProductAttribute, idCustomization = 0 } = dataset;

const payload = {
id_product: Number.parseInt(idProduct, 10),
id_product_attribute: Number.parseInt(idProductAttribute, 10),
id_customization: Number.parseInt(idCustomization, 10),
};

const { getRequest } = deleteFromCartRequest(payload);

try {
const resp = await getRequest();

if (!resp.hasError) {
prestashop.emit('updateCart', {
reason: dataset || resp,
resp,
});
} else {
prestashop.emit('handleError', {
eventType: 'deleteProductFromCart',
resp,
});
}
} catch (error) {
danger(error.message);
}
};

export default deleteFromCartHandler;
2 changes: 0 additions & 2 deletions _dev/js/theme/core/cart/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import updateCart from '@js/theme/core/cart/updateCart';
import cartQuantity from '@js/theme/core/cart/cartQuantity';
import cartDelete from '@js/theme/core/cart/cartDelete';
import DOMReady from '@js/theme/utils/DOMReady';
import cartController from './cartController';

Expand All @@ -9,6 +8,5 @@ const { init } = cartController();
DOMReady(() => {
updateCart();
cartQuantity();
cartDelete();
init();
});
20 changes: 10 additions & 10 deletions _dev/js/theme/core/cart/request/addToCartRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDef
* @typedef ServerResponse
* @type {object}
* @property {string|string[]} errors - the errors returned by the server
* @property {number} id_product - product id
* @property {number} id_product_attribute - product attribute id
* @property {number} id_customization - product customization id
* @property {number} quantity - product quantity
* @property {int} id_product - product id
* @property {int} id_product_attribute - product attribute id
* @property {int} id_customization - product customization id
* @property {int} quantity - product quantity
* @property {boolean} success - success flag
* @property {object} cart - cart front object
*/

/**
* Add voucher to cart request
* @param payload {Object} - payload object to send
* @param payload.id_product {number} - product id - Required
* @param payload.qty {number} - product quantity - Required
* @param payload.id_product_attribute {number} - product id attribute - optional pass 0 if not set
* @param payload.id_customization {number} - customization id - optional pass 0 if not set
* @param payload.add {number} - optional
* @param payload.id_product {int} - product id - Required
* @param payload.qty {int} - product quantity - Required
* @param payload.id_product_attribute {int} - product id attribute - optional pass 0 if not set
* @param payload.id_customization {int} - customization id - optional pass 0 if not set
* @param payload.add {int} - optional
* @param payload.action {string} - optional
* @param payload.token {string} - optional
* @param payload.ajax {number} - optional
* @param payload.ajax {int} - optional
* @example
* const payload = {
* id_product: 1, // Required
Expand Down
12 changes: 6 additions & 6 deletions _dev/js/theme/core/cart/request/addVoucherToCartRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDef
* @typedef ServerResponse
* @type {object}
* @property {string|string[]} errors - the errors returned by the server
* @property {number} id_customization - always 0
* @property {number} id_product - always 0
* @property {number} id_product_attribute - always 0
* @property {number} quantity - always 0
* @property {int} id_customization - always 0
* @property {int} id_product - always 0
* @property {int} id_product_attribute - always 0
* @property {int} quantity - always 0
* @property {boolean} success - success flag
* @property {object} cart - cart front object
*/
Expand All @@ -18,10 +18,10 @@ import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDef
* Add voucher to cart request
* @param payload {Object} - payload object to send
* @param payload.discount_name {string} - discount code - Required
* @param payload.addDiscount {number} - optional
* @param payload.addDiscount {int} - optional
* @param payload.action {string} - optional
* @param payload.token {string} - optional
* @param payload.ajax {number} - optional
* @param payload.ajax {int} - optional
* @example
* const payload = {
* discount_name: 'voucherName', // Required
Expand Down
98 changes: 98 additions & 0 deletions _dev/js/theme/core/cart/request/deleteFromCartRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import prestashop from 'prestashop';
import useDefaultHttpRequest from '../../../components/http/useDefaultHttpRequest';
import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDefinition';

/**
* @typedef ServerResponse
* @type {object}
* @property {string|string[]} errors - the errors returned by the server
* @property {int} id_product - product id
* @property {int} id_product_attribute - product attribute id
* @property {int} id_customization - product customization id
* @property {int} quantity - product quantity
* @property {boolean} success - success flag
* @property {object} cart - cart front object
*/

/**
* Add voucher to cart request
* @param payload {Object} - payload object to send
* @param payload.id_product {int} - product id - Required
* @param payload.id_product_attribute {int} - product id attribute - optional pass 0 if not set
* @param payload.id_customization {int} - customization id - optional pass 0 if not set
* @param payload.delete {int} - optional
* @param payload.action {string} - optional
* @param payload.token {string} - optional
* @param payload.ajax {int} - optional
* @example
* const payload = {
* id_product: 1, // Required
* id_product_attribute: 2, // optional
* id_customization: 3, // optional
* };
*
* const { getRequest } = removeFromCartRequest(payload);
*
* try {
* const resp = await getRequest();
* } catch (error) {
* console.error(error);
* }
* @returns {{getRequest: (function(): Promise<ServerResponse>)}}
*/
const deleteFromCartRequest = (payload) => {
const payloadToSend = {
delete: 1,
action: 'update',
ajax: 1,
token: prestashop.static_token,
...payload,
};

const payloadDefinition = {
id_product: {
type: 'int',
required: true,
},
id_product_attribute: {
type: 'int',
required: false,
},
id_customization: {
type: 'int',
required: false,
},
delete: {
type: 'int',
required: true,
},
action: {
type: 'string',
required: true,
},
ajax: {
type: 'int',
required: true,
},
token: {
type: 'string',
required: true,
},
};

const { validatePayload } = useHttpPayloadDefinition(payloadToSend, payloadDefinition);

const validationErrors = validatePayload();

if (validationErrors.length) {
throw Error(validationErrors.join(',\n'));
}

const getRequest = () => useDefaultHttpRequest(prestashop.urls.pages.cart, payloadToSend);

return {
getRequest,
};
};

export default deleteFromCartRequest;
12 changes: 6 additions & 6 deletions _dev/js/theme/core/cart/request/deleteVoucherFromCartRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import useHttpPayloadDefinition from '../../../components/http/useHttpPayloadDef
* @typedef ServerResponse
* @type {object}
* @property {string|string[]} errors - the errors returned by the server
* @property {number} id_customization - always 0
* @property {number} id_product - always 0
* @property {number} id_product_attribute - always 0
* @property {number} quantity - always 0
* @property {int} id_customization - always 0
* @property {int} id_product - always 0
* @property {int} id_product_attribute - always 0
* @property {int} quantity - always 0
* @property {boolean} success - success flag
* @property {object} cart - cart front object
*/

/**
* Add voucher to cart request
* @param payload {Object} - payload object to send
* @param payload.deleteDiscount {number} - discount code id - Required
* @param payload.deleteDiscount {int} - discount code id - Required
* @param payload.action {string} - optional
* @param payload.token {string} - optional
* @param payload.ajax {number} - optional
* @param payload.ajax {int} - optional
* @example
* const payload = {
* deleteDiscount: 2, // required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useDefaultHttpRequest from '../../../components/http/useDefaultHttpReques
* @param url {string} - checkout url to send request
* @param payload {object} - request payload
* @param payload.delivery_option[id] {string} - delivery option id with id_address_delivery
* @param payload.ajax {number} - optional
* @param payload.ajax {int} - optional
* @param payload.action {string} - optional
* @example
* const payload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { each } from '../../../utils/DOMHelpers';

/**
* Change the color of the edit button for the wrong address
* @param {Boolean} enabled - true if button should be dangered or false otherwise
* @param {Number} id - address id
* @param {String} type - address type (delivery or invoice)
* @param {boolean} enabled - true if button should be dangered or false otherwise
* @param {int} id - address id
* @param {string} type - address type (delivery or invoice)
*/
const switchEditAddressButtonColor = (
enabled,
Expand Down
2 changes: 0 additions & 2 deletions _dev/js/theme/frontAPI/apiAction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import refreshCartPageAction from '@js/theme/frontAPI/cart/refreshCartPageAction';
import updateCartQuantityAction from '@js/theme/frontAPI/cart/updateCartQuantityAction';
import deleteFromCartAction from '@js/theme/frontAPI/cart/deleteFromCartAction';
import updateProductAction from '@js/theme/frontAPI/product/updateProductAction';
import updateListingFacetsAction from '@js/theme/frontAPI/listing/updateListingFacetsAction';
import updateAddressAction from '@js/theme/frontAPI/address/updateAddressAction';
Expand All @@ -17,7 +16,6 @@ prestashop.addAction = (actionName, actionFunction) => {

prestashop.addAction('refreshCartPage', refreshCartPageAction);
prestashop.addAction('updateCartQuantity', updateCartQuantityAction);
prestashop.addAction('deleteFromCart', deleteFromCartAction);
prestashop.addAction('updateProduct', updateProductAction);
prestashop.addAction('updateListingFacets', updateListingFacetsAction);
prestashop.addAction('updateAddress', updateAddressAction);
30 changes: 0 additions & 30 deletions _dev/js/theme/frontAPI/cart/deleteFromCartAction.js

This file was deleted.

2 changes: 1 addition & 1 deletion _dev/js/theme/utils/debounce.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Debounce function
* @param func {function} - function to debounce
* @param timeout {number} - timeout in ms (default: 300)
* @param timeout {int} - timeout in ms (default: 300)
* @returns {(function(...[*]): void)|*}
*/
const debounce = (func, timeout = 300) => {
Expand Down
Loading

0 comments on commit ee7a7f5

Please sign in to comment.