Skip to content

Commit 1c0c963

Browse files
committed
update chat example for new API
1 parent d5bab78 commit 1c0c963

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

examples/chat/components/MessageSection.vue

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@
88
:message="message">
99
</message>
1010
</ul>
11-
<textarea class="message-composer" @keyup.enter="sendMessage"></textarea>
11+
<textarea class="message-composer" @keyup.enter="trySendMessage"></textarea>
1212
</div>
1313
</template>
1414

1515
<script>
16-
import store from '../store'
1716
import Message from './Message.vue'
17+
import { sendMessage } from '../store/actions'
1818
1919
export default {
2020
components: { Message },
21-
computed: {
22-
thread () {
23-
const id = store.state.currentThreadID
24-
return id
25-
? store.state.threads[id]
26-
: {}
21+
vuex: {
22+
state: {
23+
thread ({ currentThreadID, threads }) {
24+
return currentThreadID ? threads[currentThreadID] : {}
25+
},
26+
messages ({ messages }) {
27+
const messageIds = this.thread.messages
28+
return messageIds && messageIds.map(id => messages[id])
29+
}
2730
},
28-
messages () {
29-
return this.thread.messages &&
30-
this.thread.messages.map(id => store.state.messages[id])
31+
actions: {
32+
sendMessage
3133
}
3234
},
3335
watch: {
@@ -39,10 +41,10 @@ export default {
3941
}
4042
},
4143
methods: {
42-
sendMessage (e) {
44+
trySendMessage (e) {
4345
const text = e.target.value
4446
if (text.trim()) {
45-
store.actions.sendMessage(text, this.thread)
47+
this.sendMessage(text, this.thread)
4648
e.target.value = ''
4749
}
4850
}

examples/chat/components/Thread.vue

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<li
33
class="thread-list-item"
44
:class="{ active: isCurrentThread }"
5-
@click="onClick">
5+
@click="switchThread(thread.id)">
66
<h5 class="thread-name">{{ thread.name }}</h5>
77
<div class="thread-time">
88
{{ thread.lastMessage.timestamp | time }}
@@ -14,18 +14,18 @@
1414
</template>
1515

1616
<script>
17-
import store from '../store'
17+
import { switchThread } from '../store/actions'
1818
1919
export default {
2020
props: ['thread'],
21-
computed: {
22-
isCurrentThread () {
23-
return this.thread.id === store.state.currentThreadID
24-
}
25-
},
26-
methods: {
27-
onClick () {
28-
store.actions.switchThread(this.thread.id)
21+
vuex: {
22+
state: {
23+
isCurrentThread ({ currentThreadID }) {
24+
return this.thread.id === currentThreadID
25+
}
26+
},
27+
actions: {
28+
switchThread
2929
}
3030
}
3131
}

examples/chat/components/ThreadSection.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import Thread from './Thread.vue'
2121
2222
export default {
2323
components: { Thread },
24+
vuex: {
25+
state: {
26+
threads: state => state.threads
27+
}
28+
},
2429
computed: {
25-
threads () {
26-
return store.state.threads
27-
},
2830
unreadCount () {
29-
const threads = store.state.threads
31+
const threads = this.threads
3032
return Object.keys(threads).reduce((count, id) => {
3133
return threads[id].lastMessage.isRead
3234
? count

examples/chat/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import 'babel-polyfill'
22
import Vue from 'vue'
33
import App from './components/App.vue'
44
import store from './store'
5+
import { getAllMessages } from './store/actions'
6+
7+
Vue.config.debug = true
58

69
Vue.filter('time', timestamp => {
710
return new Date(timestamp).toLocaleTimeString()
811
})
912

1013
new Vue({
1114
el: 'body',
15+
store,
1216
components: { App }
1317
})
1418

15-
store.actions.getAllMessages()
19+
getAllMessages(store)

examples/chat/store/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
3-
import * as actions from './actions'
43
import mutations from './mutations'
54

65
Vue.use(Vuex)
@@ -32,7 +31,6 @@ export default new Vuex.Store({
3231
*/
3332
}
3433
},
35-
actions,
3634
mutations,
3735
middlewares: process.env.NODE_ENV !== 'production'
3836
? [Vuex.createLogger()]

0 commit comments

Comments
 (0)