Choices/assets/scripts/src/store/index.js

150 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-05-04 10:02:22 +02:00
import { createStore } from 'redux';
2016-05-07 13:36:50 +02:00
import rootReducer from './../reducers/index.js';
2016-05-04 10:02:22 +02:00
2016-08-14 23:14:37 +02:00
export default class Store {
2016-05-04 10:02:22 +02:00
constructor() {
this.store = createStore(
2016-08-14 23:14:37 +02:00
rootReducer,
window.devToolsExtension ? window.devToolsExtension() : undefined
);
2016-05-04 10:02:22 +02:00
}
2016-08-14 23:14:37 +02:00
/**
* Get store object (wrapping Redux method)
* @return {Object} State
*/
2016-05-04 10:02:22 +02:00
getState() {
return this.store.getState();
}
/**
* Dispatch event to store (wrapped Redux method)
* @param {Function} action Action function to trigger
* @return
*/
dispatch(action) {
this.store.dispatch(action);
2016-05-04 10:02:22 +02:00
}
/**
* Subscribe store to function call (wrapped Redux method)
* @param {Function} onChange Function to trigger when state changes
* @return
*/
subscribe(onChange) {
this.store.subscribe(onChange);
2016-05-04 10:02:22 +02:00
}
/**
* Get items from store
* @return {Array} Item objects
*/
getItems() {
const state = this.store.getState();
return state.items;
}
/**
* Get active items from store
* @return {Array} Item objects
*/
getItemsFilteredByActive() {
const items = this.getItems();
const values = items.filter((item) => {
return item.active === true;
}, []);
return values;
}
/**
* Get items from store reduced to just their values
* @return {Array} Item objects
*/
2016-05-08 01:02:52 +02:00
getItemsReducedToValues(items = this.getItems()) {
2016-05-04 10:02:22 +02:00
const values = items.reduce((prev, current) => {
prev.push(current.value);
return prev;
}, []);
return values;
}
/**
2016-08-14 23:14:37 +02:00
* Get choices from store
2016-05-04 10:02:22 +02:00
* @return {Array} Option objects
*/
getChoices() {
2016-05-04 10:02:22 +02:00
const state = this.store.getState();
return state.choices;
2016-05-04 10:02:22 +02:00
}
/**
* Get active choices from store
2016-05-04 10:02:22 +02:00
* @return {Array} Option objects
*/
getChoicesFilteredByActive() {
const choices = this.getChoices();
const values = choices.filter((choice) => {
return choice.active === true;
2016-08-14 23:14:37 +02:00
}, []);
2016-05-04 10:02:22 +02:00
return values;
}
/**
* Get selectable choices from store
2016-05-04 10:02:22 +02:00
* @return {Array} Option objects
*/
getChoicesFilteredBySelectable() {
const choices = this.getChoices();
const values = choices.filter((choice) => {
return choice.disabled !== true;
2016-08-14 23:14:37 +02:00
}, []);
2016-05-04 10:02:22 +02:00
return values;
}
/**
* Get single choice by it's ID
* @return {Object} Found choice
*/
getChoiceById(id) {
2016-08-14 23:14:37 +02:00
if (id) {
const choices = this.getChoicesFilteredByActive();
const foundChoice = choices.find((choice) => choice.id === parseInt(id, 10));
return foundChoice;
}
return false;
}
2016-05-04 10:02:22 +02:00
/**
* Get groups from store
* @return {Array} Group objects
*/
getGroups() {
const state = this.store.getState();
return state.groups;
}
/**
* Get active groups from store
* @return {Array} Group objects
*/
getGroupsFilteredByActive() {
const groups = this.getGroups();
const choices = this.getChoices();
2016-05-04 10:02:22 +02:00
const values = groups.filter((group) => {
const isActive = group.active === true && group.disabled === false;
2016-06-28 15:27:25 +02:00
const hasActiveOptions = choices.some((choice) => {
return choice.active === true && choice.disabled === false;
2016-05-04 10:02:22 +02:00
});
2016-08-14 23:14:37 +02:00
return isActive && hasActiveOptions;
}, []);
2016-05-04 10:02:22 +02:00
return values;
}
2016-08-14 23:14:37 +02:00
}
2016-05-04 10:02:22 +02:00
module.exports = Store;