github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/web/src/router/fetchers.ts (about) 1 import { Route, NavigationGuardNext } from 'vue-router'; 2 import { Store } from 'vuex'; 3 import { RootState, Actions, State, Mutations } from '@/store'; 4 import { FetchReportOption } from '@/store/modules/report/actions'; 5 6 type RouteHandler = (to: Route, from: Route, next: NavigationGuardNext) => void; 7 8 function fetchReportHistory(store: Store<RootState>) { 9 const report = (store.state as State).report.current; 10 const repo = (store.state as State).repository.current; 11 if (report && repo) { 12 store.dispatch(Actions.FETCH_REPORT_HISTORY, { 13 ReportID: report.reportID, 14 Ref: repo.Branch 15 } as FetchReportOption); 16 } else { 17 store.commit(Mutations.SET_REPORT_HISTORY, []); 18 } 19 } 20 21 export function fetchCurrentRepository(store: Store<RootState>): RouteHandler { 22 return (to, from, next) => { 23 store.dispatch(Actions.CHANGE_CURRENT_REPOSITORY, to.params) 24 .then(() => { 25 if ((store.state as State).repository.current) { 26 store.dispatch( 27 Actions.FETCH_REPORT_CURRENT, 28 { 29 ReportID: (store.state as State).repository.current?.ReportID, 30 Ref: to.query.ref 31 } as FetchReportOption 32 ).then(() => { 33 fetchReportHistory(store); 34 store.dispatch(Actions.FETCH_REPOSITORY_COMMITS); 35 store.dispatch(Actions.FETCH_REPOSITORY_BRANCHES); 36 store.dispatch(Actions.FETCH_REPOSITORY_OWNER); 37 }).finally(() => { 38 next(); 39 }); 40 store.dispatch(Actions.FETCH_REPOSITORY_SETTING); 41 } else { 42 next(); 43 } 44 }).catch(reason => { 45 console.warn(reason); 46 next(); 47 }); 48 }; 49 } 50 51 export function fetchNewRepository(store: Store<RootState>): RouteHandler { 52 return (to, from, next) => { 53 if (from.name !== null && to.query.ref !== from.query.ref && to.meta.checkRenew) { 54 fetchCurrentRepository(store)(to, from, next); 55 } else { 56 next(); 57 } 58 }; 59 } 60 61 export function fetchReportSource(store: Store<RootState>): RouteHandler { 62 return (to, from, next) => { 63 store.dispatch(Actions.FETCH_REPORT_SOURCE, to); 64 next(); 65 }; 66 } 67 68 export function fetchReportSetting(store: Store<RootState>): RouteHandler { 69 return (to, from, next) => { 70 store.dispatch(Actions.FETCH_REPOSITORY_SETTING); 71 next(); 72 }; 73 } 74 75 export function fetchUserSCM(store: Store<RootState>): RouteHandler { 76 return (to, from, next) => { 77 store.dispatch(Actions.FETCH_USER_SCM); 78 next(); 79 }; 80 } 81 82 export function fetchUserSettings(store: Store<RootState>): RouteHandler { 83 return (to, from, next) => { 84 store.dispatch(Actions.FETCH_USER_TOKENS); 85 next(); 86 }; 87 }