UNPKG

kibana-with-account

Version:

kibana-with-account is develop based kibana project

157 lines (128 loc) 4.28 kB
var Dispatcher = require('flux-dispatcher'); var FluxStore = require('flux-store'); var cloneDeep = require('lodash/lang/cloneDeep'); var Constants = require('../constants/UserRequest'); var parseValidation = require('../../../helpers/parseValidation'); var ActionTypes = Constants.ActionTypes; var Store = FluxStore.extend({ dispatcher: Dispatcher, state: {}, defaultState: { results: { hydrated: false, loading: false, success: false, error: undefined, data: [], pages: {}, items: {} }, details: { hydrated: false, fetchFailure: false, loading: false, success: false, error: undefined, hasError: {}, help: {}, _id: undefined, user: undefined, opType: undefined, timeCreated: undefined, opParameter: undefined } }, getState: function () { return this.state; }, getResults: function () { return this.state.results; }, getDetails: function () { return this.state.details; }, reset: function () { this.state = { results: cloneDeep(this.defaultState.results), details: cloneDeep(this.defaultState.details) }; }, resetResults: function () { this.state.results = cloneDeep(this.defaultState.results); }, resetDetails: function () { this.state.details = cloneDeep(this.defaultState.details); }, resetValidationErrors: function (pivot) { this.state[pivot].error = undefined; this.state[pivot].hasError = {}; this.state[pivot].help = {}; }, handleValidationErrors: function (pivot, data) { var validation = parseValidation(data.validation, data.message); this.state[pivot].hasError = validation.hasError; this.state[pivot].help = validation.help; this.state[pivot].error = validation.error; }, onDispatcherAction: function (payload) { var action = payload.action; if (ActionTypes.GET_RESULTS === action.type) { this.state.results.loading = true; this.state.results.hydrated = false; this.state.results.success = false; this.emitChange(); } if (ActionTypes.GET_RESULTS_RESPONSE === action.type) { this.state.results.loading = false; this.state.results.hydrated = true; this.state.results.success = action.data.success; this.state.results.data = action.data.data; this.state.results.pages = action.data.pages; this.state.results.items = action.data.items; this.emitChange(); } if (ActionTypes.GET_DETAILS === action.type) { this.state.details.loading = true; this.state.details.hydrated = false; this.state.details.success = false; this.emitChange(); } if (ActionTypes.GET_DETAILS_RESPONSE === action.type) { this.handleValidationErrors('details', action.data); this.state.details.loading = false; this.state.details.hydrated = true; this.state.details.fetchFailure = action.data.fetchFailure; this.state.details.success = action.data.success; this.state.details._id = action.data._id; this.state.details.username = action.data.user.name; this.state.details.opType = action.data.opType; this.state.details.opParameter = action.data.opParameter; this.state.details.timeCreated = action.data.timeCreated; this.state.details.isClosed = action.data.isClosed; if (action.data.isClosed) { this.state.details.timeExecutor = action.data.timeExecutor; } this.emitChange(); } if (ActionTypes.SAVE_DETAILS === action.type) { this.state.details.loading = true; this.emitChange(); } if (ActionTypes.SAVE_DETAILS_RESPONSE === action.type) { this.state.details.loading = false; this.state.details.success = action.data.success; this.handleValidationErrors('details', action.data); if (action.data.success) { setTimeout(function () { this.state.details.success = undefined; this.emitChange(); }.bind(this), 2500); this.resetValidationErrors('details'); this.state.details.pivot = action.data.pivot; this.state.details.name = action.data.name; } this.emitChange(); } } }); module.exports = Store;