From f74913cfb6d1cc22a938426fdb28d90bcf22e2d1 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 11 Nov 2022 09:43:30 +0000 Subject: [PATCH] Switched snippet update modal to new modal pattern refs https://github.com/TryGhost/Team/issues/1734 refs https://github.com/TryGhost/Team/issues/559 refs https://github.com/TryGhost/Ghost/issues/14101 - switches to newer modal patterns ready for later Ember upgrades --- .../editor/modals/update-snippet.hbs | 24 ++++++++++++ .../editor/modals/update-snippet.js | 37 ++++++++++++++++++ .../app/components/modal-update-snippet.hbs | 21 ---------- .../app/components/modal-update-snippet.js | 27 ------------- ghost/admin/app/controllers/editor.js | 39 +++---------------- ghost/admin/app/controllers/lexical-editor.js | 39 +++---------------- ghost/admin/app/templates/editor.hbs | 12 +----- ghost/admin/app/templates/lexical-editor.hbs | 12 +----- 8 files changed, 73 insertions(+), 138 deletions(-) create mode 100644 ghost/admin/app/components/editor/modals/update-snippet.hbs create mode 100644 ghost/admin/app/components/editor/modals/update-snippet.js delete mode 100644 ghost/admin/app/components/modal-update-snippet.hbs delete mode 100644 ghost/admin/app/components/modal-update-snippet.js diff --git a/ghost/admin/app/components/editor/modals/update-snippet.hbs b/ghost/admin/app/components/editor/modals/update-snippet.hbs new file mode 100644 index 0000000000..26031b4fd0 --- /dev/null +++ b/ghost/admin/app/components/editor/modals/update-snippet.hbs @@ -0,0 +1,24 @@ + \ No newline at end of file diff --git a/ghost/admin/app/components/editor/modals/update-snippet.js b/ghost/admin/app/components/editor/modals/update-snippet.js new file mode 100644 index 0000000000..3227dcc6bf --- /dev/null +++ b/ghost/admin/app/components/editor/modals/update-snippet.js @@ -0,0 +1,37 @@ +import Component from '@glimmer/component'; +import {inject as service} from '@ember/service'; +import {task} from 'ember-concurrency'; + +export default class UpdateSnippetModal extends Component { + @service notifications; + + @task({drop: true}) + *updateSnippetTask() { + const {snippet, updatedProperties: {mobiledoc}} = this.args.data; + + try { + snippet.mobiledoc = mobiledoc; + + yield snippet.save(); + + this.notifications.closeAlerts('snippet.save'); + this.notifications.showNotification( + `Snippet "${snippet.name}" updated`, + {type: 'success'} + ); + + return snippet; + } catch (error) { + if (!snippet.errors.isEmpty) { + this.notifications.showAlert( + `Snippet save failed: ${snippet.errors.messages.join('. ')}`, + {type: 'error', key: 'snippet.save'} + ); + } + snippet.rollbackAttributes(); + throw error; + } finally { + this.args.close(); + } + } +} diff --git a/ghost/admin/app/components/modal-update-snippet.hbs b/ghost/admin/app/components/modal-update-snippet.hbs deleted file mode 100644 index 9f56d41b6b..0000000000 --- a/ghost/admin/app/components/modal-update-snippet.hbs +++ /dev/null @@ -1,21 +0,0 @@ - -{{svg-jar "close"}} - - - - diff --git a/ghost/admin/app/components/modal-update-snippet.js b/ghost/admin/app/components/modal-update-snippet.js deleted file mode 100644 index 9a65bd8544..0000000000 --- a/ghost/admin/app/components/modal-update-snippet.js +++ /dev/null @@ -1,27 +0,0 @@ -import ModalComponent from 'ghost-admin/components/modal-base'; -import {alias} from '@ember/object/computed'; -import {inject as service} from '@ember/service'; -import {task} from 'ember-concurrency'; - -export default ModalComponent.extend({ - router: service(), - notifications: service(), - - snippet: alias('model.snippetRecord'), - - actions: { - confirm() { - this.updateSnippet.perform(); - } - }, - - updateSnippet: task(function* () { - try { - yield this.confirm(); - } catch (error) { - this.notifications.showAPIError(error, {key: 'snippet.update.failed'}); - } finally { - this.send('closeModal'); - } - }).drop() -}); diff --git a/ghost/admin/app/controllers/editor.js b/ghost/admin/app/controllers/editor.js index 1392008157..2169f8d72c 100644 --- a/ghost/admin/app/controllers/editor.js +++ b/ghost/admin/app/controllers/editor.js @@ -4,6 +4,7 @@ import DeletePostModal from '../components/modals/delete-post'; import DeleteSnippetModal from '../components/editor/modals/delete-snippet'; import PostModel from 'ghost-admin/models/post'; import PublishLimitModal from '../components/modals/limits/publish-limit'; +import UpdateSnippetModal from '../components/editor/modals/update-snippet'; import boundOneWay from 'ghost-admin/utils/bound-one-way'; import classic from 'ember-classic-decorator'; import config from 'ghost-admin/config/environment'; @@ -378,40 +379,10 @@ export default class EditorController extends Controller { } @action - toggleUpdateSnippetModal(snippetRecord, updatedProperties = {}) { - if (snippetRecord) { - this.set('snippetToUpdate', {snippetRecord, updatedProperties}); - } else { - this.set('snippetToUpdate', null); - } - } - - @action - updateSnippet() { - if (!this.snippetToUpdate) { - return Promise.reject(); - } - - const {snippetRecord, updatedProperties: {mobiledoc}} = this.snippetToUpdate; - snippetRecord.set('mobiledoc', mobiledoc); - - return snippetRecord.save().then(() => { - this.set('snippetToUpdate', null); - this.notifications.closeAlerts('snippet.save'); - this.notifications.showNotification( - `Snippet "${snippetRecord.name}" updated`, - {type: 'success'} - ); - return snippetRecord; - }).catch((error) => { - if (!snippetRecord.errors.isEmpty) { - this.notifications.showAlert( - `Snippet save failed: ${snippetRecord.errors.messages.join('. ')}`, - {type: 'error', key: 'snippet.save'} - ); - } - snippetRecord.rollbackAttributes(); - throw error; + async confirmUpdateSnippet(snippet, updatedProperties = {}) { + await this.modals.open(UpdateSnippetModal, { + snippet, + updatedProperties }); } diff --git a/ghost/admin/app/controllers/lexical-editor.js b/ghost/admin/app/controllers/lexical-editor.js index e8b4ecaf38..b0f03af525 100644 --- a/ghost/admin/app/controllers/lexical-editor.js +++ b/ghost/admin/app/controllers/lexical-editor.js @@ -4,6 +4,7 @@ import DeletePostModal from '../components/modals/delete-post'; import DeleteSnippetModal from '../components/editor/modals/delete-snippet'; import PostModel from 'ghost-admin/models/post'; import PublishLimitModal from '../components/modals/limits/publish-limit'; +import UpdateSnippetModal from '../components/editor/modals/update-snippet'; import boundOneWay from 'ghost-admin/utils/bound-one-way'; import classic from 'ember-classic-decorator'; import config from 'ghost-admin/config/environment'; @@ -379,40 +380,10 @@ export default class LexicalEditorController extends Controller { } @action - toggleUpdateSnippetModal(snippetRecord, updatedProperties = {}) { - if (snippetRecord) { - this.set('snippetToUpdate', {snippetRecord, updatedProperties}); - } else { - this.set('snippetToUpdate', null); - } - } - - @action - updateSnippet() { - if (!this.snippetToUpdate) { - return Promise.reject(); - } - - const {snippetRecord, updatedProperties: {mobiledoc}} = this.snippetToUpdate; - snippetRecord.set('mobiledoc', mobiledoc); - - return snippetRecord.save().then(() => { - this.set('snippetToUpdate', null); - this.notifications.closeAlerts('snippet.save'); - this.notifications.showNotification( - `Snippet "${snippetRecord.name}" updated`, - {type: 'success'} - ); - return snippetRecord; - }).catch((error) => { - if (!snippetRecord.errors.isEmpty) { - this.notifications.showAlert( - `Snippet save failed: ${snippetRecord.errors.messages.join('. ')}`, - {type: 'error', key: 'snippet.save'} - ); - } - snippetRecord.rollbackAttributes(); - throw error; + async confirmUpdateSnippet(snippet, updatedProperties = {}) { + await this.modals.open(UpdateSnippetModal, { + snippet, + updatedProperties }); } diff --git a/ghost/admin/app/templates/editor.hbs b/ghost/admin/app/templates/editor.hbs index 8541b580d7..40fee577ad 100644 --- a/ghost/admin/app/templates/editor.hbs +++ b/ghost/admin/app/templates/editor.hbs @@ -79,7 +79,7 @@ @onWordCountChange={{this.updateWordCount}} @snippets={{this.snippets}} @saveSnippet={{if this.canManageSnippets this.saveSnippet}} - @updateSnippet={{if this.canManageSnippets this.toggleUpdateSnippetModal}} + @updateSnippet={{if this.canManageSnippets this.confirmUpdateSnippet}} @deleteSnippet={{if this.canManageSnippets this.confirmDeleteSnippet}} @featureImage={{this.post.featureImage}} @featureImageAlt={{this.post.featureImageAlt}} @@ -126,16 +126,6 @@ @close={{this.toggleReAuthenticateModal}} @modifier="action wide" /> {{/if}} - - {{#if this.snippetToUpdate}} - - {{/if}} {{/if}} {{outlet}} diff --git a/ghost/admin/app/templates/lexical-editor.hbs b/ghost/admin/app/templates/lexical-editor.hbs index 722de479f5..310e48ba99 100644 --- a/ghost/admin/app/templates/lexical-editor.hbs +++ b/ghost/admin/app/templates/lexical-editor.hbs @@ -79,7 +79,7 @@ @onWordCountChange={{this.updateWordCount}} @snippets={{this.snippets}} @saveSnippet={{if this.canManageSnippets this.saveSnippet}} - @updateSnippet={{if this.canManageSnippets this.toggleUpdateSnippetModal}} + @updateSnippet={{if this.canManageSnippets this.confirmUpdateSnippet}} @deleteSnippet={{if this.canManageSnippets this.confirmDeleteSnippet}} @featureImage={{this.post.featureImage}} @featureImageAlt={{this.post.featureImageAlt}} @@ -127,16 +127,6 @@ @close={{this.toggleReAuthenticateModal}} @modifier="action wide" /> {{/if}} - - {{#if this.snippetToUpdate}} - - {{/if}} {{/if}} {{outlet}}