import { createStore } from 'potent-reducer' import { safeSelectNextWord, safeSelectNextSentence, safeSelectPrevWord, safeSelectPrevSentence, safeSelectNextSegment, safeSelectPrevSegment } from './selectors' const initialState = { originalText: 'Sample Text', curIdx: 0, maxLength: 10, isPlaying: false, wpm: 300, offset: -15, running: false, lang: 'en', textDisplay: { mode: 'pagination', options: { pagination: { pageLength: 50 }, segments: { prev: 9, next: 40 }, sentences: { prev: 1, next: 8 } } } } const thunks = { setText: text => ({ text }), resetSegment: () => {}, incSegment: () => {}, decSegment: () => {}, incWord: () => {}, decWord: () => {}, incSentence: () => {}, decSentence: () => {}, setMaxLength: maxLength => ({ maxLength }), setWpm: wpm => ({ wpm }), setOffset: offset => ({ offset }), start: () => {}, stop: () => {}, pause: () => {}, setLang: lang => ({ lang }) } const reducer = { SET_TEXT: (state, { text }) => ({ ...state, originalText: text, curIdx: 0 }), RESET_SEGMENT: state => ({ ...state, curIdx: 0 }), INC_SEGMENT: state => ({ ...state, curIdx: safeSelectNextSegment(state) }), DEC_SEGMENT: state => ({ ...state, curIdx: safeSelectPrevSegment(state) }), INC_WORD: state => ({ ...state, curIdx: safeSelectNextWord(state) }), DEC_WORD: state => ({ ...state, curIdx: safeSelectPrevWord(state) }), INC_SENTENCE: state => ({ ...state, curIdx: safeSelectNextSentence(state) }), DEC_SENTENCE: state => ({ ...state, curIdx: safeSelectPrevSentence(state) }), SET_MAX_LENGTH: (state, { maxLength }) => ({ ...state, maxLength, running: false, curIdx: 0 }), SET_WPM: (state, { wpm }) => ({ ...state, wpm }), SET_OFFSET: (state, { offset }) => ({ ...state, offset }), START: state => ({ ...state, running: true }), STOP: state => ({ ...state, running: false, curIdx: 0 }), PAUSE: state => ({ ...state, running: false }), SET_LANG: (state, { lang }) => ({ ...state, lang }) } const store = createStore({ reducer, thunks, initialState, logging: false, warnOnUndefinedSelect: true }) export const Provider = store.Provider export const useStore = store.useStore export const useSelector = store.useSelector export const useDispatch = store.useDispatch