Skip to content

Instantly share code, notes, and snippets.

@lukepolo
Last active November 5, 2018 21:09
Show Gist options
  • Save lukepolo/8e8179cae6e80b07a6833a18fed1ccab to your computer and use it in GitHub Desktop.
Save lukepolo/8e8179cae6e80b07a6833a18fed1ccab to your computer and use it in GitHub Desktop.
Vuex Sample - read filename inside of the file
import store from "./store";
var app = new Vue({
store,
}).$mount('#app-layout');
window.app = app;
export function action(action, parameters) {
// laroute is a package https://github.com/aaronlord/laroute
return laroute.action(action, parameters);
}
// store/index.js
import Vue from "vue";
import Vuex from "vuex";
import {action} from "./helpers";
import * as UserStore from "./modules/user";
Vue.action = action;
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
userStore: UserStore.user,
teamsStore: UserStore.teams,
userSshKeysStore: UserStore.sshKeys,
userSubscriptionsStore: UserStore.subscriptions,
userNotificationsStore: UserStore.notifications,
}
})
// store/modules/user/index.js
export const user = require('./UserStore').default;
export const teams = require('./UserTeamStore').default;
export const sshKeys = require('./UserSshKeyStore').default;
export const subscriptions = require('./UserSubscriptionStore').default;
export const notifications = require('./UserNotificationStore').default;
// store/modules/user/UserStore.js
export default {
state: {
user: user,
},
actions: {
getCurrentUser : ({commit}) => {
Vue.http.get(Vue.action('User\UserController@index')).then((response) => {
commit('SET_USER', response.data);
}, (errors) => {
app.showError(error);
});
},
getUser : ({commit}, userId) => {
return Vue.http.get(Vue.action('User\UserController@show', {user : userId})).then((response) => {
return response.data;
}, (errors) => {
app.showError(error);
});
},
updateUser: ({commit}, form) => {
Vue.http.put(Vue.action('User\UserController@update', {user: form.user_id}), form, {}).then((response) => {
commit('SET_USER', response.data);
}, (errors) => {
app.showError(error);
});
},
},
mutations: {
SET_USER: (state, user) => {
state.user = user;
}
}
}
<template>
<section>
<div class="jcf-form-wrap">
<form @submit.prevent="onSubmit" class="floating-labels">
<div class="jcf-input-group">
<input name="name" type="text" v-model="form.name">
<label for="name">
<span class="float-label">Name</span>
</label>
</div>
<div class="jcf-input-group">
<input name="email" type="email" v-model="form.email">
<label for="email">
<span class="float-label">Email - need to include email as a type for jcf forms</span>
</label>
</div>
<section v-if="user.password">
<div class="jcf-input-group">
<input name="new_password" type="password">
<label for="new_password">
<span class="float-label">New Password</span>
</label>
</div>
<div class="jcf-input-group">
<input name="confirm_password" type="password">
<label for="confirm_password">
<span class="float-label">Confirm Password</span>
</label>
</div>
</section>
<div class="btn-footer">
<button class="btn btn-primary" type="submit">Update Profile</button>
</div>
</form>
</div>
</section>
</template>
<script>
export default {
data() {
return {
found_user : null,
form: {
name: user.name,
email: user.email,
new_password: null,
confirm_password: null
}
}
},
computed: {
user() {
return this.$store.state.userStore.user;
}
},
created() {
this.fetchData();
},
watch: {
'$route': 'fetchData'
},
methods: {
fetchData() {
this.$store.dispatch('getCurrentUser');
// or we can get a speific user , look how you can return a promise
this.$store.dispatch('getUser', this.$route.params.user_id).then((user) => {
this.found_user = user;
});
},
onSubmit() {
this.form.user_id = this.user.id;
this.$store.dispatch('updateUser', this.form)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment