Skip to content

Commit

Permalink
new router
Browse files Browse the repository at this point in the history
  • Loading branch information
YiDaoJ committed Nov 20, 2022
1 parent 2cefc49 commit e7f2c71
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import EventList from '@/views/EventList.vue'
import EventDetail from '@/views/EventDetail.vue'
import CreateEvent from '@/views/CreateEvent.vue'
import ErrorDisplay from '@/views/ErrorDisplay.vue'

const routes: Array<RouteRecordRaw> = [
{
Expand All @@ -20,6 +21,12 @@ const routes: Array<RouteRecordRaw> = [
props: true,
component: EventDetail,
},
{
path: '/error/:error',
name: 'ErrorDisplay',
props: true,
component: ErrorDisplay,
},
]

const router = createRouter({
Expand Down
18 changes: 13 additions & 5 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,35 @@ export default createStore({
createEvent({ commit }, event) {
EventService.postEvent(event)
.then(() => commit('ADD_EVENT', event))
.catch((err: AxiosError) => console.log({ err }))
.catch((err: AxiosError) => {
throw err
})
},

fetchEvents({ commit }) {
EventService.getEvents()
return EventService.getEvents()
.then((res: AxiosResponse) => {
commit('SET_EVENTS', res.data.events)
})
.catch((err: AxiosError) => console.log(err))
.catch((err: AxiosError) => {
throw err
})
},

fetchEvent({ commit, state }, id) {
// if the event to fetch is already in state.events, there is no need to fetch it once more
const existingEvent = state.events.find((evt) => evt.id === id)

if (existingEvent) {
commit('SET_EVENT', existingEvent)
} else {
EventService.getEvent(id)
return EventService.getEvent(id)
.then((res: AxiosResponse) => {
commit('SET_EVENT', res.data.event)
})
.catch((err: AxiosError) => console.log(err))
.catch((err: AxiosError) => {
throw err
})
}
},
},
Expand Down

0 comments on commit e7f2c71

Please sign in to comment.