sdy

create reducers

1 +import { combineReducers } from "redux";
2 +
3 +import watson from "./reducer_watson";
4 +
5 +export default combineReducers({ watson });
1 +import {
2 + INPUT_SUCCESS,
3 + INPUT_FAIL,
4 + SESSION_SUCCESS,
5 + SESSION_FAIL,
6 + MESSAGE_SUCCESS,
7 + MESSAGE_FAIL,
8 +} from "../actions/types";
9 +
10 +const initialState = {
11 + messages: [],
12 + userMessages: [],
13 + botMessages: [],
14 + botOptions: []
15 +};
16 +
17 +export default (state = initialState, action) => {
18 + const { type, payload, botMsg, botLabels } = action;
19 + let { messages, userMessages, botMessages, botOptions } = state;
20 +
21 + switch (type) {
22 + case INPUT_SUCCESS:
23 + messages = [...messages, { message: payload, type: "user" }];
24 + userMessages = [...userMessages, {messages: payload, type: "user"}];
25 + return {
26 + ...state,
27 + messages,
28 + userMessages,
29 + };
30 + case INPUT_FAIL:
31 + return {
32 + ...state,
33 + };
34 + case SESSION_SUCCESS:
35 + localStorage.setItem("session", payload["session_id"]);
36 + return {
37 + ...state,
38 + };
39 + case SESSION_FAIL:
40 + return {
41 + ...state,
42 + };
43 + case MESSAGE_SUCCESS:
44 + botOptions = [...botOptions, { options: botLabels, type: "bot"}];
45 + botMessages = [...botMessages, { botMessage: botMsg, type: "bot"}];
46 + messages = [...messages, { message: payload, type: "bot" }];
47 + return {
48 + ...state,
49 + messages,
50 + botMessages,
51 + botOptions,
52 + };
53 + case MESSAGE_FAIL:
54 + return {
55 + ...state,
56 + };
57 + default:
58 + return {
59 + ...state,
60 + };
61 + }
62 +};