From fa890258e43b32ff73ac6ea342f4ff7d95c6d51a Mon Sep 17 00:00:00 2001 From: Stanislav Romanov Date: Mon, 14 Nov 2022 15:00:30 +0300 Subject: [PATCH 1/2] 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 @@ +
+ + + + +
From 612b225bc8608a5cb1ece92832aa7c038676a0f0 Mon Sep 17 00:00:00 2001 From: Stanislav Romanov Date: Thu, 17 Nov 2022 14:32:38 +0300 Subject: [PATCH 2/2] Add uploading many documents --- .../indexes/item/documents/document/edit.js | 4 +- .../admin/indexes/item/documents/new.js | 37 +++++++------------ app/models/index.js | 11 +----- .../admin/indexes/item/documents/new.hbs | 25 ++++++++++++- app/utils/query.js | 6 +-- 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/app/controllers/admin/indexes/item/documents/document/edit.js b/app/controllers/admin/indexes/item/documents/document/edit.js index 23c3749..51a8ee4 100644 --- a/app/controllers/admin/indexes/item/documents/document/edit.js +++ b/app/controllers/admin/indexes/item/documents/document/edit.js @@ -28,7 +28,9 @@ export default class AdminIndexesItemDocumentsDocumentEditController extends Con this.isSaving = false; - this.toaster.taskToast(task); + if (task) { + this.toaster.taskToast(task); + } } catch (error) { this.errors = [error.error || error]; } diff --git a/app/controllers/admin/indexes/item/documents/new.js b/app/controllers/admin/indexes/item/documents/new.js index 6de710b..8f7a522 100644 --- a/app/controllers/admin/indexes/item/documents/new.js +++ b/app/controllers/admin/indexes/item/documents/new.js @@ -1,20 +1,18 @@ -import Controller from '@ember/controller'; +import AdminIndexesItemDocumentsDocumentEditController from './document/edit'; 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; +export default class AdminIndexesItemDocumentsNewController extends AdminIndexesItemDocumentsDocumentEditController { + fileInputId = 'upload-file'; - @tracked errors = []; - @tracked isSaving; + @action + async readFile(event) { + event.preventDefault(); - constructor() { - super(...arguments); + const formData = new FormData(event.currentTarget); + const data = Object.fromEntries(formData.entries()); + const text = await data[this.fileInputId].text(); - this.invoker = new ActionInvoker(); + this.save(text); } @action @@ -24,22 +22,15 @@ export default class AdminIndexesItemDocumentsNewController extends Controller { this.isSaving = true; try { - const task = await this.model.index.saveDocuments(JSON.parse(value)); + const task = await this.model.index.saveDocuments(value); this.isSaving = false; - this.toaster.taskToast(task); + if (task) { + 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 4221e6c..538d775 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -56,15 +56,8 @@ 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), - }); + saveDocuments(body) { + return query(`${this.path}/documents`, { method: 'POST', body }); } deleteDocument(document) { diff --git a/app/templates/admin/indexes/item/documents/new.hbs b/app/templates/admin/indexes/item/documents/new.hbs index 5cb8cc4..3f3f458 100644 --- a/app/templates/admin/indexes/item/documents/new.hbs +++ b/app/templates/admin/indexes/item/documents/new.hbs @@ -1,5 +1,28 @@ +
+ + + + +