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 new file mode 100644 index 0000000..8f7a522 --- /dev/null +++ b/app/controllers/admin/indexes/item/documents/new.js @@ -0,0 +1,36 @@ +import AdminIndexesItemDocumentsDocumentEditController from './document/edit'; +import { action } from '@ember/object'; + +export default class AdminIndexesItemDocumentsNewController extends AdminIndexesItemDocumentsDocumentEditController { + fileInputId = 'upload-file'; + + @action + async readFile(event) { + event.preventDefault(); + + const formData = new FormData(event.currentTarget); + const data = Object.fromEntries(formData.entries()); + const text = await data[this.fileInputId].text(); + + this.save(text); + } + + @action + async save(value) { + this.errors = []; + + this.isSaving = true; + + try { + const task = await this.model.index.saveDocuments(value); + + this.isSaving = false; + + if (task) { + this.toaster.taskToast(task); + } + } catch (error) { + this.errors = [error.error || error]; + } + } +} diff --git a/app/models/index.js b/app/models/index.js index 8b377dd..538d775 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,10 @@ export default class Index { }); } + saveDocuments(body) { + return query(`${this.path}/documents`, { method: 'POST', body }); + } + 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..3f3f458 --- /dev/null +++ b/app/templates/admin/indexes/item/documents/new.hbs @@ -0,0 +1,45 @@ +
+ + + + +