Choices/src/scripts/store/store.ts

145 lines
3.1 KiB
TypeScript
Raw Normal View History

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