Skip to content

Commit bce8962

Browse files
committed
EventShow now using Vuex, with a little caching
1 parent ea07140 commit bce8962

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/store.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default new Vuex.Store({
1717
'community'
1818
],
1919
events: [],
20-
eventsTotal: 0
20+
eventsTotal: 0,
21+
event: {}
2122
},
2223
mutations: {
2324
ADD_EVENT(state, event) {
@@ -28,6 +29,9 @@ export default new Vuex.Store({
2829
},
2930
SET_EVENTS_TOTAL(state, eventsTotal) {
3031
state.eventsTotal = eventsTotal
32+
},
33+
SET_EVENT(state, event) {
34+
state.event = event
3135
}
3236
},
3337
actions: {
@@ -48,6 +52,21 @@ export default new Vuex.Store({
4852
.catch(error => {
4953
console.log('There was an error:', error.response)
5054
})
55+
},
56+
fetchEvent({ commit, getters }, id) {
57+
var event = getters.getEventById(id)
58+
59+
if (event) {
60+
commit('SET_EVENT', event)
61+
} else {
62+
EventService.getEvent(id)
63+
.then(response => {
64+
commit('SET_EVENT', response.data)
65+
})
66+
.catch(error => {
67+
console.log('There was an error:', error.response)
68+
})
69+
}
5170
}
5271
},
5372
getters: {

src/views/EventShow.vue

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,14 @@
2525
</div>
2626
</template>
2727
<script>
28-
import EventService from '@/services/EventService.js'
28+
import { mapState } from 'vuex'
2929
3030
export default {
3131
props: ['id'],
32-
data() {
33-
return {
34-
event: {}
35-
}
36-
},
3732
created() {
38-
EventService.getEvent(this.id)
39-
.then(response => {
40-
this.event = response.data
41-
})
42-
.catch(error => {
43-
console.log('There was an error:', error.response)
44-
})
45-
}
33+
this.$store.dispatch('fetchEvent', this.id)
34+
},
35+
computed: mapState(['event'])
4636
}
4737
</script>
4838
<style scoped>

0 commit comments

Comments
 (0)