Skip to content

Commit 01d7d35

Browse files
committed
proper checks in router
1 parent e8c0cf9 commit 01d7d35

File tree

9 files changed

+72
-23
lines changed

9 files changed

+72
-23
lines changed

src/api/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,24 @@ import locations from './data/locations.json';
33
export async function getLocations() {
44
return Promise.resolve(locations);
55
}
6+
7+
export const loadLocalStore = () => {
8+
try {
9+
const serializedState = localStorage.getItem('state');
10+
if (serializedState === null) {
11+
return undefined;
12+
}
13+
return JSON.parse(serializedState);
14+
} catch(e) {
15+
return undefined;
16+
}
17+
}
18+
19+
export const saveToLocalStore = (state: any) => {
20+
try {
21+
const serializedState = JSON.stringify(state);
22+
localStorage.setItem('state', serializedState);
23+
} catch(e) {
24+
//ignore errors
25+
}
26+
}
File renamed without changes.

src/router.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import Vue from 'vue';
22
import Router, { NavigationGuard } from 'vue-router';
3-
import Tutorial from './views/Tutorial.vue';
3+
import store from './store';
4+
import Tutorial from './components/Tutorial.vue';
45

56
Vue.use(Router);
67

78
const privateRoute: NavigationGuard = function (to, from, next) {
8-
if (!User.isAuthenticated) {
9+
if (!store.state.user.isAuthenticated) {
910
next({ name: 'login' });
1011
} else {
1112
next();
1213
}
1314
}
1415

1516
const requiresTutorialRoute: NavigationGuard = function (to, from, next) {
16-
if (!hasSeenTutorial) {
17+
if (!store.state.user.hasSeenTutorial) {
1718
next({ name: 'tutorial' });
1819
} else {
1920
next();
@@ -27,7 +28,7 @@ export default new Router({
2728
{
2829
path: '/',
2930
name: 'app',
30-
component: () => import(/* webpackChunkName: "about" */ './views/App.vue'),
31+
component: () => import(/* webpackChunkName: "about" */ './components/App.vue'),
3132
beforeEnter: requiresTutorialRoute
3233
},
3334
{
@@ -38,25 +39,25 @@ export default new Router({
3839
{
3940
path: '/account',
4041
name: 'account',
41-
component: () => import(/* webpackChunkName: "about" */ './views/Account.vue'),
42+
component: () => import(/* webpackChunkName: "about" */ './components/Account.vue'),
4243
beforeEnter: privateRoute
4344
},
4445
{
4546
path: '/support',
4647
name: 'support',
47-
component: () => import(/* webpackChunkName: "about" */ './views/Support.vue'),
48+
component: () => import(/* webpackChunkName: "about" */ './components/Support.vue'),
4849
beforeEnter: requiresTutorialRoute
4950
},
5051
{
5152
path: '/login',
5253
name: 'login',
53-
component: () => import(/* webpackChunkName: "about" */ './views/Login.vue'),
54+
component: () => import(/* webpackChunkName: "about" */ './components/Login.vue'),
5455
beforeEnter: requiresTutorialRoute
5556
},
5657
{
5758
path: '/signup',
5859
name: 'signup',
59-
component: () => import(/* webpackChunkName: "about" */ './views/Signup.vue'),
60+
component: () => import(/* webpackChunkName: "about" */ './components/Signup.vue'),
6061
beforeEnter: requiresTutorialRoute
6162
}
6263
]

src/store/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import Vue from 'vue';
22
import Vuex from 'vuex';
3-
import locations from './modules/locations';
4-
import sessions from './modules/sessions';
5-
import speakers from './modules/speakers';
6-
import user from './modules/user';
3+
import locations, { LocationState } from './modules/locations';
4+
import sessions, { SessionState } from './modules/sessions';
5+
import speakers, { Speaker} from './modules/speakers';
6+
import user, { User } from './modules/user';
77

8-
Vue.use(Vuex)
8+
export interface StoreState {
9+
locations: LocationState,
10+
sessions: SessionState,
11+
speakers: Speaker[],
12+
user: User
13+
}
914

10-
export default new Vuex.Store({
15+
Vue.use(Vuex);
16+
17+
export default new Vuex.Store<StoreState>({
1118
modules: {
1219
locations,
1320
sessions,
1421
speakers,
1522
user
1623
},
1724
strict: process.env.NODE_ENV !== 'production'
18-
})
25+
});

src/store/modules/locations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StoreOptions } from 'vuex';
1+
import { Module } from 'vuex';
22

33
export interface Location {
44
name?: string;
@@ -11,7 +11,7 @@ export interface LocationState {
1111
locations: Location[]
1212
}
1313

14-
const locationStore: StoreOptions<LocationState> = {
14+
const locationStore: Module<LocationState, {}> = {
1515
state: {
1616
mapCenter: {
1717
lat: 43.071584,

src/store/modules/sessions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StoreOptions } from 'vuex';
1+
import { Module } from 'vuex';
22

33
export interface Session {
44
id: number,
@@ -27,7 +27,7 @@ const defaultState: SessionState = {
2727
filterFavorites: false
2828
}
2929

30-
const sessionStore: StoreOptions<SessionState> = {
30+
const sessionStore: Module<SessionState, {}> = {
3131
state: defaultState,
3232
mutations: {
3333
setSearchText(state, searchText: string) {

src/store/modules/speakers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StoreOptions } from 'vuex';
1+
import { Module } from 'vuex';
22

33
export interface Speaker {
44
id: number,
@@ -11,7 +11,7 @@ export interface Speaker {
1111
phone: string
1212
}
1313

14-
const speakerStore: StoreOptions<Speaker[]> = {
14+
const speakerStore: Module<Speaker[], {}> = {
1515
state: []
1616
};
1717

src/store/modules/user.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StoreOptions } from 'vuex';
1+
import { Module } from 'vuex';
22

33
export interface User {
44
pictureLocation: string | null;
@@ -14,7 +14,7 @@ const defaultState: User = {
1414
hasSeenTutorial: false
1515
};
1616

17-
const userStore: StoreOptions<User> = {
17+
const userStore: Module<User, {}> = {
1818
state: defaultState,
1919
mutations: {
2020
sawTutorial(state) {

src/utils/loadState.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const loadState = () => {
2+
try {
3+
const serializedState = localStorage.getItem('state');
4+
if (serializedState === null) {
5+
return undefined;
6+
}
7+
return JSON.parse(serializedState);
8+
} catch(e) {
9+
return undefined;
10+
}
11+
}
12+
13+
export const saveState = (state: any) => {
14+
try {
15+
const serializedState = JSON.stringify(state);
16+
localStorage.setItem('state', serializedState);
17+
} catch(e) {
18+
//ignore errors
19+
}
20+
}

0 commit comments

Comments
 (0)