Merge pull request #35 from kaermorchen/feature/creating-many-documents

Add uploading many documents
This commit is contained in:
Stanislav Romanov 2022-11-17 14:36:21 +03:00 committed by GitHub
commit 9e354ca15b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 19 deletions

View file

@ -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];
}

View file

@ -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];
}
}
}

View file

@ -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];

View file

@ -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 });
}
}

View file

@ -8,7 +8,7 @@
<Tab @route='admin.indexes.item.delete' @text='Delete' />
<Tab
@route='admin.indexes.item.documents.new'
@text='Create new document'
@text='Create new documents'
class='ml-auto'
/>
</Tabs>

View file

@ -0,0 +1,45 @@
<form class='mb-4' {{on 'submit' this.readFile}}>
<Form::Element
@errors={{this.errors}}
@label='Upload file'
@helper='JSON format. The file must contains an array of documents.'
@for={{this.fileInputId}}
>
<input
type='file'
accept='application/json'
id={{this.fileInputId}}
name={{this.fileInputId}}
/>
</Form::Element>
<Button
type='submit'
@text='Upload'
@style='primary'
@state={{if this.isSaving 'pending'}}
/>
</form>
<div class='mb-4'>
<Form::Element @errors={{this.errors}} @label='Form'>
<MonacoEditor
@value={{json-stringify @model.documents}}
@uri={{@model.schema.uri}}
@language='json'
@schema={{@model.schema}}
@sendValue={{this.save}}
@invoker={{this.invoker}}
@onDidValidation={{set this 'errors'}}
/>
</Form::Element>
<Button
type='submit'
@text='Save'
@style='primary'
@onClick={{fn this.invoker.invoke 'invokeSendValue'}}
@state={{if this.isSaving 'pending'}}
/>
<Button @text='Cancel' @onClick={{this.cancel}} />
</div>

View file

@ -23,18 +23,18 @@ export default function query(path, options = {}) {
} else if (response.ok) {
return response.json();
} else {
response.json().then((reason) => {
return response.json().then((reason) => {
toaster.dangerToast({
header: reason.type,
text: reason.message,
});
throw new Error('Network response was not OK');
// throw new Error('Network response was not OK');
});
}
})
.catch((response) => {
response.json().then((reason) => {
return response.json().then((reason) => {
toaster.dangerToast({
header: reason.type,
text: reason.message,