Choices/src/scripts/store/store.js

160 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-05-04 10:02:22 +02:00
import { createStore } from 'redux';
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() {
2018-05-24 10:22:07 +02:00
this._store = createStore(
2017-08-15 10:29:42 +02:00
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__(),
);
}
/**
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) {
2018-05-24 10:22:07 +02:00
this._store.subscribe(onChange);
}
/**
* Dispatch event to store (wrapped Redux method)
* @param {Function} action Action function to trigger
* @return
*/
dispatch(action) {
2018-05-24 10:22:07 +02:00
this._store.dispatch(action);
}
/**
2017-10-10 13:06:00 +02:00
* Get store object (wrapping Redux method)
* @return {Object} State
*/
2018-04-24 13:54:45 +02:00
get state() {
2018-05-24 10:22:07 +02:00
return this._store.getState();
}
/**
* Get items from store
* @return {Array} Item objects
*/
2018-04-24 13:54:45 +02:00
get items() {
return this.state.items;
}
/**
* Get active items from store
* @return {Array} Item objects
*/
2018-04-24 14:52:13 +02:00
get activeItems() {
2018-04-24 14:23:26 +02:00
return this.items.filter(item => item.active === true);
}
/**
* Get highlighted items from store
* @return {Array} Item objects
*/
2018-04-24 14:52:13 +02:00
get highlightedActiveItems() {
2018-04-24 14:23:26 +02:00
return this.items.filter(item => item.active && item.highlighted);
}
/**
* Get choices from store
* @return {Array} Option objects
*/
2018-04-24 14:23:26 +02:00
get choices() {
2018-04-24 13:54:45 +02:00
return this.state.choices;
}
/**
* Get active choices from store
* @return {Array} Option objects
*/
2018-04-24 14:52:13 +02:00
get activeChoices() {
const { choices } = this;
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
*/
2018-04-24 14:52:13 +02:00
get selectableChoices() {
2018-04-24 14:49:57 +02:00
return this.choices.filter(choice => choice.disabled !== true);
}
2017-08-02 15:05:26 +02:00
/**
* Get choices that can be searched (excluding placeholders)
* @return {Array} Option objects
*/
2018-04-24 14:49:57 +02:00
get searchableChoices() {
2018-04-24 14:52:13 +02:00
return this.selectableChoices.filter(choice => choice.placeholder !== true);
}
2017-10-10 13:35:05 +02:00
/**
* Get placeholder choice from store
* @return {Object} Found placeholder
*/
2018-04-24 14:49:57 +02:00
get placeholderChoice() {
return [...this.choices]
2017-10-10 13:35:05 +02:00
.reverse()
.find(choice => choice.placeholder === true);
}
/**
* Get groups from store
* @return {Array} Group objects
*/
2018-04-24 14:49:57 +02:00
get groups() {
2018-04-24 13:54:45 +02:00
return this.state.groups;
}
/**
* Get active groups from store
* @return {Array} Group objects
*/
2018-04-24 14:52:13 +02:00
get activeGroups() {
const { groups, choices } = this;
return groups.filter(group => {
const isActive = group.active === true && group.disabled === false;
const hasActiveOptions = choices.some(
choice => choice.active === true && choice.disabled === false,
2017-08-15 10:29:42 +02:00
);
return isActive && hasActiveOptions;
}, []);
2018-04-24 14:49:57 +02:00
}
/**
* Get loading state from store
* @return {Boolean} Loading State
*/
isLoading() {
return this.state.general.loading;
}
2018-04-24 14:49:57 +02:00
/**
* Get single choice by it's ID
* @return {Object} Found choice
*/
getChoiceById(id) {
if (id) {
return this.activeChoices.find(choice => choice.id === parseInt(id, 10));
2018-04-24 14:49:57 +02:00
}
return false;
}
2016-10-18 20:23:07 +02:00
/**
* Get group by group id
* @param {Number} id Group ID
* @return {Object} Group data
*/
getGroupById(id) {
2018-04-24 14:49:57 +02:00
return this.groups.find(group => group.id === parseInt(id, 10));
2016-10-18 20:23:07 +02:00
}
2016-08-14 23:14:37 +02:00
}