import { reducerWithInitialState } from 'typescript-fsa-reducers';

import { dicitActions } from '../actions';
import { Quote } from '../states/QuoteState';

export interface DicitState {
  quote: Quote;
}

const initialState: DicitState = { quote: { sentence: '', author: '' } };

export const quotesReducer = reducerWithInitialState(initialState)
  .case(dicitActions.init, (state) => {
    return Object.assign({}, state, { initialState });
  })
  .case(dicitActions.fetchQuote, (state) => {
    const currQuote = { sentence: 'The sinews of war are not gold, but good soldiers.', author: 'Niccoló Machiavelli' };
    return Object.assign({}, state, { currQuote });
  });

import { reducerWithInitialState } from 'typescript-fsa-reducers';

import { dicitActions } from '../actions';
import { Quote } from '../states/QuoteState';

export interface DicitState {
  quote: Quote;
}

const fetchNewQuote = (): Quote => {
  let remainedQuotes = [];
  if ('remainedQuotes' in localStorage) {
    remainedQuotes = JSON.parse(localStorage.getItem('remainedQuotes'));
  }

  if (remainedQuotes.length === 0) {
    const wholeQuotes = require('./../../data/quotes.json');
    remainedQuotes = wholeQuotes.quotes;
  }

  const index = Math.floor(Math.random() * remainedQuotes.length);
  remainedQuotes.splice(index, 1);
  localStorage.setItem('remainedQuotes', JSON.stringify(remainedQuotes));

  return remainedQuotes[index];
};

const initialState: DicitState = { quote: fetchNewQuote() };

export const quotesReducer = reducerWithInitialState(initialState)
  .case(dicitActions.init, (state) => {
    return Object.assign({}, state, { initialState });
  })
  .case(dicitActions.fetchQuote, (state) => {
    return Object.assign({}, state, { initialState });
  });

Add an action to fetch dynamically
Add an action to fetch dynamically
