From caa0d21ac938cd7ac12484a30206f2e967a73b63 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 28 May 2018 15:37:09 -0700 Subject: [PATCH 1/2] Handle state mutations for users better in Vuex --- resources/assets/scripts/models/user.js | 69 +++++++++++++++---------- resources/assets/scripts/store.js | 16 ++++-- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/resources/assets/scripts/models/user.js b/resources/assets/scripts/models/user.js index c7612377..e1a07c44 100644 --- a/resources/assets/scripts/models/user.js +++ b/resources/assets/scripts/models/user.js @@ -1,33 +1,48 @@ +import { Collection, Model } from 'vue-mc'; import JwtDecode from 'jwt-decode'; -const User = function () { - this.id = 0; - this.admin = false; - this.email = ''; -}; +export class User extends Model { + static defaults() { + return { + id: null, + uuid: '', + username: '', + email: '', + name_first: '', + name_last: '', + language: 'en', + root_admin: false, + } + } -/** - * Return a new instance of the user model using a JWT. - * - * @param {string} token - * @returns {User} - */ -User.prototype.fromJwt = function (token) { - return this.newModel(JwtDecode(token)); -}; + static mutations() { + return { + id: Number, + uuid: String, + username: String, + email: String, + name_first: String, + name_last: String, + language: String, + root_admin: Boolean, + } + } -/** - * Return an instance of this user model with the properties set on it. - * - * @param {object} obj - * @returns {User} - */ -User.prototype.newModel = function (obj) { - this.id = obj.id; - this.admin = obj.admin; - this.email = obj.email; + static fromJWT(token) { + return new User(JwtDecode(token).user || {}); + } +} - return this; -}; +export class UserCollection extends Collection { + static model() { + return User; + } -export default User; + get todo() { + return this.sum('done'); + } + + get done() { + return this.todo === 0; + } +} diff --git a/resources/assets/scripts/store.js b/resources/assets/scripts/store.js index f0e1da06..f49ffebc 100644 --- a/resources/assets/scripts/store.js +++ b/resources/assets/scripts/store.js @@ -1,4 +1,4 @@ -import User from './models/user'; +import { User } from './models/user'; export const storeData = { state: { @@ -13,15 +13,25 @@ export const storeData = { }, }, getters: { - user: function (state) { + getCurrentUser: function (state) { + if (!(state.user instanceof User)) { + state.user = User.fromJWT(localStorage.getItem('token')); + } + return state.user; }, }, mutations: { + /** + * Log in a user and store them in vuex using the local storage token. + * + * @param state + */ login: function (state) { - state.user = new User().fromJwt(localStorage.getItem('token')); + state.user = User.fromJWT(localStorage.getItem('token')); }, logout: function (state) { + console.log('logout'); state.user = null; } } From 9831adb91c955460da1e4b4cac3e78d9e7415d36 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 28 May 2018 16:28:36 -0700 Subject: [PATCH 2/2] Cleanup dashboard, make flash more customizable for pages --- resources/assets/scripts/components/Flash.vue | 15 +++++-- .../components/dashboard/Dashboard.vue | 41 +++++++++++++++---- .../assets/styles/components/spinners.css | 9 ++-- resources/lang/en/dashboard/index.php | 8 ++++ 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 resources/lang/en/dashboard/index.php diff --git a/resources/assets/scripts/components/Flash.vue b/resources/assets/scripts/components/Flash.vue index ac41e4dd..b42da3b1 100644 --- a/resources/assets/scripts/components/Flash.vue +++ b/resources/assets/scripts/components/Flash.vue @@ -1,7 +1,12 @@