koel/resources/assets/js/components/shared/user-item.vue

134 lines
3.2 KiB
Vue
Raw Normal View History

2016-03-20 13:42:33 +00:00
<template>
2016-06-25 16:05:24 +00:00
<article class="user-item" :class="{ editing: editing }">
<div class="info" v-if="!editing">
<img :src="user.avatar" width="128" height="128" alt="">
<div class="right">
<div>
<h1>{{ user.name }}
<i v-if="isCurrentUser" class="you fa fa-check-circle"></i>
</h1>
<p>{{ user.email }}</p>
2016-03-20 13:42:33 +00:00
</div>
2016-06-25 16:05:24 +00:00
<div class="buttons">
2016-08-11 02:55:54 +00:00
<button class="btn btn-blue" @click="edit">
2016-06-25 16:05:24 +00:00
{{ isCurrentUser ? 'Update Profile' : 'Edit' }}
</button>
2016-08-11 02:55:54 +00:00
<button class="btn btn-red" @click="del">Delete</button>
2016-06-25 16:05:24 +00:00
</div>
</div>
</div>
<form class="edit" @submit.prevent="update" v-else>
<div class="input-row">
<label>Name</label>
<input type="text" v-model="user.name" required v-koel-focus="editing">
</div>
<div class="input-row">
<label>Email</label>
<input type="email" v-model="user.email" required>
</div>
<div class="input-row">
<label>Password</label>
<input type="password" v-model="user.password" placeholder="Leave blank for no changes">
</div>
<div class="input-row">
<label></label>
<button class="btn btn-green">Update</button>
<button class="btn btn-red" @click.prevent="cancelEdit">Cancel</button>
</div>
</form>
</article>
2016-03-20 13:42:33 +00:00
</template>
<script>
2016-06-25 16:05:24 +00:00
import { clone, assign } from 'lodash';
2016-08-11 02:55:54 +00:00
import swal from 'sweetalert';
2016-06-25 16:05:24 +00:00
import { userStore } from '../../stores';
2016-07-10 17:55:20 +00:00
import router from '../../router';
2016-06-25 16:05:24 +00:00
export default {
props: ['user'],
data() {
return {
editing: false,
confirmingDelete: false,
cached: {},
2016-03-20 13:42:33 +00:00
};
2016-06-25 16:05:24 +00:00
},
computed: {
/**
* Determine if the current logged in user is the user bound to this component.
*
* @return {Boolean}
*/
isCurrentUser() {
return this.user.id === userStore.current.id;
},
},
methods: {
/**
* Trigger editing a user.
* If the user is the current logged-in user, redirect to the profile screen instead.
*/
edit() {
if (this.isCurrentUser) {
2016-07-11 01:41:35 +00:00
router.go('profile');
2016-06-25 16:05:24 +00:00
return;
}
// Keep a cached version of the user for rolling back.
this.cached = clone(this.user);
this.editing = true;
},
/**
* Cancel editing a user.
*/
cancelEdit() {
// Restore the original user's properties
assign(this.user, this.cached);
this.editing = false;
},
/**
* Update the edited user.
*/
update() {
2016-06-27 06:11:35 +00:00
userStore.update(this.user, this.user.name, this.user.email, this.user.password). then(u => {
2016-06-25 16:05:24 +00:00
this.editing = false;
});
},
/**
* Kill off the freaking user.
*/
2016-08-11 02:55:54 +00:00
del() {
swal({
title: 'Hey…',
text: `Youre about to unperson ${this.user.name}. Are you sure?`,
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Certainly',
cancelButtonText: 'Oops',
}, () => {
userStore.destroy(this.user).then(() => {
this.$destroy(true);
});
2016-06-25 16:05:24 +00:00
});
},
},
};
2016-03-20 13:42:33 +00:00
</script>
<style lang="sass">
2016-06-25 16:05:24 +00:00
@import "../../../sass/partials/_vars.scss";
@import "../../../sass/partials/_mixins.scss";
2016-03-20 13:42:33 +00:00
</style>