From fa890258e43b32ff73ac6ea342f4ff7d95c6d51a Mon Sep 17 00:00:00 2001 From: Stanislav Romanov Date: Mon, 14 Nov 2022 15:00:30 +0300 Subject: [PATCH] Refactore form for creating many documents --- .../admin/indexes/item/documents/new.js | 45 +++++++++++++++++++ app/models/index.js | 31 +++++++++---- .../admin/indexes/item/documents/new.js | 19 +++++--- app/templates/admin/indexes/item.hbs | 2 +- .../admin/indexes/item/documents/new.hbs | 22 +++++++++ 5 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 app/controllers/admin/indexes/item/documents/new.js create mode 100644 app/templates/admin/indexes/item/documents/new.hbs diff --git a/app/controllers/admin/indexes/item/documents/new.js b/app/controllers/admin/indexes/item/documents/new.js new file mode 100644 index 0000000..6de710b --- /dev/null +++ b/app/controllers/admin/indexes/item/documents/new.js @@ -0,0 +1,45 @@ +import Controller from '@ember/controller'; +import { action } from '@ember/object'; +import { inject as service } from '@ember/service'; +import { tracked } from '@glimmer/tracking'; +import ActionInvoker from 'meiliadmin/lib/action-invoker'; + +export default class AdminIndexesItemDocumentsNewController extends Controller { + @service router; + @service toaster; + + @tracked errors = []; + @tracked isSaving; + + constructor() { + super(...arguments); + + this.invoker = new ActionInvoker(); + } + + @action + async save(value) { + this.errors = []; + + this.isSaving = true; + + try { + const task = await this.model.index.saveDocuments(JSON.parse(value)); + + this.isSaving = false; + + this.toaster.taskToast(task); + } catch (error) { + this.errors = [error.error || error]; + } + } + + @action + cancel() { + const newRouteName = this.router.currentRouteName.replace( + /\.(new|edit|delete)$/, + '' + ); + this.router.transitionTo(newRouteName); + } +} diff --git a/app/models/index.js b/app/models/index.js index 8b377dd..4221e6c 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -2,6 +2,8 @@ import query from '../utils/query'; import Document from './document'; import { dasherize } from '@ember/string'; +const uri = 'https://docs.meilisearch.com/reference/api/document'; + export default class Index { constructor(data = {}) { for (const key in data) { @@ -18,16 +20,18 @@ export default class Index { } get documentSetting() { - const uri = 'https://docs.meilisearch.com/reference/api/documents'; - return { - uri: uri, + uri, fileMatch: [uri], - schema: { - $id: uri, - type: 'object', - required: [this.primaryKey], - }, + schema: this.schema, + }; + } + + get schema() { + return { + $id: uri, + type: 'object', + required: [this.primaryKey], }; } @@ -52,6 +56,17 @@ export default class Index { }); } + saveDocuments(documents) { + if (typeof documents === 'string') { + documents = JSON.parse(documents); + } + + return query(`${this.path}/documents`, { + method: 'POST', + body: JSON.stringify(documents), + }); + } + deleteDocument(document) { const document_id = document[this.primaryKey]; diff --git a/app/routes/admin/indexes/item/documents/new.js b/app/routes/admin/indexes/item/documents/new.js index 6d2ebd2..7ed1f70 100644 --- a/app/routes/admin/indexes/item/documents/new.js +++ b/app/routes/admin/indexes/item/documents/new.js @@ -1,15 +1,22 @@ import Route from '@ember/routing/route'; -import Document from '../../../../../models/document'; import { hash } from 'rsvp'; export default class AdminIndexesItemDocumentsNewRoute extends Route { - templateName = 'admin.indexes.item.documents.document.edit'; - controllerName = 'admin.indexes.item.documents.document.edit'; - model() { const index = this.modelFor('admin.indexes.item'); - const document = new Document({ [index.primaryKey]: '' }); + const documents = [{ [index.primaryKey]: '' }]; + const uri = 'https://docs.meilisearch.com/reference/api/documents'; + const schema = { + uri, + fileMatch: [uri], + schema: { + $id: uri, + type: 'array', + items: index.schema, + minItems: 1, + }, + }; - return hash({ index, document }); + return hash({ index, documents, schema }); } } diff --git a/app/templates/admin/indexes/item.hbs b/app/templates/admin/indexes/item.hbs index fa64568..b0edd33 100644 --- a/app/templates/admin/indexes/item.hbs +++ b/app/templates/admin/indexes/item.hbs @@ -8,7 +8,7 @@ diff --git a/app/templates/admin/indexes/item/documents/new.hbs b/app/templates/admin/indexes/item/documents/new.hbs new file mode 100644 index 0000000..5cb8cc4 --- /dev/null +++ b/app/templates/admin/indexes/item/documents/new.hbs @@ -0,0 +1,22 @@ +
+ + + + +