ibt2/src/User.vue

94 lines
2.5 KiB
Vue
Raw Normal View History

2017-01-15 19:07:10 +01:00
<template>
<div id="user">
2017-01-16 21:06:10 +01:00
<md-card>
<md-card-header>
<span class="md-title">User: {{ user.username }}</span>
</md-card-header>
<md-card-content>
<md-input-container>
<label>Email</label>
<md-input v-model="user.email" />
</md-input-container>
<div class="md-body-2">Change password</div>
<md-input-container id="password-input" md-has-password>
<label>New password</label>
2017-01-19 22:03:52 +01:00
<md-input v-model="user.password" type="password" />
2017-01-16 21:06:10 +01:00
</md-input-container>
2017-01-19 22:03:52 +01:00
<md-switch v-if="loggedInUser.isAdmin" v-model="user.isAdmin" class="md-warn">is admin</md-switch>
<br />
<md-button id="save-button" class="md-raised md-primary" @click="save()">Save</md-button>
2017-01-16 21:06:10 +01:00
</md-card-content>
</md-card>
2017-01-19 21:14:52 +01:00
<ibt-snackbar ref="snackbarObj" />
2017-01-19 20:46:17 +01:00
<ibt-dialog ref="dialogObj" />
2017-01-15 19:07:10 +01:00
</div>
</template>
<script>
2017-01-19 20:46:17 +01:00
import IbtDialog from './IbtDialog.vue';
2017-01-19 21:14:52 +01:00
import IbtSnackbar from './IbtSnackbar.vue';
2017-01-19 20:46:17 +01:00
2017-01-15 19:07:10 +01:00
export default {
data () {
return {
2017-01-19 22:03:52 +01:00
user: {email: '', password: null, isAdmin: false},
2017-01-16 21:06:10 +01:00
password: null
2017-01-15 19:07:10 +01:00
}
},
2017-01-15 22:35:54 +01:00
computed: {
loggedInUser() {
return this.$store.state.loggedInUser;
}
},
2017-01-15 19:07:10 +01:00
beforeCreate: function() {
2017-01-16 21:06:10 +01:00
this.usersUrl = this.$resource('users{/id}');
2017-01-15 19:07:10 +01:00
},
mounted: function() {
2017-01-16 21:06:10 +01:00
this.getUser(this.$route.params.id);
2017-01-15 19:07:10 +01:00
},
methods: {
2017-01-16 21:06:10 +01:00
getUser(id) {
this.usersUrl.get({id: id}).then((response) => {
return response.json();
}, (response) => {
2017-01-19 20:46:17 +01:00
this.$refs.dialogObj.show({text: 'unable to get user'});
2017-01-16 21:06:10 +01:00
}).then((data) => {
this.user = data || {};
});
},
2017-01-19 20:46:17 +01:00
2017-01-16 21:06:10 +01:00
save() {
2017-01-19 22:03:52 +01:00
this.usersUrl.update({id: this.user._id}, this.user).then((response) => {
2017-01-16 21:06:10 +01:00
return response.json();
}, (response) => {
2017-01-19 20:46:17 +01:00
this.$refs.dialogObj.show({text: 'unable to save user settings'});
2017-01-16 21:06:10 +01:00
}).then((data) => {
this.user = data;
2017-01-19 21:14:52 +01:00
this.$refs.snackbarObj.show('user saved');
2017-01-16 21:06:10 +01:00
});
}
2017-01-19 20:46:17 +01:00
},
2017-01-19 21:14:52 +01:00
components: { IbtDialog, IbtSnackbar }
2017-01-15 19:07:10 +01:00
}
</script>
<style>
2017-01-19 22:21:50 +01:00
2017-01-16 21:06:10 +01:00
#user {
padding: 10px;
}
2017-01-19 22:03:52 +01:00
#save-button {
margin-top: 40px;
}
2017-01-19 22:21:50 +01:00
2017-01-15 19:07:10 +01:00
</style>