|
1 | | -import { createAction, Action } from 'redux-actions'; |
| 1 | +import { createAction, handleActions, Action } from 'redux-actions'; |
2 | 2 | import * as Immutable from 'seamless-immutable'; |
3 | 3 |
|
4 | | -const RATES_MOCK = { 'PLN': 1, 'SEK': 2.1919 }; |
| 4 | +const RESPONSE_MOCK: IFixerServiceResponse = { |
| 5 | + base: 'PLN', |
| 6 | + date: Date.now().toString(), |
| 7 | + rates: { 'PLN': 1, 'SEK': 2.1919 }, |
| 8 | +}; |
5 | 9 |
|
6 | 10 | // Action Types - LOAD, CREATE, UPDATE, REMOVE |
7 | 11 | const LOAD_CURRENCY_RATES = 'currencyRates/LOAD_CURRENCY_RATES'; |
8 | 12 | const LOAD_CURRENCY_RATES_SUCCESS = 'currencyRates/LOAD_CURRENCY_RATES_SUCCESS'; |
9 | 13 | const LOAD_CURRENCY_RATES_ERROR = 'currencyRates/LOAD_CURRENCY_RATES_ERROR'; |
10 | 14 |
|
11 | 15 | // Action Creators |
12 | | -export const loadCurrencyRates = createAction(LOAD_CURRENCY_RATES); |
13 | | -export const loadCurrencyRatesSuccess = createAction(LOAD_CURRENCY_RATES_SUCCESS); |
14 | | -export const loadCurrencyRatesError = createAction(LOAD_CURRENCY_RATES_ERROR); |
| 16 | +export const loadCurrencyRates = createAction<void>(LOAD_CURRENCY_RATES); |
| 17 | +export const loadCurrencyRatesSuccess = createAction<IFixerServiceResponse>(LOAD_CURRENCY_RATES_SUCCESS); |
| 18 | +export const loadCurrencyRatesError = createAction<string>(LOAD_CURRENCY_RATES_ERROR); |
| 19 | + |
15 | 20 |
|
16 | 21 | // Reducer |
17 | | -export interface ICurrencyRates { |
| 22 | +export interface ICurrencyRatesReducer { |
18 | 23 | isLoading: boolean; |
19 | 24 | errorMessage: string | null; |
20 | 25 | lastUpdated: Date | null; |
21 | 26 | base: string; |
22 | 27 | rates: any; |
23 | | - currencies: string[]; |
24 | 28 | } |
25 | | -const initialState: ICurrencyRates = { |
| 29 | + |
| 30 | +const initialState = Immutable.from<ICurrencyRatesReducer>({ |
26 | 31 | isLoading: false, |
27 | 32 | errorMessage: null, |
28 | 33 | lastUpdated: null, |
29 | 34 | base: 'PLN', |
30 | | - rates: RATES_MOCK, |
31 | | - currencies: Object.keys(RATES_MOCK) |
32 | | -}; |
33 | | - |
34 | | -export default function reducer(state = Immutable.from(initialState), action: Action<any>) { |
35 | | - switch (action.type) { |
36 | | - case LOAD_CURRENCY_RATES: |
37 | | - return state.merge({ |
38 | | - isLoading: true |
39 | | - }); |
40 | | - case LOAD_CURRENCY_RATES_SUCCESS: |
41 | | - return state.merge({ |
42 | | - isLoading: false, |
43 | | - errorMessage: null, |
44 | | - results: action.payload, |
45 | | - lastUpdated: Date.now() |
46 | | - }); |
47 | | - case LOAD_CURRENCY_RATES_ERROR: |
48 | | - return state.merge({ |
49 | | - isLoading: false, |
50 | | - errorMessage: action.payload |
51 | | - }); |
| 35 | + rates: RESPONSE_MOCK.rates, |
| 36 | +}); |
52 | 37 |
|
53 | | - default: return state; |
54 | | - } |
55 | | -} |
| 38 | +export default handleActions<any, any>({ |
| 39 | + [LOAD_CURRENCY_RATES]: (state: typeof initialState, action: Action<void>) => state.merge({ |
| 40 | + isLoading: true, |
| 41 | + }), |
| 42 | + [LOAD_CURRENCY_RATES_SUCCESS]: (state: typeof initialState, action: Action<IFixerServiceResponse>) => state.merge({ |
| 43 | + isLoading: false, |
| 44 | + errorMessage: null, |
| 45 | + rates: action.payload && action.payload.rates, |
| 46 | + lastUpdated: Date.now(), |
| 47 | + }), |
| 48 | + [LOAD_CURRENCY_RATES_ERROR]: (state: typeof initialState, action: Action<string>) => state.merge({ |
| 49 | + isLoading: false, |
| 50 | + errorMessage: action.payload, |
| 51 | + }), |
| 52 | +}, initialState); |
0 commit comments