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

Fetches the offers data for a specific product.

Parameters

Takes an object with the following keys:

NameTypeDescription
categoryIdstringID of the category (used to fetch the offers)
productIdstringID of the product (this is not used to fetch the offer).
pricenumberPrice 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

Fetches the offers data for multiple products.

Parameters

Takes an array of objects with the following keys:

NameTypeDescription
categoryIdstringID of the category (used to fetch the offers)
productIdstringID of the product
pricenumberPrice 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

Sets the quantity of a product.

Parameters

NameTypeDescription
productIdstringID of the product
quantitynumberQuantity of product

Usage

statelyInstance.setProductQuantity('fg-2345-gd' /* Product ID */, 5 /* quantity */);

getProductQuantity

Gets the quantity of a product.

Parameters

NameTypeDescription
productIdstringID of the product

Usage

const quantity = statelyInstance.getProductQuantity('fg-2345-gd' /* Product ID */);

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

NameTypeDescription
offerIdstringID of the offer
productIdstringID 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

Selects an offer for a product. This method should be called when selecting the offer in the UI.

Parameters

NameTypeDescription
productIdstringID of the product
offerId?stringID 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

Attaches the currently selected offer of a product.

Parameters

NameTypeDescription
productIdstringID of the product

Emits the STORE_UPDATED event once the data is updated in the store.

Usage

stately.attachSelectedOffer('as-5435-vb' /* productId */);

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

This method deletes the offers data for a specific product. The selection is cleared, if needed.

Parameters

NameTypeDescription
productIdstringID of the product

Emits the STORE_UPDATED event once the data is updated in the store.

Usage

stately.removeOffer('as-5435-vb' /* productId */);

cancelAttachedOffer

This method removes an attached offer for a specific product.

Parameters

NameTypeDescription
productIdstringID of the product

Emits the STORE_UPDATED event once the data is updated in the store.

Usage

statelyInstance.cancelAttachedOffer('as-5435-vb' /* */);

Getters

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:

NameTypeDescription
offerobjectthe offer object (see getSelectedOffer returned value)
quantitynumberNumber of offers to attach to the product

Usage

const attachedOffers = statelyInstance.getAttachedOffers();

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:

NameTypeDescription
idstringId of the offer
objectstringThe covered object type
coveragestringList of coverage options
termnumberDuration of the insurance in months
coverage_idstringId of the coverage

Usage

const selectedOffer = statelyInstance.getSelectedOffer();