client/posts: add post deleting
This commit is contained in:
parent
179cf57cb9
commit
0c64d1fe1c
5 changed files with 56 additions and 0 deletions
|
@ -76,6 +76,9 @@
|
||||||
<% if (ctx.canFeaturePosts) { %>
|
<% if (ctx.canFeaturePosts) { %>
|
||||||
<li><a class='feature'>Feature this post on main page</a></li>
|
<li><a class='feature'>Feature this post on main page</a></li>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
<% if (ctx.canDeletePosts) { %>
|
||||||
|
<li><a class='delete'>Delete this post</a></li>
|
||||||
|
<% } %>
|
||||||
</ul>
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|
|
@ -61,6 +61,8 @@ class PostController {
|
||||||
'submit', e => this._evtPostEdit(e));
|
'submit', e => this._evtPostEdit(e));
|
||||||
this._view.sidebarControl.addEventListener(
|
this._view.sidebarControl.addEventListener(
|
||||||
'feature', e => this._evtPostFeature(e));
|
'feature', e => this._evtPostFeature(e));
|
||||||
|
this._view.sidebarControl.addEventListener(
|
||||||
|
'delete', e => this._evtPostDelete(e));
|
||||||
}
|
}
|
||||||
if (this._view.commentFormControl) {
|
if (this._view.commentFormControl) {
|
||||||
this._view.commentFormControl.addEventListener(
|
this._view.commentFormControl.addEventListener(
|
||||||
|
@ -115,6 +117,20 @@ class PostController {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_evtPostDelete(e) {
|
||||||
|
this._view.sidebarControl.disableForm();
|
||||||
|
this._view.sidebarControl.clearMessages();
|
||||||
|
e.detail.post.delete()
|
||||||
|
.then(() => {
|
||||||
|
misc.disableExitConfirmation();
|
||||||
|
const ctx = router.show('/posts');
|
||||||
|
ctx.controller.showSuccess('Post deleted.');
|
||||||
|
}, errorMessage => {
|
||||||
|
this._view.sidebarControl.showError(errorMessage);
|
||||||
|
this._view.sidebarControl.enableForm();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
_evtPostEdit(e) {
|
_evtPostEdit(e) {
|
||||||
this._view.sidebarControl.disableForm();
|
this._view.sidebarControl.disableForm();
|
||||||
this._view.sidebarControl.clearMessages();
|
this._view.sidebarControl.clearMessages();
|
||||||
|
|
|
@ -51,6 +51,10 @@ class PostListController {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showSuccess(message) {
|
||||||
|
this._pageController.showSuccess(message);
|
||||||
|
}
|
||||||
|
|
||||||
get _massTagTags() {
|
get _massTagTags() {
|
||||||
return (this._ctx.parameters.tag || '').split(/\s+/).filter(s => s);
|
return (this._ctx.parameters.tag || '').split(/\s+/).filter(s => s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,6 +89,11 @@ class PostEditSidebarControl extends events.EventTarget {
|
||||||
'click', e => this._evtFeatureClick(e));
|
'click', e => this._evtFeatureClick(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this._deleteLinkNode) {
|
||||||
|
this._deleteLinkNode.addEventListener(
|
||||||
|
'click', e => this._evtDeleteClick(e));
|
||||||
|
}
|
||||||
|
|
||||||
this._post.addEventListener(
|
this._post.addEventListener(
|
||||||
'changeContent', e => this._evtPostContentChange(e));
|
'changeContent', e => this._evtPostContentChange(e));
|
||||||
|
|
||||||
|
@ -131,6 +136,16 @@ class PostEditSidebarControl extends events.EventTarget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_evtDeleteClick(e) {
|
||||||
|
if (confirm('Are you sure you want to delete this post?')) {
|
||||||
|
this.dispatchEvent(new CustomEvent('delete', {
|
||||||
|
detail: {
|
||||||
|
post: this._post,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_evtSubmit(e) {
|
_evtSubmit(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.dispatchEvent(new CustomEvent('submit', {
|
this.dispatchEvent(new CustomEvent('submit', {
|
||||||
|
@ -207,6 +222,10 @@ class PostEditSidebarControl extends events.EventTarget {
|
||||||
return this._formNode.querySelector('.management .feature');
|
return this._formNode.querySelector('.management .feature');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get _deleteLinkNode() {
|
||||||
|
return this._formNode.querySelector('.management .delete');
|
||||||
|
}
|
||||||
|
|
||||||
enableForm() {
|
enableForm() {
|
||||||
views.enableForm(this._formNode);
|
views.enableForm(this._formNode);
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,6 +137,20 @@ class Post extends events.EventTarget {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete() {
|
||||||
|
return api.delete('/post/' + this._id)
|
||||||
|
.then(response => {
|
||||||
|
this.dispatchEvent(new CustomEvent('delete', {
|
||||||
|
detail: {
|
||||||
|
post: this,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
return Promise.resolve();
|
||||||
|
}, response => {
|
||||||
|
return Promise.reject(response.description);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
setScore(score) {
|
setScore(score) {
|
||||||
return api.put('/post/' + this._id + '/score', {score: score})
|
return api.put('/post/' + this._id + '/score', {score: score})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
|
Loading…
Reference in a new issue