From 74541568a645357d8ed9c6ca13eceb9723b06342 Mon Sep 17 00:00:00 2001 From: jeremie Date: Wed, 2 Oct 2024 16:52:41 +0200 Subject: [PATCH 1/2] Add phrase service Co-authored-by: Iris Benoit Co-authored-by: Fael Bassetti Co-authored-by: Laura Bergoens --- .../authenticated/synchronize-translations.js | 3 +- pix-editor/app/services/phrase.js | 15 ++++++++ pix-editor/tests/unit/services/phrase-test.js | 38 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pix-editor/app/services/phrase.js create mode 100644 pix-editor/tests/unit/services/phrase-test.js diff --git a/pix-editor/app/controllers/authenticated/synchronize-translations.js b/pix-editor/app/controllers/authenticated/synchronize-translations.js index f9ae11900..86705294a 100644 --- a/pix-editor/app/controllers/authenticated/synchronize-translations.js +++ b/pix-editor/app/controllers/authenticated/synchronize-translations.js @@ -4,11 +4,12 @@ import { inject as service } from '@ember/service'; export default class SynchronizeTranslationsController extends Controller { @service notifications; + @service phrase; @action async importFromPhrase() { try { - await this.model.download(); + await this.phrase.download(); this.notifications.success('Téléchargement des traductions depuis Phrase effectué.'); } catch { this.notifications.error('Erreur lors du téléchargement des traductions.'); diff --git a/pix-editor/app/services/phrase.js b/pix-editor/app/services/phrase.js new file mode 100644 index 000000000..3561e132d --- /dev/null +++ b/pix-editor/app/services/phrase.js @@ -0,0 +1,15 @@ +import Service, { inject as service } from '@ember/service'; +import fetch from 'fetch'; + +export default class PhraseService extends Service { + @service session; + + async download(fetchFn = fetch) { + await fetchFn('/api/phrase/download', { + method: 'POST', + headers: { + authorization: `Bearer ${this.session.data.authenticated.apiKey}`, + }, + }); + } +} diff --git a/pix-editor/tests/unit/services/phrase-test.js b/pix-editor/tests/unit/services/phrase-test.js new file mode 100644 index 000000000..e4efdcc5f --- /dev/null +++ b/pix-editor/tests/unit/services/phrase-test.js @@ -0,0 +1,38 @@ +import Service from '@ember/service'; +import { setupTest } from 'ember-qunit'; +import { module, test } from 'qunit'; +import sinon from 'sinon'; + +module('Unit | Service | storage', function(hooks) { + setupTest(hooks); + + hooks.beforeEach(function() { + class sessionServiceStub extends Service { + get data() { + return { authenticated: { apiKey: 'someApiKey' } }; + } + } + this.owner.register('service:session', sessionServiceStub); + }); + + module('download', function() { + test('it should trigger API call', async function(assert) { + // given + const phraseService = this.owner.lookup('service:phrase'); + const fetch = sinon.stub().resolves(); + + // when + await phraseService.download(fetch); + + // then + assert.ok(fetch.calledOnce); + assert.deepEqual(fetch.args[0], ['/api/phrase/download', { + method: 'POST', + headers: { + authorization: 'Bearer someApiKey', + }, + }]); + }); + + }); +}); From 6cc33b00ee031e54654daf006af48d46d9e79441 Mon Sep 17 00:00:00 2001 From: jeremie Date: Wed, 2 Oct 2024 16:53:43 +0200 Subject: [PATCH 2/2] remove useless phrase model Co-authored-by: Iris Benoit Co-authored-by: Fael Bassetti Co-authored-by: Laura Bergoens --- pix-editor/app/adapters/phrase.js | 7 ------- pix-editor/app/models/phrase.js | 10 ---------- .../routes/authenticated/synchronize-translations.js | 9 +-------- 3 files changed, 1 insertion(+), 25 deletions(-) delete mode 100644 pix-editor/app/adapters/phrase.js delete mode 100644 pix-editor/app/models/phrase.js diff --git a/pix-editor/app/adapters/phrase.js b/pix-editor/app/adapters/phrase.js deleted file mode 100644 index 0408ad2f4..000000000 --- a/pix-editor/app/adapters/phrase.js +++ /dev/null @@ -1,7 +0,0 @@ -import ApplicationAdapter from './application.js'; - -export default class PhraseAdapter extends ApplicationAdapter { - pathForType() { - return 'phrase'; - } -} diff --git a/pix-editor/app/models/phrase.js b/pix-editor/app/models/phrase.js deleted file mode 100644 index 7c242485a..000000000 --- a/pix-editor/app/models/phrase.js +++ /dev/null @@ -1,10 +0,0 @@ -import Model from '@ember-data/model'; -import { collectionAction } from 'ember-api-actions'; - -export default class PhraseModel extends Model { - download = collectionAction({ - path: 'download', - type: 'post', - urlType: 'findRecord', - }); -} diff --git a/pix-editor/app/routes/authenticated/synchronize-translations.js b/pix-editor/app/routes/authenticated/synchronize-translations.js index 89814fc43..4ca2c13ca 100644 --- a/pix-editor/app/routes/authenticated/synchronize-translations.js +++ b/pix-editor/app/routes/authenticated/synchronize-translations.js @@ -1,10 +1,3 @@ import Route from '@ember/routing/route'; -import { inject as service } from '@ember/service'; -export default class SynchronizeTranslationsRoute extends Route { - @service store; - - model() { - return this.store.createRecord('phrase'); - } -} +export default class SynchronizeTranslationsRoute extends Route {}