Skip to content

Commit 560c6a2

Browse files
committed
improved reducer types using redux-actions
1 parent 45b231d commit 560c6a2

File tree

4 files changed

+42
-44
lines changed

4 files changed

+42
-44
lines changed

src/containers/currency-converter-container/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { Action } from 'redux-actions';
44
import { connect } from 'react-redux';
55
// components imports
66
import { IRootReducer } from '../../store';
7-
import { ICurrencyRates } from '../../store/currency-rates-reducer';
8-
import { ICurrencyConverter } from '../../store/currency-converter-reducer';
7+
import { ICurrencyRatesReducer } from '../../store/currency-rates-reducer';
8+
import { ICurrencyConverterReducer } from '../../store/currency-converter-reducer';
99
import { PageHeader } from '../../components/page-header';
1010
import { PageSection } from '../../components/page-section';
1111
import * as currencyConverterActions from '../../store/currency-converter-reducer';
1212
import { CurrencyConverter } from './components/currency-converter';
1313

1414
interface IProps {
15-
currencyRates: ICurrencyRates;
16-
currencyConverter: ICurrencyConverter;
15+
currencyRates: ICurrencyRatesReducer;
16+
currencyConverter: ICurrencyConverterReducer;
1717
updateBaseCurrency: (payload: string) => Action<string>;
1818
updateBaseValue: (payload: string) => Action<string>;
1919
updateTargetCurrency: (payload: string) => Action<string>;
@@ -25,7 +25,8 @@ interface IState {
2525
export class CurrencyConverterContainer extends React.Component<IProps, IState> {
2626
render() {
2727
const { baseCurrency, targetCurrency, baseValue, targetValue } = this.props.currencyConverter;
28-
const { currencies } = this.props.currencyRates;
28+
const { rates } = this.props.currencyRates;
29+
const currencies = Object.keys(rates);
2930
const { updateBaseCurrency, updateBaseValue, updateTargetCurrency, updateTargetValue } = this.props;
3031

3132
return (

src/store/currency-converter-reducer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ export const updateBaseValue = createAction<string>(UPDATE_BASE_VALUE);
1414
export const updateTargetValue = createAction<string>(UPDATE_TARGET_VALUE);
1515

1616
// Reducer
17-
export interface ICurrencyConverter {
17+
export interface ICurrencyConverterReducer {
1818
baseCurrency: string;
1919
targetCurrency: string;
2020
baseValue: string;
2121
targetValue: string;
2222
}
23-
const initialState: ICurrencyConverter = {
23+
const initialState: ICurrencyConverterReducer = {
2424
baseCurrency: 'PLN',
2525
targetCurrency: 'SEK',
2626
baseValue: '0',
Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
1-
import { createAction, Action } from 'redux-actions';
1+
import { createAction, handleActions, Action } from 'redux-actions';
22
import * as Immutable from 'seamless-immutable';
33

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+
};
59

610
// Action Types - LOAD, CREATE, UPDATE, REMOVE
711
const LOAD_CURRENCY_RATES = 'currencyRates/LOAD_CURRENCY_RATES';
812
const LOAD_CURRENCY_RATES_SUCCESS = 'currencyRates/LOAD_CURRENCY_RATES_SUCCESS';
913
const LOAD_CURRENCY_RATES_ERROR = 'currencyRates/LOAD_CURRENCY_RATES_ERROR';
1014

1115
// 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+
1520

1621
// Reducer
17-
export interface ICurrencyRates {
22+
export interface ICurrencyRatesReducer {
1823
isLoading: boolean;
1924
errorMessage: string | null;
2025
lastUpdated: Date | null;
2126
base: string;
2227
rates: any;
23-
currencies: string[];
2428
}
25-
const initialState: ICurrencyRates = {
29+
30+
const initialState = Immutable.from<ICurrencyRatesReducer>({
2631
isLoading: false,
2732
errorMessage: null,
2833
lastUpdated: null,
2934
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+
});
5237

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);

src/store/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { routerReducer } from 'react-router-redux';
44
import { createStore } from 'redux';
55

66
import {
7-
default as currencyRatesReducer, ICurrencyRates
7+
default as currencyRatesReducer, ICurrencyRatesReducer
88
} from './currency-rates-reducer';
99
import {
10-
default as currencyConverterReducer, ICurrencyConverter
10+
default as currencyConverterReducer, ICurrencyConverterReducer
1111
} from './currency-converter-reducer';
1212

1313
export interface IRootReducer {
1414
routing: any;
15-
currencyRates: ICurrencyRates;
16-
currencyConverter: ICurrencyConverter;
15+
currencyRates: ICurrencyRatesReducer;
16+
currencyConverter: ICurrencyConverterReducer;
1717
}
1818

1919
export const rootReducer = combineReducers({

0 commit comments

Comments
 (0)