client/users: refactor to match other models

This commit is contained in:
rr- 2016-07-26 21:02:21 +02:00
parent d2a5e1056d
commit 3f7ccfaea2
2 changed files with 57 additions and 40 deletions

View file

@ -6,6 +6,7 @@ const events = require('../events.js');
class User extends events.EventTarget { class User extends events.EventTarget {
constructor() { constructor() {
super(); super();
this._orig = {};
this._updateFromResponse({}); this._updateFromResponse({});
} }
@ -49,31 +50,31 @@ class User extends events.EventTarget {
save() { save() {
const files = []; const files = [];
const data = {}; const detail = {};
if (this.name !== this._origName) { const transient = this._orig._name;
data.name = this.name;
}
if (this._password) {
data.password = this._password;
}
if (this.email !== this._origEmail) { if (this._name !== this._orig._name) {
data.email = this.email; detail.name = this._name;
} }
if (this._email !== this._orig._email) {
if (this.rank !== this._origRank) { detail.email = this._email;
data.rank = this.rank;
} }
if (this.avatarStyle !== this._origAvatarStyle) { if (this._rank !== this._orig._rank) {
data.avatarStyle = this.avatarStyle; detail.rank = this._rank;
}
if (this._avatarStyle !== this._orig._avatarStyle) {
detail.avatarStyle = this._avatarStyle;
} }
if (this._avatarContent) { if (this._avatarContent) {
files.avatar = this._avatarContent; files.avatar = this._avatarContent;
} }
if (this._password) {
detail.password = this._password;
}
let promise = this._origName ? let promise = this._orig._name ?
api.put('/user/' + this._origName, data, files) : api.put('/user/' + this._orig._name, detail, files) :
api.post('/users', data, files); api.post('/users', detail, files);
return promise return promise
.then(response => { .then(response => {
@ -90,7 +91,7 @@ class User extends events.EventTarget {
} }
delete() { delete() {
return api.delete('/user/' + this._origName) return api.delete('/user/' + this._orig._name)
.then(response => { .then(response => {
this.dispatchEvent(new CustomEvent('delete', { this.dispatchEvent(new CustomEvent('delete', {
detail: { detail: {
@ -104,23 +105,23 @@ class User extends events.EventTarget {
} }
_updateFromResponse(response) { _updateFromResponse(response) {
this._name = response.name; const map = {
this._rank = response.rank; _name: response.name,
this._email = response.email; _rank: response.rank,
this._avatarStyle = response.avatarStyle; _email: response.email,
this._avatarUrl = response.avatarUrl; _avatarStyle: response.avatarStyle,
this._creationTime = response.creationTime; _avatarUrl: response.avatarUrl,
this._lastLoginTime = response.lastLoginTime; _creationTime: response.creationTime,
this._commentCount = response.commentCount; _lastLoginTime: response.lastLoginTime,
this._favoritePostCount = response.favoritePostCount; _commentCount: response.commentCount,
this._uploadedPostCount = response.uploadedPostCount; _favoritePostCount: response.favoritePostCount,
this._likedPostCount = response.likedPostCount; _uploadedPostCount: response.uploadedPostCount,
this._dislikedPostCount = response.dislikedPostCount; _likedPostCount: response.likedPostCount,
_dislikedPostCount: response.dislikedPostCount,
};
this._origName = this.name; Object.assign(this, map);
this._origRank = this.rank; Object.assign(this._orig, map);
this._origEmail = this.email;
this._origAvatarStyle = this.avatarStyle;
this._password = null; this._password = null;
this._avatarContent = null; this._avatarContent = null;

View file

@ -61,11 +61,27 @@ class UserEditView extends events.EventTarget {
this.dispatchEvent(new CustomEvent('submit', { this.dispatchEvent(new CustomEvent('submit', {
detail: { detail: {
user: this._user, user: this._user,
name: (this._userNameFieldNode || {}).value,
email: (this._emailFieldNode || {}).value, name: this._userNameFieldNode ?
rank: (this._rankFieldNode || {}).value, this._userNameFieldNode.value :
avatarStyle: (this._avatarStyleFieldNode || {}).value, undefined,
password: (this._passwordFieldNode || {}).value,
email: this._emailFieldNode ?
this._emailFieldNode.value :
undefined,
rank: this._rankFieldNode ?
this._rankFieldNode.value :
undefined,
avatarStyle: this._avatarStyleFieldNode ?
this._avatarStyleFieldNode.value :
undefined,
password: this._passwordFieldNode ?
this._passwordFieldNode.value :
undefined,
avatarContent: this._avatarContent, avatarContent: this._avatarContent,
}, },
})); }));