Skip to content
22 changes: 22 additions & 0 deletions src/_actions/ChatAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NEW_CHAT_ROOM, LEAVE_CHAT_ROOM, RECEIVE_MESSAGE, SEND_MESSAGE } from "./types";
import { TargetChatRoom, TargetMessage } from "./ChatActionTypes";

export const createChatRoom = (targetChatRoom: TargetChatRoom) => ({
type: NEW_CHAT_ROOM,
payload: targetChatRoom,
});

export const leaveChatRoom = (targetChatRoom: TargetChatRoom) => ({
type: LEAVE_CHAT_ROOM,
payload: targetChatRoom,
});

export const sendMessage = (targetMessage: TargetMessage) => ({
type: SEND_MESSAGE,
payload: targetMessage,
});

export const receiveMessage = (targetMessage: TargetMessage) => ({
type: RECEIVE_MESSAGE,
payload: targetMessage,
});
9 changes: 9 additions & 0 deletions src/_actions/ChatActionTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface TargetChatRoom {
id: number
title: String,
messages: Array<String>
}

export interface TargetMessage {
content: String
}
4 changes: 4 additions & 0 deletions src/_actions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const NEW_CHAT_ROOM = "new_chat_room" as const;
export const LEAVE_CHAT_ROOM = "leave_chat_room" as const;
export const SEND_MESSAGE = "send_message" as const;
export const RECEIVE_MESSAGE = "receive_message" as const;
41 changes: 41 additions & 0 deletions src/_reducers/ChatReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
NEW_CHAT_ROOM,
LEAVE_CHAT_ROOM,
SEND_MESSAGE,
RECEIVE_MESSAGE
} from '_actions/types';

import {
createChatRoom,
leaveChatRoom,
sendMessage,
receiveMessage
} from '_actions/ChatAction'

type ChatAction =
| ReturnType<typeof createChatRoom>
| ReturnType<typeof leaveChatRoom>
| ReturnType<typeof sendMessage>
| ReturnType<typeof receiveMessage>;

type ChatState = {
chat: Array<Array<String>>;
}

const initialState: ChatState = {
chat : []
}

export default function chatReducer(
state: ChatState = initialState,
action: ChatAction
) : ChatState {
switch(action.type) {
case NEW_CHAT_ROOM:
state.chat[action.payload.id] = [...action.payload.messages];
return {...state};
default:
return state;
}

}