koel/resources/assets/js/stores/user.js

218 lines
5.1 KiB
JavaScript
Raw Normal View History

import { each, find, without } from 'lodash';
2016-03-12 16:15:14 +00:00
import md5 from 'blueimp-md5';
2016-02-09 04:57:08 +00:00
import Vue from 'vue';
2016-03-11 09:55:04 +00:00
import NProgress from 'nprogress';
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
import { http } from '../services';
2015-12-13 04:42:28 +00:00
import stub from '../stubs/user';
2016-06-25 10:15:57 +00:00
export const userStore = {
2015-12-13 04:42:28 +00:00
stub,
state: {
users: [],
current: stub,
},
/**
* Init the store.
2016-01-07 09:03:38 +00:00
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} users The users in the system. Empty array if current user is not an admin.
* @param {Object} currentUser The current user.
2015-12-13 04:42:28 +00:00
*/
2015-12-22 17:46:54 +00:00
init(users, currentUser) {
2016-04-05 07:38:10 +00:00
this.all = users;
this.current = currentUser;
2015-12-13 04:42:28 +00:00
// Set the avatar for each of the users…
2016-04-05 07:38:10 +00:00
each(this.all, this.setAvatar);
2016-02-07 04:23:12 +00:00
2015-12-13 04:42:28 +00:00
// …and the current user as well.
this.setAvatar();
},
2015-12-22 17:46:54 +00:00
/**
2016-03-18 04:45:12 +00:00
* All users.
2016-02-07 04:23:12 +00:00
*
* @return {Array.<Object>}
2015-12-22 17:46:54 +00:00
*/
2016-03-18 04:45:12 +00:00
get all() {
2015-12-13 04:42:28 +00:00
return this.state.users;
},
2016-04-05 07:38:10 +00:00
/**
* Set all users.
*
* @param {Array.<Object>} value
*/
set all(value) {
this.state.users = value;
},
2015-12-13 04:42:28 +00:00
/**
* Get a user by his ID
2016-02-07 04:23:12 +00:00
*
2016-01-17 14:26:24 +00:00
* @param {Integer} id
2016-02-07 04:23:12 +00:00
*
2015-12-22 17:46:54 +00:00
* @return {Object}
2015-12-13 04:42:28 +00:00
*/
byId(id) {
2016-04-05 07:38:10 +00:00
return find(this.all, { id });
2015-12-13 04:42:28 +00:00
},
/**
2016-03-18 04:45:12 +00:00
* The current user.
2016-01-07 09:03:38 +00:00
*
2016-03-18 04:45:12 +00:00
* @return {Object}
*/
get current() {
return this.state.current;
},
/**
* Set the current user.
*
* @param {Object} user
2016-01-07 09:03:38 +00:00
*
* @return {Object}
2015-12-13 04:42:28 +00:00
*/
2016-03-18 04:45:12 +00:00
set current(user) {
2016-05-05 10:38:54 +00:00
return this.state.current = user;
2015-12-13 04:42:28 +00:00
},
/**
* Set a user's avatar using Gravatar's service.
2016-02-07 04:23:12 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {?Object} user The user. If null, the current user.
2015-12-13 04:42:28 +00:00
*/
setAvatar(user = null) {
if (!user) {
2016-03-18 04:45:12 +00:00
user = this.current;
2015-12-13 04:42:28 +00:00
}
2016-02-07 04:23:12 +00:00
Vue.set(user, 'avatar', `https://www.gravatar.com/avatar/${md5(user.email)}?s=256`);
2015-12-13 04:42:28 +00:00
},
2015-12-29 01:16:36 +00:00
/**
* Log a user in.
2016-02-07 04:23:12 +00:00
*
* @param {String} email
2016-01-17 14:26:24 +00:00
* @param {String} password
2016-01-07 09:03:38 +00:00
* @param {?Function} successCb
* @param {?Function} errorCb
2015-12-29 01:16:36 +00:00
*/
2016-01-01 06:16:17 +00:00
login(email, password, successCb = null, errorCb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2016-05-03 04:08:24 +00:00
http.post('me', { email, password }, () => successCb && successCb(), errorCb);
2015-12-29 01:16:36 +00:00
},
2016-01-25 10:37:14 +00:00
/**
* Log the current user out.
2016-02-07 04:23:12 +00:00
*
2016-01-25 10:37:14 +00:00
* @param {Function} cb The callback.
*/
logout(cb = null) {
2016-05-03 04:08:24 +00:00
http.delete('me', {}, () => cb && cb());
2016-01-25 10:37:14 +00:00
},
2015-12-13 04:42:28 +00:00
/**
* Update the current user's profile.
2016-02-07 04:23:12 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {string} password Can be an empty string if the user is not changing his password.
* @param {?Function} successCb
* @param {?Function} errorCb
2015-12-13 04:42:28 +00:00
*/
updateProfile(password = null, cb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2016-02-07 04:23:12 +00:00
http.put('me', {
2015-12-13 04:42:28 +00:00
password,
2016-03-18 04:45:12 +00:00
name: this.current.name,
email: this.current.email
2015-12-29 01:35:22 +00:00
}, () => {
2015-12-13 04:42:28 +00:00
this.setAvatar();
2016-05-03 04:08:24 +00:00
cb && cb();
2015-12-13 04:42:28 +00:00
}
);
},
/**
* Stores a new user into the database.
2016-02-07 04:23:12 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {string} name
* @param {string} email
* @param {string} password
* @param {?Function} cb
2015-12-13 04:42:28 +00:00
*/
store(name, email, password, cb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-29 01:35:22 +00:00
http.post('user', { name, email, password }, response => {
2016-03-28 13:38:14 +00:00
const user = response.data;
2016-02-07 04:23:12 +00:00
2015-12-13 04:42:28 +00:00
this.setAvatar(user);
2016-04-05 07:38:10 +00:00
this.all.unshift(user);
2015-12-13 04:42:28 +00:00
2016-05-03 04:08:24 +00:00
cb && cb();
2015-12-13 04:42:28 +00:00
});
},
2016-01-07 09:03:38 +00:00
/**
* Update a user's profile.
2016-02-07 04:23:12 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {Object} user
2016-01-17 14:26:24 +00:00
* @param {String} name
* @param {String} email
* @param {String} password
2016-01-07 09:03:38 +00:00
* @param {?Function} cb
*/
2015-12-13 04:42:28 +00:00
update(user, name, email, password, cb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-13 04:42:28 +00:00
http.put(`user/${user.id}`, { name, email, password }, () => {
this.setAvatar(user);
user.password = '';
2016-05-03 04:08:24 +00:00
cb && cb();
2015-12-13 04:42:28 +00:00
});
},
2016-01-07 09:03:38 +00:00
/**
* Delete a user.
2016-02-07 04:23:12 +00:00
*
2016-01-07 09:03:38 +00:00
* @param {Object} user
* @param {?Function} cb
*/
2015-12-13 04:42:28 +00:00
destroy(user, cb = null) {
2016-03-11 09:55:04 +00:00
NProgress.start();
2015-12-13 04:42:28 +00:00
http.delete(`user/${user.id}`, {}, () => {
2016-04-05 07:38:10 +00:00
this.all = without(this.all, user);
2015-12-13 04:42:28 +00:00
// Mama, just killed a man
// Put a gun against his head
// Pulled my trigger, now he's dead
// Mama, life had just begun
// But now I've gone and thrown it all away
// Mama, oooh
// Didn't mean to make you cry
// If I'm not back again this time tomorrow
// Carry on, carry on, as if nothing really matters
2016-02-07 04:23:12 +00:00
//
2015-12-13 04:42:28 +00:00
// Too late, my time has come
// Sends shivers down my spine
// Body's aching all the time
// Goodbye everybody - I've got to go
// Gotta leave you all behind and face the truth
// Mama, oooh
// I don't want to die
// I sometimes wish I'd never been born at all
/**
* Brian May enters the stage.
*/
2016-05-03 04:08:24 +00:00
cb && cb();
2015-12-13 04:42:28 +00:00
});
},
};