store init

This commit is contained in:
Willyb321
2018-12-04 07:53:26 +11:00
parent 4a38f2e02e
commit c1c6ea5001
4 changed files with 93 additions and 10 deletions

View File

@@ -1,10 +0,0 @@
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
export default new Vuex.Store({
state: {},
mutations: {},
actions: {}
});

15
src/store/index.js Normal file
View File

@@ -0,0 +1,15 @@
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
import { CommonStore as Common } from "./modules";
export default new Vuex.Store({
state: {},
modules: {
Common
},
mutations: {},
actions: {}
});

View File

@@ -0,0 +1,75 @@
import axios from 'axios';
const types = {
ANNOUNCEMENT_REQUEST: 'ANNOUNCEMENT_REQUEST',
AUTH_REQUEST: 'AUTH_REQUEST',
UPDATE_REQUEST: 'UPDATE_REQUEST',
};
export default {
state: {
announcements: [],
builds: [],
featuredBuilds: [],
user: {},
admin: false,
updateAvailable: false,
accessToken: ''
},
mutations: {
ANNOUNCEMENT_REQUEST(state, data) {
if (!data || !data.data) {
return;
}
state.announcements = data.data;
},
UPDATE_REQUEST(state) {
state.updateAvailable = true;
},
AUTH_REQUEST(state, data) {
if (!data) {
state.user = null;
state.admin = null;
state.accessToken = null;
return;
}
state.user = data.user;
window.bugsnagClient.user = state.user;
state.admin = data.admin;
state.accessToken = data.accessToken;
}
},
actions: {
async getAnnouncements({ commit }) {
let data;
try {
data = await axios.get('/api/announcement');
} catch (e) {
if (e.response.status !== 401) {
console.log(e);
}
}
commit(types.ANNOUNCEMENT_REQUEST, data);
},
async checkAuth({ commit }) {
let data;
try {
data = await axios.get('/api/checkauth', {
withCredentials: true
});
} catch (e) {
if (e && e.response && e.response.status !== 401) {
console.log(e);
}
}
if (data && data.data) {
return commit(types.AUTH_REQUEST, data.data);
}
return commit(types.AUTH_REQUEST, null);
},
async updateAvailable({ commit }) {
commit(types.UPDATE_REQUEST);
}
},
getters: {}
};

View File

@@ -0,0 +1,3 @@
import CommonStore from './common';
export { CommonStore };