Choices/src/scripts/src/store/store.js

177 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-05-04 10:02:22 +02:00
import { createStore } from 'redux';
2017-08-15 10:29:42 +02:00
import rootReducer from './../reducers/index';
2016-05-04 10:02:22 +02:00
2016-08-14 23:14:37 +02:00
export default class Store {
constructor() {
this.store = createStore(
2017-08-15 10:29:42 +02:00
rootReducer,
window.devToolsExtension ?
window.devToolsExtension() :
undefined,
);
}
/**
2017-10-10 13:06:00 +02:00
* Subscribe store to function call (wrapped Redux method)
* @param {Function} onChange Function to trigger when state changes
* @return
*/
2017-10-10 13:06:00 +02:00
subscribe(onChange) {
this.store.subscribe(onChange);
}
/**
* Dispatch event to store (wrapped Redux method)
* @param {Function} action Action function to trigger
* @return
*/
dispatch(action) {
this.store.dispatch(action);
}
/**
2017-10-10 13:06:00 +02:00
* Get store object (wrapping Redux method)
* @return {Object} State
*/
2017-10-10 13:06:00 +02:00
getState() {
return this.store.getState();
}
/**
* 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 => item.active === true);
return values;
}
/**
* Get highlighted items from store
* @return {Array} Item objects
*/
getItemsFilteredByHighlighted() {
const items = this.getItems();
const values = items.filter(item => item.active && item.highlighted);
return values;
}
/**
* Get choices from store
* @return {Array} Option objects
*/
getChoices() {
const state = this.store.getState();
return state.choices;
}
/**
* Get active choices from store
* @return {Array} Option objects
*/
getChoicesFilteredByActive() {
const choices = this.getChoices();
2017-08-02 15:05:26 +02:00
const values = choices.filter(choice => choice.active === true);
return values;
}
/**
* Get selectable choices from store
* @return {Array} Option objects
*/
getChoicesFilteredBySelectable() {
const choices = this.getChoices();
2017-08-02 15:05:26 +02:00
const values = choices.filter(choice => choice.disabled !== true);
return values;
}
2017-08-02 15:05:26 +02:00
/**
* Get choices that can be searched (excluding placeholders)
* @return {Array} Option objects
*/
getSearchableChoices() {
const filtered = this.getChoicesFilteredBySelectable();
return filtered.filter(choice => choice.placeholder !== true);
}
/**
* Get single choice by it's ID
* @return {Object} Found choice
*/
getChoiceById(id) {
if (id) {
const choices = this.getChoicesFilteredByActive();
2017-08-15 10:29:42 +02:00
const foundChoice = choices.find(choice => choice.id === parseInt(id, 10));
return foundChoice;
2016-05-04 10:02:22 +02:00
}
return false;
}
2017-10-10 13:35:05 +02:00
/**
* Get placeholder choice from store
* @return {Object} Found placeholder
*/
getPlaceholderChoice() {
const choices = this.getChoices();
const placeholderChoice = [...choices]
.reverse()
.find(choice => choice.placeholder === true);
return placeholderChoice;
}
/**
* 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();
const values = groups.filter((group) => {
const isActive = group.active === true && group.disabled === false;
2017-08-15 10:29:42 +02:00
const hasActiveOptions = choices.some(choice =>
choice.active === true && choice.disabled === false,
);
return isActive && hasActiveOptions;
}, []);
return values;
}
2016-10-18 20:23:07 +02:00
/**
* Get group by group id
* @param {Number} id Group ID
* @return {Object} Group data
*/
getGroupById(id) {
const groups = this.getGroups();
2017-10-10 13:35:05 +02:00
const foundGroup = groups.find(group => group.id === parseInt(id, 10));
2016-10-18 20:23:07 +02:00
return foundGroup;
}
2016-08-14 23:14:37 +02:00
}