-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
48 lines (40 loc) · 928 Bytes
/
common.ts
File metadata and controls
48 lines (40 loc) · 928 Bytes
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
import { Reducer } from '../../src/reducer/types'
export type ChatMessage = {
from: string
text: string
}
export type ChatAppState = {
chatMessages: ChatMessage[]
}
export type AddChatMessageAction = {
type: 'addChatMessage'
payload: {
chatMessage: ChatMessage
}
}
export type ChatAppActions = AddChatMessageAction
export const INITIAL_STATE: ChatAppState = {
chatMessages: [],
}
export const chatAppReducer: Reducer<ChatAppState, ChatAppActions> = (state, action) => {
if (state == null)
return INITIAL_STATE
switch (action.type) {
case 'addChatMessage':
return {
...state,
chatMessages: state.chatMessages.concat(action.payload.chatMessage),
}
default:
return state
}
}
export const addChatMessage = (from: string, text: string): ChatAppActions => ({
type: 'addChatMessage',
payload: {
chatMessage: {
from,
text,
},
},
})