Home Manual Reference Source Repository

src/entities/Shop.js

import Evee from 'evee';
import Queue from 'promise-queue';
import deepEqual from 'deep-equal';
import _ from 'lodash';

import config from 'config';
import log from 'log';

const TAG = 'Shop';


/**
 * @typedef {Object}  ShopifyShop
 * @property {ShopifyAddress} address
 * @property {Number} collections_count
 * @property {String} currency
 * @property {String} description
 * @property {String} domain
 * @property {String} email
 * @property {String[]} enabled_payment_types
 * @property {String} locale
 * @property {String} money_format
 * @property {String} money_format_with_currency
 * @property {String} name
 * @property {String} password_message
 * @property {String} permanent_domain
 * @property {ShopifyPolicy[]} policies
 * @property {String} refund_policy
 * @property {String} secure_url
 * @property {String} terms_of_service
 * @property {String[]} types
 * @property {String} url
 * @property {String[]} vendors
 */

/**
 * @typedef {Object} ShopifyAddress
 * @property {String} address1
 * @property {String} address2
 * @property {String} city
 * @property {String|null} company
 * @property {String} country
 * @property {String} country_code
 * @property {String|null} first_name
 * @property {String|null} last_name
 * @property {Number|null} latitude
 * @property {Number|null} longitude
 * @property {String|null} name
 * @property {String|null} phone
 * @property {String|null} province
 * @property {String|null} province_code
 * @property {String|null} zip
 */

/**
 * @typedef {Object} ShopifyPolicy
 * @property {String} body
 * @property {String} title
 */

/**
 * Shopify Shop entity.
 * @extends {evee}
 */
export default class Shop extends Evee {
    /**
     * Construct a new instance of the shop entity.
     */
    constructor() {
        super();

        /** @type {ShopifyAddress} */
        this.address = null;
        /** @type {Number} */
        this.collections_count = null;
        /** @type {String} */
        this.currency = null;
        /** @type {String} */
        this.description = null;
        /** @type {String} */
        this.domain = null;
        /** @type {String} */
        this.email = null;
        /** @type {String[]} */
        this.enabled_payment_types = [];
        /** @type {String} */
        this.locale = null;
        /** @type {String} */
        this.money_format = null;
        /** @type {String} */
        this.money_format_with_currency = null;
        /** @type {String} */
        this.name = null;
        /** @type {String} */
        this.password_message = null;
        /** @type {String} */
        this.permanent_domain = null;
        /** @type {ShopifyPolicy[]} */
        this.policies = [];
        /** @type {String} */
        this.refund_policy = null;
        /** @type {String} */
        this.secure_url = null;
        /** @type {String} */
        this.terms_of_service = null;
        /** @type {String[]} */
        this.types = [];
        /** @type {String} */
        this.url = null;
        /** @type {String[]} */
        this.vendors = [];
    }

    /**
     * Initialize the shop entity with the global shop state.
     * @param {ShopifyShop} shopData - Shopify shop object.
     */
    initialize(shopData) {
        this._loadFromShopifyShop(shopData);

        this.emit('init', { shop: this });
        log.send(log.DEBUG, TAG, 'Loaded.');
    }

    /**
     * Loads properties from the given shop.
     * @param {ShopifyShop} shop
     */
    _loadFromShopifyShop(shop) {
        this.address = shop.address;
        this.collections_count = shop.collections_count;
        this.currency = shop.currency;
        this.description = shop.description;
        this.domain = shop.domain;
        this.email = shop.email;
        this.enabled_payment_types = shop.enabled_payment_types;
        this.locale = shop.locale;
        this.money_format = shop.money_format;
        this.money_format_with_currency = shop.money_format_with_currency;
        this.name = shop.name;
        this.password_message = shop.password_message;
        this.permanent_domain = shop.permanent_domain;
        this.policies = shop.policies;
        this.refund_policy = shop.refund_polcy;
        this.secure_url = shop.secure_url;
        this.terms_of_service = shop.terms_of_service;
        this.types = shop.types;
        this.url = shop.url;
        this.vendors = shop.vendors;
    }

    /**
     * Normalizes a relative URL according to the current pathname.
     * @param {String} rel
     * @returns {String}
     */
    _normalizeRelativeUrl(rel) {
        var normal = relative;
        if (normal.length > 0 && normal[0] != '/') {
            normal = window.location.pathname + normal;
        }
    }
        

    /**
     * Generates an absolute URL containing the shop primary domain.
     * @param {String} relative - A relative URL to make absolute (i.e. '/collections/all')
     * @returns {String}
     */
    makeAbsoluteUrl(relative) {
        var normal = this._normalizeRelativeUrl(relative);
        return this.secure_url + normal;
    }

    /**
     * Generates an absolute URL containing the shop's myshopify domain.
     * @param {String} relative - A relative URL to make absolute (i.e. '/collections/all')
     * @returns {String}
     */
    makePermanentUrl(relative) {
        var normal = this._normalizeRelativeUrl(relative);
        return this.secure_url.replace(this.domain, this.permanent_domain) + normal;
    }


}