Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post duplicate fix #769

Merged
merged 2 commits into from Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 36 additions & 30 deletions modules/ept-posts/client/posts/parent.controller.js
Expand Up @@ -336,6 +336,8 @@ var ctrl = [ '$scope', '$stateParams', '$timeout', '$location', '$filter', '$sta
}
};

this.postSubmitted = false;

this.savePost = function() {
var post = ctrl.posting.post;
var type = post.id ? 'update' : 'create';
Expand All @@ -346,36 +348,40 @@ var ctrl = [ '$scope', '$stateParams', '$timeout', '$location', '$filter', '$sta
if (post.id) { postPromise = Posts.update(post).$promise; }
else { postPromise = Posts.save(post).$promise; }

postPromise.then(function(data) {
if (type === 'create') {
// Increment post count and recalculate ctrl.pageCount
ctrl.thread.post_count++;
ctrl.pageCount = Math.ceil(ctrl.thread.post_count / ctrl.limit);
// Go to last page in the thread and scroll to new post
var lastPage = ctrl.pageCount;
var params = angular.copy($stateParams);
params.page = lastPage;
delete params['#'];
delete params['start'];
delete params['threadId'];
$location.search(params).hash(data.id);
if (ctrl.page === lastPage) { ctrl.pullPage(); }
}
else if (type === 'update') {
var filtered = ctrl.posts.filter(function(p) { return p.id === data.id; });
var editPost = filtered.length > 0 && filtered[0] || {};
editPost.body_html = data.body_html;
editPost.body = data.body;
editPost.updated_at = data.updated_at;
editPost.metadata = data.metadata;
}
})
.then(closeEditor)
.catch(function(err) {
var error = err.data.message;
if (err.status === 429) { error = 'Post Rate Limit Exceeded'; }
Alert.error(error);
});
if (!ctrl.postSubmitted) {
ctrl.postSubmitted = true;
postPromise.then(function(data) {
if (type === 'create') {
// Increment post count and recalculate ctrl.pageCount
ctrl.thread.post_count++;
ctrl.pageCount = Math.ceil(ctrl.thread.post_count / ctrl.limit);
// Go to last page in the thread and scroll to new post
var lastPage = ctrl.pageCount;
var params = angular.copy($stateParams);
params.page = lastPage;
delete params['#'];
delete params['start'];
delete params['threadId'];
$location.search(params).hash(data.id);
if (ctrl.page === lastPage) { ctrl.pullPage(); }
}
else if (type === 'update') {
var filtered = ctrl.posts.filter(function(p) { return p.id === data.id; });
var editPost = filtered.length > 0 && filtered[0] || {};
editPost.body_html = data.body_html;
editPost.body = data.body;
editPost.updated_at = data.updated_at;
editPost.metadata = data.metadata;
}
})
.then(closeEditor)
.catch(function(err) {
var error = err.data.message;
if (err.status === 429) { error = 'Post Rate Limit Exceeded'; }
Alert.error(error);
})
.finally(function() { ctrl.postSubmitted = false; });
}
};

this.cancelPost = function() { if (discardAlert()) { closeEditor(); } };
Expand Down
2 changes: 1 addition & 1 deletion modules/ept-posts/client/posts/posts.html
Expand Up @@ -256,7 +256,7 @@ <h4>BBCode Lookup</h4>
<button class="inverted-button cancel" ng-click="PostsParentCtrl.cancelPost()">
Cancel
</button>
<button class="send" ng-click="PostsParentCtrl.savePost()" ng-disabled="form.title.$error.required || !PostsParentCtrl.canSave()">
<button class="send" ng-click="PostsParentCtrl.savePost()" ng-disabled="form.title.$error.required || !PostsParentCtrl.canSave() || PostsParentCtrl.postSubmitted">
<i class="fa fa-paper-plane" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;{{ PostsParentCtrl.posting.post.id ? 'Edit Post' : 'Send Reply' }}
</button>

Expand Down