From 3b3db4b1427c71c6ce22d45c705e67be972c2d4d Mon Sep 17 00:00:00 2001 From: Ricky Date: Sun, 3 Nov 2019 11:03:11 +0000 Subject: [PATCH] Add discard changes button --- system/Controllers/ContentApiController.php | 31 ++++++++++++ system/Routes/Api.php | 1 + system/author/editor/publish-controller.twig | 1 + system/author/js/vue-editor.js | 1 + system/author/js/vue-publishcontroller.js | 52 +++++++++++++++++++- 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/system/Controllers/ContentApiController.php b/system/Controllers/ContentApiController.php index 490bd8b..8babb9b 100644 --- a/system/Controllers/ContentApiController.php +++ b/system/Controllers/ContentApiController.php @@ -151,6 +151,37 @@ class ContentApiController extends ContentController } } + public function discardArticleChanges(Request $request, Response $response, $args) + { + # get params from call + $this->params = $request->getParams(); + $this->uri = $request->getUri(); + + # set structure + if(!$this->setStructure($draft = true)){ return $response->withJson($this->errors, 404); } + + # set item + if(!$this->setItem()){ return $response->withJson($this->errors, 404); } + + # set redirect url to edit page + $url = $this->uri->getBaseUrl() . '/tm/content/' . $this->settings['editor'] . $this->item->urlRel; + + # remove the unpublished changes + $delete = $this->deleteContentFiles(['txt']); + + if($delete) + { + # update the backend structure + $this->setStructure($draft = true, $cache = false); + + return $response->withJson(['data' => $this->structure, 'errors' => false, 'url' => $url], 200); + } + else + { + return $response->withJson(['data' => $this->structure, 'errors' => $this->errors], 404); + } + } + public function deleteArticle(Request $request, Response $response, $args) { # get params from call diff --git a/system/Routes/Api.php b/system/Routes/Api.php index 712f211..b42fc04 100644 --- a/system/Routes/Api.php +++ b/system/Routes/Api.php @@ -11,6 +11,7 @@ $app->post('/api/v1/article/markdown', ContentApiController::class . ':getArticl $app->post('/api/v1/article/html', ContentApiController::class . ':getArticleHtml')->setName('api.article.html')->add(new RestrictApiAccess($container['router'])); $app->post('/api/v1/article/publish', ContentApiController::class . ':publishArticle')->setName('api.article.publish')->add(new RestrictApiAccess($container['router'])); $app->delete('/api/v1/article/unpublish', ContentApiController::class . ':unpublishArticle')->setName('api.article.unpublish')->add(new RestrictApiAccess($container['router'])); +$app->delete('/api/v1/article/discard', ContentApiController::class . ':discardArticleChanges')->setName('api.article.discard')->add(new RestrictApiAccess($container['router'])); $app->post('/api/v1/article', ContentApiController::class . ':createArticle')->setName('api.article.create')->add(new RestrictApiAccess($container['router'])); $app->put('/api/v1/article', ContentApiController::class . ':updateArticle')->setName('api.article.update')->add(new RestrictApiAccess($container['router'])); $app->delete('/api/v1/article', ContentApiController::class . ':deleteArticle')->setName('api.article.delete')->add(new RestrictApiAccess($container['router'])); diff --git a/system/author/editor/publish-controller.twig b/system/author/editor/publish-controller.twig index 5b86543..d70c87e 100644 --- a/system/author/editor/publish-controller.twig +++ b/system/author/editor/publish-controller.twig @@ -1,6 +1,7 @@
${ errors.message }
+
diff --git a/system/author/js/vue-editor.js b/system/author/js/vue-editor.js index c0a08ab..861236e 100644 --- a/system/author/js/vue-editor.js +++ b/system/author/js/vue-editor.js @@ -21,6 +21,7 @@ let editor = new Vue({ publishController.publishDisabled = false; publishController.draftResult = ""; publishController.publishResult = ""; + publishController.discardResult = ""; }, } }); \ No newline at end of file diff --git a/system/author/js/vue-publishcontroller.js b/system/author/js/vue-publishcontroller.js index e2bdb5f..ec628ab 100644 --- a/system/author/js/vue-publishcontroller.js +++ b/system/author/js/vue-publishcontroller.js @@ -19,6 +19,7 @@ let publishController = new Vue({ deleteDisabled: false, draftResult: "", publishResult: "", + discardResult: "", deleteResult: "", publishStatus: document.getElementById("publishController").dataset.published ? false : true, publishLabel: document.getElementById("publishController").dataset.published ? "online" : "offline", @@ -83,6 +84,53 @@ let publishController = new Vue({ } }, method, url, this.form ); }, + discardDraft: function(e) { + var self = this; + + self.errors.message = false; + editor.errors = {title: false, content: false}; + + self.discardResult = "load"; + self.publishDisabled = "disabled"; + + var url = self.root + '/api/v1/article/discard'; + var method = 'DELETE'; + + sendJson(function(response, httpStatus) + { + if(httpStatus == 400) + { + self.publishDisabled = false; + self.discardResult = "fail"; + self.errors.message = "You are probably logged out. Please backup your changes, login and then try again." + } + else if(response) + { + var result = JSON.parse(response); + + if(result.errors) + { + self.publishDisabled = false; + self.discardResult = "fail"; + + if(result.errors.title){ editor.errors.title = result.errors.title[0] } + if(result.errors.content){ editor.errors.content = result.errors.content[0] } + if(result.errors.message){ self.errors.message = result.errors.message } + } + else + { + window.location.replace(result.url); + } + } + else if(httpStatus != 200) + { + self.publishDisabled = false; + self.discardResult = "fail"; + self.errors.message = "Something went wrong, please refresh the page and try again." + } + + }, method, url, this.form); + }, saveDraft: function(e){ var self = this; @@ -93,7 +141,7 @@ let publishController = new Vue({ self.draftResult = "load"; var url = this.root + '/api/v1/article'; - var method = 'PUT'; + var method = 'PUT'; this.form.title = editor.form.title; this.form.content = editor.form.content; @@ -107,7 +155,7 @@ let publishController = new Vue({ self.errors.message = "You are probably logged out. Please backup your changes, login and then try again." } else if(response) - { + { var result = JSON.parse(response); if(result.errors)