-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delete from cart action, minor jsdocs fixes
- Loading branch information
Showing
15 changed files
with
175 additions
and
101 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 was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
_dev/js/theme/core/cart/handler/cart/deleteFromCartHandler.js
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,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; |
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
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,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; |
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
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.