Skip to content

Commit a77f431

Browse files
committed
port chat example
1 parent ea429c9 commit a77f431

File tree

9 files changed

+90
-71
lines changed

9 files changed

+90
-71
lines changed

examples/chat/components/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ThreadSection from './ThreadSection.vue'
1212
import MessageSection from './MessageSection.vue'
1313
1414
export default {
15+
name: 'App',
1516
components: {
1617
ThreadSection,
1718
MessageSection

examples/chat/components/Message.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
<script>
1212
export default {
13-
props: ['message']
13+
name: 'Message',
14+
props: {
15+
message: Object
16+
}
1417
}
1518
</script>

examples/chat/components/MessageSection.vue

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,26 @@
44
<ul class="message-list" ref="list">
55
<message
66
v-for="message in sortedMessages"
7-
track-by="id"
7+
:key="message.id"
88
:message="message">
99
</message>
1010
</ul>
11-
<textarea class="message-composer" @keyup.enter="trySendMessage"></textarea>
11+
<textarea class="message-composer" @keyup.enter="sendMessage"></textarea>
1212
</div>
1313
</template>
1414

1515
<script>
1616
import Message from './Message.vue'
17-
import { sendMessage } from '../vuex/actions'
18-
import { currentThread, currentMessages } from '../vuex/getters'
17+
import { mapGetters } from 'vuex'
1918
2019
export default {
20+
name: 'MessageSection',
2121
components: { Message },
22-
vuex: {
23-
getters: {
24-
thread: currentThread,
25-
messages: currentMessages
26-
},
27-
actions: {
28-
sendMessage
29-
}
30-
},
3122
computed: {
23+
...mapGetters({
24+
thread: 'currentThread',
25+
messages: 'currentMessages'
26+
}),
3227
sortedMessages () {
3328
return this.messages
3429
.slice()
@@ -44,10 +39,13 @@ export default {
4439
}
4540
},
4641
methods: {
47-
trySendMessage (e) {
42+
sendMessage (e) {
4843
const text = e.target.value
4944
if (text.trim()) {
50-
this.sendMessage(text, this.thread)
45+
this.$store.call('sendMessage', {
46+
text,
47+
thread: this.thread
48+
})
5149
e.target.value = ''
5250
}
5351
}

examples/chat/components/Thread.vue

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<li
33
class="thread-list-item"
4-
:class="{ active: thread.id === currentThreadID }"
5-
@click="switchThread(thread.id)">
4+
:class="{ active: active }"
5+
@click="$emit('switch-thread', thread.id)">
66
<h5 class="thread-name">{{ thread.name }}</h5>
77
<div class="thread-time">
88
{{ thread.lastMessage.timestamp | time }}
@@ -14,17 +14,11 @@
1414
</template>
1515

1616
<script>
17-
import { switchThread } from '../vuex/actions'
18-
1917
export default {
20-
props: ['thread'],
21-
vuex: {
22-
getters: {
23-
currentThreadID: state => state.currentThreadID
24-
},
25-
actions: {
26-
switchThread
27-
}
18+
name: 'Thread',
19+
props: {
20+
thread: Object,
21+
active: Boolean
2822
}
2923
}
3024
</script>

examples/chat/components/ThreadSection.vue

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@
88
<ul class="thread-list">
99
<thread
1010
v-for="thread in threads"
11-
track-by="id"
12-
:thread="thread">
11+
:key="thread.id"
12+
:thread="thread"
13+
:active="thread.id === currentThread.id"
14+
@switch-thread="switchThread">
1315
</thread>
1416
</ul>
1517
</div>
1618
</template>
1719

1820
<script>
1921
import Thread from './Thread.vue'
22+
import { mapGetters } from 'vuex'
2023
2124
export default {
25+
name: 'ThreadSection',
2226
components: { Thread },
23-
vuex: {
24-
getters: {
25-
threads: state => state.threads
26-
}
27-
},
2827
computed: {
28+
...mapGetters([
29+
'threads',
30+
'currentThread'
31+
]),
2932
unreadCount () {
3033
const threads = this.threads
3134
return Object.keys(threads).reduce((count, id) => {
@@ -34,6 +37,11 @@ export default {
3437
: count + 1
3538
}, 0)
3639
}
40+
},
41+
methods: {
42+
switchThread (id) {
43+
this.$store.call('switchThread', { id })
44+
}
3745
}
3846
}
3947
</script>

examples/chat/vuex/actions.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ import * as types from './mutation-types'
33

44
export const getAllMessages = ({ dispatch }) => {
55
api.getAllMessages(messages => {
6-
dispatch(types.RECEIVE_ALL, messages)
6+
dispatch(types.RECEIVE_ALL, {
7+
messages
8+
})
79
})
810
}
911

10-
export const sendMessage = ({ dispatch }, text, thread) => {
11-
api.createMessage({ text, thread }, message => {
12-
dispatch(types.RECEIVE_MESSAGE, message)
12+
export const sendMessage = ({ dispatch }, payload) => {
13+
api.createMessage(payload, message => {
14+
dispatch(types.RECEIVE_MESSAGE, {
15+
message
16+
})
1317
})
1418
}
1519

16-
export const switchThread = ({ dispatch }, id) => {
17-
dispatch(types.SWITCH_THREAD, id)
20+
export const switchThread = ({ dispatch }, payload) => {
21+
dispatch(types.SWITCH_THREAD, payload)
1822
}

examples/chat/vuex/getters.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
export function currentThread (state) {
1+
export const threads = state => state.threads
2+
3+
export const currentThread = state => {
24
return state.currentThreadID
35
? state.threads[state.currentThreadID]
46
: {}
57
}
68

7-
export function currentMessages (state) {
9+
export const currentMessages = state => {
810
const thread = currentThread(state)
911
return thread.messages
1012
? thread.messages.map(id => state.messages[id])

examples/chat/vuex/mutations.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { set } from 'vue'
22
import * as types from './mutation-types'
33

44
export default {
5-
[types.RECEIVE_ALL] (state, messages) {
5+
[types.RECEIVE_ALL] (state, { messages }) {
66
let latestMessage
77
messages.forEach(message => {
88
// create new thread if the thread doesn't exist
@@ -20,11 +20,11 @@ export default {
2020
setCurrentThread(state, latestMessage.threadID)
2121
},
2222

23-
[types.RECEIVE_MESSAGE] (state, message) {
23+
[types.RECEIVE_MESSAGE] (state, { message }) {
2424
addMessage(state, message)
2525
},
2626

27-
[types.SWITCH_THREAD] (state, id) {
27+
[types.SWITCH_THREAD] (state, { id }) {
2828
setCurrentThread(state, id)
2929
}
3030
}
@@ -53,6 +53,9 @@ function addMessage (state, message) {
5353

5454
function setCurrentThread (state, id) {
5555
state.currentThreadID = id
56+
if (!state.threads[id]) {
57+
debugger
58+
}
5659
// mark thread as read
5760
state.threads[id].lastMessage.isRead = true
5861
}

examples/chat/vuex/store.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
3+
import * as getters from './getters'
4+
import * as actions from './actions'
35
import mutations from './mutations'
46
import createLogger from '../../../src/plugins/logger'
57

68
Vue.use(Vuex)
79

8-
export default new Vuex.Store({
9-
state: {
10-
currentThreadID: null,
11-
threads: {
12-
/*
13-
id: {
14-
id,
15-
name,
16-
messages: [...ids],
17-
lastMessage
18-
}
19-
*/
20-
},
21-
messages: {
22-
/*
23-
id: {
24-
id,
25-
threadId,
26-
threadName,
27-
authorName,
28-
text,
29-
timestamp,
30-
isRead
31-
}
32-
*/
10+
const state = {
11+
currentThreadID: null,
12+
threads: {
13+
/*
14+
id: {
15+
id,
16+
name,
17+
messages: [...ids],
18+
lastMessage
3319
}
20+
*/
3421
},
22+
messages: {
23+
/*
24+
id: {
25+
id,
26+
threadId,
27+
threadName,
28+
authorName,
29+
text,
30+
timestamp,
31+
isRead
32+
}
33+
*/
34+
}
35+
}
36+
37+
export default new Vuex.Store({
38+
state,
39+
getters,
40+
actions,
3541
mutations,
3642
plugins: process.env.NODE_ENV !== 'production'
3743
? [createLogger()]

0 commit comments

Comments
 (0)