Show validation errors

This commit is contained in:
Stanislav Romanov 2022-10-15 13:27:45 +03:00
parent 300bf883e3
commit 2cae8467d6
6 changed files with 30 additions and 212 deletions

View file

@ -1,177 +0,0 @@
// import { dasherize } from '@ember/string';
// // import { languages } from 'monaco-editor/esm/vs/editor/editor.api.js';
// const urlSettingsPrefix = 'https://docs.meilisearch.com/reference/api/settings';
// export const indexSettings = [
// {
// name: 'displayedAttributes',
// schema: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// {
// name: 'distinctAttribute',
// schema: {
// type: ['string', 'null'],
// },
// },
// {
// name: 'faceting',
// schema: {
// type: 'object',
// properties: {
// maxValuesPerFacet: {
// type: 'integer',
// },
// },
// required: ['maxValuesPerFacet'],
// additionalProperties: false,
// },
// },
// {
// name: 'filterableAttributes',
// schema: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// {
// name: 'pagination',
// schema: {
// type: 'object',
// properties: {
// maxTotalHits: {
// type: 'integer',
// },
// },
// required: ['maxTotalHits'],
// additionalProperties: false,
// },
// },
// {
// name: 'rankingRules',
// schema: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// {
// name: 'searchableAttributes',
// schema: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// {
// name: 'sortableAttributes',
// schema: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// {
// name: 'stopWords',
// schema: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// {
// name: 'synonyms',
// schema: {
// type: 'object',
// additionalProperties: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// },
// {
// name: 'typoTolerance',
// schema: {
// type: 'object',
// properties: {
// enabled: {
// type: 'boolean',
// },
// minWordSizeForTypos: {
// type: 'object',
// properties: {
// oneTypo: { type: 'integer' },
// twoTypos: { type: 'integer' },
// },
// additionalProperties: false,
// },
// disableOnWords: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// disableOnAttributes: {
// type: 'array',
// items: {
// type: 'string',
// },
// uniqueItems: true,
// },
// },
// additionalProperties: false,
// },
// },
// ].map((item) => {
// item.uri = item.schema.$id = `${urlSettingsPrefix}/${dasherize(item.name)}`;
// item.fileMatch = [item.uri];
// return item;
// });
// const allSettings = {
// name: 'allSettings',
// uri: urlSettingsPrefix,
// fileMatch: [urlSettingsPrefix],
// schema: {
// $id: urlSettingsPrefix,
// type: 'object',
// properties: indexSettings.reduce((properties, item) => {
// properties[item.name] = {
// $ref: item.schema.$id,
// };
// return properties;
// }, {}),
// additionalProperties: false,
// },
// };
// indexSettings.unshift(allSettings);
// // TODO: do it async, not in main stream
// // languages.json.jsonDefaults.setDiagnosticsOptions({
// // validate: true,
// // schemas: indexSettings,
// // });

View file

@ -4,7 +4,7 @@
{{did-update this.setValue @setting}}
>
{{! TODO: add link to meilisearch's documentation }}
<Form::Element @label={{@label}} @errors={{array this.error}}>
<Form::Element @label={{@label}} @errors={{this.errors}}>
<MonacoEditor
@value={{json-stringify this.value}}
@uri={{@setting.uri}}
@ -12,7 +12,6 @@
@schema={{@setting}}
@sendValue={{this.save}}
@invoker={{this.invoker}}
@onDidChangeValidation={{this.onDidChangeValidation}}
class='h-40'
/>
</Form::Element>

View file

@ -3,53 +3,56 @@ import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import ActionInvoker from '../lib/action-invoker';
import { inject as service } from '@ember/service';
import { editor } from 'monaco-editor/esm/vs/editor/editor.api.js';
import { registerDestructor } from '@ember/destroyable';
export default class FormIndexSettingComponent extends Component {
@service toaster;
@tracked value;
@tracked error;
@tracked errors = [];
constructor() {
super(...arguments);
this.invoker = new ActionInvoker();
// Checking errors
const onDidChangeMarkersHandler = editor.onDidChangeMarkers(() => {
this.errors = editor.getModelMarkers().map((item) => item.message);
});
registerDestructor(this, onDidChangeMarkersHandler.dispose);
}
@action
save(newValue) {
this.error = null;
this.errors = [];
try {
return this.args.index
.updateSetting(JSON.parse(newValue), this.args.setting.name)
.then(this.toaster.taskToast);
} catch (error) {
this.error = error.error || error;
this.errors = [error.error || error];
}
}
@action
async setValue() {
this.value = await this.args.index.getSetting(this.args.setting.name);
this.error = null;
this.error = [];
}
@action
reset() {
this.error = null;
this.error = [];
try {
return this.args.index
.resetSetting(this.args.setting.name)
.then(this.toaster.taskToast);
} catch (error) {
this.error = error.error || error;
this.errors = [error.error || error];
}
}
@action
onDidChangeValidation(error) {
this.error = error;
}
}

View file

@ -82,15 +82,6 @@ export default class MonacoEditorComponent extends Component {
}
);
registerDestructor(this, onDidContentSizeChangeHandler.dispose);
// const onDidChangeMarkersHandler = editor.onDidChangeMarkers((uris) => {
// const error = uris.find((item) => item.toString() === this.args.uri);
// this.args.onDidChangeValidation?.(
// error ? 'Error: the value is invalid' : false
// );
// });
// registerDestructor(this, onDidChangeMarkersHandler.dispose);
}
@action

View file

@ -1,12 +1,8 @@
import query from '../utils/query';
import Document from './document';
import { dasherize } from '@ember/string';
import { indexSettings, allIndexSettings } from '../common';
export default class Index {
settings = indexSettings;
allSettings = allIndexSettings;
constructor(data = {}) {
for (const key in data) {
this[key] = data[key];

View file

@ -143,18 +143,11 @@ export const settings = [
additionalProperties: false,
},
},
].map((item) => {
item.uri = item.schema.$id = `${urlSettingsPrefix}/${dasherize(item.name)}`;
item.fileMatch = [item.uri];
return item;
});
];
// All settings
settings.unshift({
uri: urlSettingsPrefix,
fileMatch: [urlSettingsPrefix],
schema: {
$id: urlSettingsPrefix,
type: 'object',
properties: settings.reduce((properties, item) => {
properties[item.name] = item.schema;
@ -165,6 +158,19 @@ settings.unshift({
},
});
settings.forEach((item) => {
let uri = urlSettingsPrefix;
if (item.name) {
uri += `/${dasherize(item.name)}`;
}
item.uri = item.schema.$id = uri;
item.fileMatch = [uri];
return item;
});
export default class AdminIndexesItemSettingsRoute extends Route {
model() {
const index = this.modelFor('admin.indexes.item');