Methods reference
The Stately object holds the state of the offers, provides methods to manipulate the state of the offers, and reports the changes using events. The class implements the EventEmiter and the scripts can listen to changes using the method on
.
There are three types of methods on the Stately object:
- setters update the state and emit a
STORE_UPDATED
event when the data in the store changes - getters read the current state
- EventEmiter methods for registering to events
Setters
The setters emits an event when it updates the store. And they all return the instance of Stately, making them chainable.
addProduct
addProduct
Fetches the offers data for a specific product.
Parameters
Takes an object with the following keys:
Name | Type | Description |
---|---|---|
categoryId | string | ID of the category (used to fetch the offers) |
productId | string | ID of the product (this is not used to fetch the offer). |
price | number | Price of the product when retrieving the offer |
Emits STORE_UPDATED
event when inserting the offer data retrieved from the server.
Usage
statelyInstance.addProduct({ productId: 'as-5435-vb', price: 5000 });
addProducts
addProducts
Fetches the offers data for multiple products.
Parameters
Takes an array of objects with the following keys:
Name | Type | Description |
---|---|---|
categoryId | string | ID of the category (used to fetch the offers) |
productId | string | ID of the product |
price | number | Price of the product when retrieving the offer |
Emits STORE_UPDATED
event when inserting the offer data retrieved from the server.
Usage
statelyInstance.addProducts([
{ productId: 'fg-5645-vb', price: 8000 },
{ productId: 'as-5435-vb', price: 5000 },
]);
setProductQuantity
setProductQuantity
Sets the quantity of a product.
Parameters
Name | Type | Description |
---|---|---|
productId | string | ID of the product |
quantity | number | Quantity of product |
Usage
statelyInstance.setProductQuantity('fg-2345-gd' /* Product ID */, 5 /* quantity */);
getProductQuantity
getProductQuantity
Gets the quantity of a product.
Parameters
Name | Type | Description |
---|---|---|
productId | string | ID of the product |
Usage
const quantity = statelyInstance.getProductQuantity('fg-2345-gd' /* Product ID */);
setAttachedOffer
setAttachedOffer
Attaches an offer to a product. This method should be called when adding the product to the basket.
You can use this method to update the quantity.
Parameters
Name | Type | Description |
---|---|---|
offerId | string | ID of the offer |
productId | string | ID of the product |
Emits the STORE_UPDATED
event once the data is updated in state and local storage.
Usage
stately.setAttachedOffer('evy-offer-54645' /* offerId */, 'as-5435-vb' /* productId */);
addSelectedOffer
addSelectedOffer
Selects an offer for a product. This method should be called when selecting the offer in the UI.
Parameters
Name | Type | Description |
---|---|---|
productId | string | ID of the product |
offerId | ?string | ID of the offer. Can be undefined, it deselects the offers |
Emits the STORE_UPDATED
event once the data is updated in the store and synchronized in the local storage.
Usage
Selects an offer:
stately.addSelectedOffer('as-5435-vb' /* productId */, 'evy-offer-54645' /* offerId */);
Deselects an offer:
stately.addSelectedOffer('as-5435-vb' /* productId */, 'evy-offer-54645' /* offerId */);
attachSelectedOffer
attachSelectedOffer
Attaches the currently selected offer of a product.
Parameters
Name | Type | Description |
---|---|---|
productId | string | ID of the product |
Emits the STORE_UPDATED
event once the data is updated in the store.
Usage
stately.attachSelectedOffer('as-5435-vb' /* productId */);
clearOffers
clearOffers
This method removes all attached offers and their data from the state and the local storage.
Parameters
It takes no parameters and it emits the STORE_UPDATED
event once the data is updated in state and local storage.
Usage
statelyInstance.clearOffers();
removeOffer
removeOffer
This method deletes the offers data for a specific product. The selection is cleared, if needed.
Parameters
Name | Type | Description |
---|---|---|
productId | string | ID of the product |
Emits the STORE_UPDATED
event once the data is updated in the store.
Usage
stately.removeOffer('as-5435-vb' /* productId */);
cancelAttachedOffer
cancelAttachedOffer
This method removes an attached offer for a specific product.
Parameters
Name | Type | Description |
---|---|---|
productId | string | ID of the product |
Emits the STORE_UPDATED
event once the data is updated in the store.
Usage
statelyInstance.cancelAttachedOffer('as-5435-vb' /* */);
Getters
getAttachedOffers
getAttachedOffers
Returns all the currently attached offers.
It takes no parameters.
Returned value
The returned values is an object. The keys are the product ids, and the values are the attached offers.
The attached offers have the following keys:
Name | Type | Description |
---|---|---|
offer | object | the offer object (see getSelectedOffer returned value) |
quantity | number | Number of offers to attach to the product |
Usage
const attachedOffers = statelyInstance.getAttachedOffers();
getSelectedOffer
getSelectedOffer
Returns the currently selected offer for a product.
It takes no parameters.
Returned value
The value returned is an object with the following keys:
Name | Type | Description |
---|---|---|
id | string | Id of the offer |
object | string | The covered object type |
coverage | string | List of coverage options |
term | number | Duration of the insurance in months |
coverage_id | string | Id of the coverage |
Usage
const selectedOffer = statelyInstance.getSelectedOffer();
Updated 12 days ago