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