import {createStore} from 'fluxxor'; import {pluck} from 'lodash'; import {c as generic} from '../clients/generic'; import {c as layerMembers} from '../clients/layer_members'; import {LAYER as constants} from '../constants'; var fieldMapping = { ATTRIBUTES: 'layer', OWNED_RESOURCES: 'owned', ALL_ROLES: 'roles', LAYER_MEMBERS_USE: 'users', LAYER_MEMBERS_ADMIN: 'admins', RESOURCES: 'resources' }; export default createStore({ initialize() { this.data = this._fresh(); this.bindActions( constants.LOAD, this.onLoad, constants.LOAD_SUCCESS, this.onLoadSuccess, constants.LOAD_FAIL, this.onLoadFail ); }, _fresh() { return { layer: { hosts: [] }, admins: [], users: [], loading: { layer: true, owned: true, roles: true, users: true, admins: true, resources: true } }; }, onLoad(payload) { if (typeof payload.type === 'undefined') { this.data = this._fresh(); this.data.identifier = payload.id; this.emit('change'); } else if (payload.id === this.data.identifier) { this.turn(payload.type, true); this.emit('change'); } }, onLoadSuccess(payload) { if (payload.id === this.data.identifier) { this.turn(payload.type, false); this.setData(payload); this.emit('change'); } }, onLoadFail(payload) { this.turn(payload.type, false); }, turn(type, state) { var fields = fieldMapping[type]; if (typeof fields === 'object') { fields.forEach((field) => { this.data.loading[field] = state; }); } else { this.data.loading[fields] = state; } }, setData(payload) { switch (payload.type) { case generic.ATTRIBUTES: this.data.layer = payload.res.body; break; case generic.OWNED_RESOURCES: this.data.owned = payload.res.body; break; case generic.ALL_ROLES: this.data.roles = payload.res.body; break; case layerMembers.LAYER_MEMBERS_USE: this.data.users = pluck(payload.res.body, 'member'); break; case layerMembers.LAYER_MEMBERS_ADMIN: this.data.admins = pluck(payload.res.body, 'member'); break; case 'RESOURCES': this.data.resources = payload.res.body; break; default: } }, getData() { return this.data; } });