Some main.ts refactoring

This commit is contained in:
mProjectsCode 2022-09-22 13:19:23 +02:00
parent 7ae51676a2
commit 6a0d192aba
8 changed files with 172 additions and 98 deletions

View file

@ -8,16 +8,16 @@ export class MediaDbAdvancedSearchModal extends Modal {
isBusy: boolean;
plugin: MediaDbPlugin;
searchBtn: ButtonComponent;
selectedApis: any;
onSubmit: (err: Error, result?: MediaTypeModel[]) => void;
selectedApis: {name: string, selected: boolean}[];
onSubmit: (res: {query: string, apis: string[]}, err?: Error) => void;
constructor(app: App, plugin: MediaDbPlugin, onSubmit?: (err: Error, result?: MediaTypeModel[]) => void) {
constructor(app: App, plugin: MediaDbPlugin, onSubmit?: (res: {query: string, apis: string[]}, err?: Error) => void) {
super(app);
this.plugin = plugin;
this.onSubmit = onSubmit;
this.selectedApis = [];
for (const api of this.plugin.apiManager.apis) {
this.selectedApis[api.apiName] = false;
this.selectedApis.push({name: api.apiName, selected: false});
}
}
@ -36,14 +36,9 @@ export class MediaDbAdvancedSearchModal extends Modal {
return;
}
let selectedAPICount = 0;
for (const api in this.selectedApis) {
if (this.selectedApis[api]) {
selectedAPICount += 1;
}
}
const apis: string[] = this.selectedApis.filter(x => x.selected).map(x => x.name);
if (selectedAPICount === 0) {
if (apis.length === 0) {
new Notice('MDB | No API selected');
return;
}
@ -54,12 +49,9 @@ export class MediaDbAdvancedSearchModal extends Modal {
this.searchBtn.setDisabled(false);
this.searchBtn.setButtonText('Searching...');
console.log(`MDB | query started with title ${this.query}`);
const res = await this.plugin.apiManager.query(this.query, this.selectedApis);
this.onSubmit(null, res);
this.onSubmit({query: this.query, apis: apis});
} catch (e) {
this.onSubmit(e);
this.onSubmit(null, e);
} finally {
this.close();
}
@ -96,9 +88,9 @@ export class MediaDbAdvancedSearchModal extends Modal {
const apiToggleComponent = new ToggleComponent(apiToggleComponentWrapper);
apiToggleComponent.setTooltip(api.apiName);
apiToggleComponent.setValue(this.selectedApis[api.apiName]);
apiToggleComponent.setValue(this.selectedApis.find(x => x.name === api.apiName).selected);
apiToggleComponent.onChange((value) => {
this.selectedApis[api.apiName] = value;
this.selectedApis.find(x => x.name === api.apiName).selected = value;
});
apiToggleComponentWrapper.appendChild(apiToggleComponent.toggleEl);
}

View file

@ -9,9 +9,9 @@ export class MediaDbIdSearchModal extends Modal {
plugin: MediaDbPlugin;
searchBtn: ButtonComponent;
selectedApi: string;
onSubmit: (err: Error, result?: MediaTypeModel) => void;
onSubmit: (res: {query: string, api: string}, err?: Error) => void;
constructor(app: App, plugin: MediaDbPlugin, onSubmit?: (err: Error, result?: MediaTypeModel) => void) {
constructor(app: App, plugin: MediaDbPlugin, onSubmit?: (res: {query: string, api: string}, err?: Error) => void) {
super(app);
this.plugin = plugin;
this.onSubmit = onSubmit;
@ -44,16 +44,9 @@ export class MediaDbIdSearchModal extends Modal {
this.searchBtn.setDisabled(false);
this.searchBtn.setButtonText('Searching...');
console.log(`MDB | query started with id ${this.query}`);
const api = this.plugin.apiManager.getApiByName(this.selectedApi);
if (!api) {
this.onSubmit(new Error('the selected api does not exist'));
}
const res = await api.getById(this.query);
this.onSubmit(null, res);
this.onSubmit({query: this.query, api: this.selectedApi});
} catch (e) {
this.onSubmit(e);
this.onSubmit(null, e);
} finally {
this.close();
}

View file

@ -6,13 +6,13 @@ import {SelectModal} from './SelectModal';
export class MediaDbSearchResultModal extends SelectModal<MediaTypeModel> {
plugin: MediaDbPlugin;
heading: string;
onSubmit: (error: Error, result: MediaTypeModel[]) => void;
onSubmit: (res: MediaTypeModel[], err?: Error) => void;
onCancel: () => void;
onSkip: () => void;
sendCallback: boolean;
constructor(app: App, plugin: MediaDbPlugin, elements: MediaTypeModel[], skipButton: boolean, onSubmit: (error: Error, result: MediaTypeModel[]) => void, onCancel: () => void, onSkip?: () => void) {
constructor(app: App, plugin: MediaDbPlugin, elements: MediaTypeModel[], skipButton: boolean, onSubmit: (res: MediaTypeModel[], err?: Error) => void, onCancel: () => void, onSkip?: () => void) {
super(app, elements);
this.plugin = plugin;
this.onSubmit = onSubmit;
@ -35,7 +35,7 @@ export class MediaDbSearchResultModal extends SelectModal<MediaTypeModel> {
// Perform action on the selected suggestion.
submit() {
this.onSubmit(null, this.selectModalElements.filter(x => x.isActive()).map(x => x.value));
this.onSubmit(this.selectModalElements.filter(x => x.isActive()).map(x => x.value));
this.sendCallback = true;
this.close();
}