UNPKG

1.52 kBJavaScriptView Raw
1import { combineReducers } from 'redux';
2import * as types from '../types';
3
4const topic = (
5 state = {},
6 action
7) => {
8 switch (action.type) {
9 case types.CREATE_TOPIC_REQUEST:
10 return {
11 id: action.id,
12 count: action.count,
13 text: action.text
14 };
15 case types.INCREMENT_COUNT:
16 if (state.id === action.id) {
17 return { ...state, count: state.count + 1 };
18 }
19 return state;
20 case types.DECREMENT_COUNT:
21 if (state.id === action.id) {
22 return { ...state, count: state.count - 1 };
23 }
24 return state;
25 default:
26 return state;
27 }
28};
29
30const topics = (
31 state = [],
32 action
33) => {
34 switch (action.type) {
35 case types.REQUEST_SUCCESS:
36 if (action.data) return action.data;
37 return state;
38 case types.CREATE_TOPIC_REQUEST:
39 return [...state, topic(undefined, action)];
40 case types.CREATE_TOPIC_FAILURE:
41 return state.filter(t => t.id !== action.id);
42 case types.DESTROY_TOPIC:
43 return state.filter(t => t.id !== action.id);
44 case types.INCREMENT_COUNT:
45 case types.DECREMENT_COUNT:
46 return state.map(t => topic(t, action));
47 default:
48 return state;
49 }
50};
51
52const newTopic = (
53 state = '',
54 action
55) => {
56 switch (action.type) {
57 case types.TYPING:
58 return action.newTopic;
59 case types.CREATE_TOPIC_REQUEST:
60 return '';
61 default:
62 return state;
63 }
64};
65
66const topicReducer = combineReducers({
67 topics,
68 newTopic
69});
70
71export default topicReducer;