-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.js
34 lines (27 loc) · 828 Bytes
/
reducer.js
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
module.exports = function (bus, state) {
bus.on('add-chat-room', function addChatRoom (room) {
state.rooms.push(room)
bus.emit('update')
})
bus.on('add-message-into-chat', function addMessageIntoChat (chat_id, message) {
var room = state.rooms.filter(function (room) {
return room.id === chat_id
})[0]
if (room) room.messages.push(message)
bus.emit('update')
})
bus.on('fetch-chat-rooms', function fetchChatRooms () {
fetch('http://localhost:5000/chats?user=' + state.username)
.then(function (res) {
return res.json()
})
.then(function updateRooms (rooms) {
state.rooms = rooms || []
bus.emit('update')
})
})
bus.on('change-user', function changeUser (username) {
state.username = username
bus.emit('update')
})
}