diff --git a/src/_actions/ChatAction.ts b/src/_actions/ChatAction.ts new file mode 100644 index 0000000..a18cb27 --- /dev/null +++ b/src/_actions/ChatAction.ts @@ -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, +}); \ No newline at end of file diff --git a/src/_actions/ChatActionTypes.ts b/src/_actions/ChatActionTypes.ts new file mode 100644 index 0000000..c93cc86 --- /dev/null +++ b/src/_actions/ChatActionTypes.ts @@ -0,0 +1,9 @@ +export interface TargetChatRoom { + id: number + title: String, + messages: Array +} + +export interface TargetMessage { + content: String +} \ No newline at end of file diff --git a/src/_actions/types.ts b/src/_actions/types.ts new file mode 100644 index 0000000..a8e29bd --- /dev/null +++ b/src/_actions/types.ts @@ -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; diff --git a/src/_reducers/ChatReducer.ts b/src/_reducers/ChatReducer.ts new file mode 100644 index 0000000..c6f6094 --- /dev/null +++ b/src/_reducers/ChatReducer.ts @@ -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 + | ReturnType + | ReturnType + | ReturnType; + +type ChatState = { + chat: Array>; +} + +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; + } + +} \ No newline at end of file