reducer_watson.js 1.64 KB
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,
            };
    }
};