2019-12-23 19:22:54 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import { createStore, Store as IStore, AnyAction } from 'redux';
|
2019-10-21 21:03:57 +02:00
|
|
|
import rootReducer from '../reducers/index';
|
2019-12-23 19:22:54 +01:00
|
|
|
import { Choice, Group, Item, State } from '../interfaces';
|
2019-11-03 14:18:16 +01:00
|
|
|
|
2016-08-14 23:14:37 +02:00
|
|
|
export default class Store {
|
2019-12-23 19:22:54 +01:00
|
|
|
_store: IStore;
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
constructor() {
|
2018-05-24 10:22:07 +02:00
|
|
|
this._store = createStore(
|
2017-08-15 10:29:42 +02:00
|
|
|
rootReducer,
|
2019-12-23 19:22:54 +01:00
|
|
|
(window as any).__REDUX_DEVTOOLS_EXTENSION__ &&
|
|
|
|
(window as any).__REDUX_DEVTOOLS_EXTENSION__(),
|
2016-09-05 23:04:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-10 13:06:00 +02:00
|
|
|
* Subscribe store to function call (wrapped Redux method)
|
2016-09-05 23:04:15 +02:00
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
subscribe(onChange: () => void): void {
|
2018-05-24 10:22:07 +02:00
|
|
|
this._store.subscribe(onChange);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch event to store (wrapped Redux method)
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
dispatch(action: AnyAction): void {
|
2018-05-24 10:22:07 +02:00
|
|
|
this._store.dispatch(action);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-10 13:06:00 +02:00
|
|
|
* Get store object (wrapping Redux method)
|
2016-09-05 23:04:15 +02:00
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get state(): State {
|
2018-05-24 10:22:07 +02:00
|
|
|
return this._store.getState();
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get items from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get items(): Item[] {
|
2018-04-24 13:54:45 +02:00
|
|
|
return this.state.items;
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get active items from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get activeItems(): Item[] {
|
2018-04-24 14:23:26 +02:00
|
|
|
return this.items.filter(item => item.active === true);
|
2017-11-29 14:25:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-28 14:55:44 +02:00
|
|
|
* Get highlighted items from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get highlightedActiveItems(): Item[] {
|
2018-04-24 14:23:26 +02:00
|
|
|
return this.items.filter(item => item.active && item.highlighted);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get choices from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get choices(): Choice[] {
|
2018-04-24 13:54:45 +02:00
|
|
|
return this.state.choices;
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get active choices from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get activeChoices(): Choice[] {
|
2019-11-03 14:18:16 +01:00
|
|
|
return this.choices.filter(choice => choice.active === true);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get selectable choices from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get selectableChoices(): Choice[] {
|
2018-04-24 14:49:57 +02:00
|
|
|
return this.choices.filter(choice => choice.disabled !== true);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 15:05:26 +02:00
|
|
|
/**
|
|
|
|
* Get choices that can be searched (excluding placeholders)
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get searchableChoices(): Choice[] {
|
2018-04-24 14:52:13 +02:00
|
|
|
return this.selectableChoices.filter(choice => choice.placeholder !== true);
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 13:35:05 +02:00
|
|
|
/**
|
|
|
|
* Get placeholder choice from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get placeholderChoice(): Choice | undefined {
|
2018-04-24 14:49:57 +02:00
|
|
|
return [...this.choices]
|
2017-10-10 13:35:05 +02:00
|
|
|
.reverse()
|
|
|
|
.find(choice => choice.placeholder === true);
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
/**
|
|
|
|
* Get groups from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get groups(): Group[] {
|
2018-04-24 13:54:45 +02:00
|
|
|
return this.state.groups;
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get active groups from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
get activeGroups(): Group[] {
|
2019-10-21 21:03:57 +02:00
|
|
|
const { groups, choices } = this;
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2018-05-28 14:55:44 +02:00
|
|
|
return groups.filter(group => {
|
2016-09-05 23:04:15 +02:00
|
|
|
const isActive = group.active === true && group.disabled === false;
|
2018-05-28 14:55:44 +02:00
|
|
|
const hasActiveOptions = choices.some(
|
|
|
|
choice => choice.active === true && choice.disabled === false,
|
2017-08-15 10:29:42 +02:00
|
|
|
);
|
2019-10-29 19:26:11 +01:00
|
|
|
|
2016-09-05 23:04:15 +02:00
|
|
|
return isActive && hasActiveOptions;
|
|
|
|
}, []);
|
2018-04-24 14:49:57 +02:00
|
|
|
}
|
2016-09-05 23:04:15 +02:00
|
|
|
|
2019-01-26 13:36:47 +01:00
|
|
|
/**
|
|
|
|
* Get loading state from store
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
isLoading(): boolean {
|
|
|
|
return this.state.loading;
|
2019-01-26 13:36:47 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 14:49:57 +02:00
|
|
|
/**
|
|
|
|
* Get single choice by it's ID
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
getChoiceById(id: string): Choice | undefined {
|
2019-11-03 14:18:16 +01:00
|
|
|
return this.activeChoices.find(choice => choice.id === parseInt(id, 10));
|
2016-09-05 23:04:15 +02:00
|
|
|
}
|
2016-10-18 20:23:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get group by group id
|
|
|
|
*/
|
2019-12-23 19:22:54 +01:00
|
|
|
getGroupById(id: number): Group | undefined {
|
2019-11-08 10:19:18 +01:00
|
|
|
return this.groups.find(group => group.id === id);
|
2016-10-18 20:23:07 +02:00
|
|
|
}
|
2016-08-14 23:14:37 +02:00
|
|
|
}
|