Sha256: fa31c8b99fcaa588b2f3bb123b2111171edd43e082c5a167f311cebc3a8020dc

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

import { observable, observe } from 'mobx';
import { keysIn, pick, assign, get } from 'lodash';
import { persist, create as createHydrator } from 'mobx-persist';

import Extensions from '../extensions';

const STORAGE_KEY = 'hippo-user-data';

export default class Config {
    @persist @observable api_host = get(window, 'location.origin', '');
    @persist @observable api_path = '/api';
    @persist @observable access_token;
    @persist @observable root_view;
    @persist @observable assets_path_prefix = '/assets';
    @persist @observable website_domain;
    @persist @observable product_name;
    @persist('list') @observable screen_ids = [];
    @persist @observable user_info;

    @observable user;
    @observable isIntialized = false;

    static create(hydrationConfig) {
        const hydrate = createHydrator(hydrationConfig);
        const ConfigInstance = new Config();
        hydrate('config', ConfigInstance).then(() => (ConfigInstance.isIntialized = true));
        return ConfigInstance;
    }

    constructor() {
        observe(this, 'user', ({ newValue }) => {
            if (newValue) { this.setUserData(); }
        });
    }

    update(attrs) {
        assign(this, pick(attrs, keysIn(this)));
        Extensions.setBootstrapData(attrs);
    }

    setScreenData() {
        if (this.screens && this.data) {
            this.screens.configure(this.data.screens);
        }
    }

    setUserData() {
        if (this.user && this.data) {
            this.user.set(this.data.user);
            this.user.access = this.data.access;
        }
    }

    reset() {
        this.data = {};
        this.access_token = null;
        if (this.user) { this.user.reset(); }
    }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hippo-fw-0.9.5 client/hippo/models/config.js