shopperman
Version:
shopping cart ui for shopify stores
38 lines (30 loc) • 976 B
text/typescript
import {observable, computed, action} from "mobx"
import {CurrencyControl} from "./currency-control"
import {ProductOptions, ElementAttributes} from "./stores-interfaces"
/**
* Represent a single ecommerce product
*/
export class Product {
readonly id: string
readonly value: number
readonly title: string
readonly description: string
private readonly currencyControl: CurrencyControl
precision: number = null
attributes: {[key: string]: string} = {}
constructor({precision, attributes, ...options}: ProductOptions) {
Object.assign(this, options)
this.setPrecision(precision)
this.setAttributes(attributes)
}
get price(): string {
const {currencyControl, value, precision} = this
return currencyControl.convertAndFormat(value, precision)
}
setPrecision(precision: number) {
this.precision = precision
}
setAttributes(attributes: ElementAttributes = {}) {
this.attributes = attributes
}
}