Skip to content

Commit

Permalink
refactor: No results found
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Aug 22, 2022
1 parent ab170ea commit 4859a52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/apis/google_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class GoogleBooksApi implements BaseBooksApiImpl {
params['langRestrict'] = langRestrict;
}
const searchResults = await apiGet<GoogleBooksResponse>('https://www.googleapis.com/books/v1/volumes', params);
if (searchResults.totalItems == 0) {
throw new Error('No results found.');
if (!searchResults?.totalItems) {
return [];
}
return searchResults.items.map(({ volumeInfo }) => this.createBookItem(volumeInfo));
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/apis/naver_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class NaverBooksApi implements BaseBooksApiImpl {
params,
header,
);
if (searchResults.total == 0) {
throw new Error('No results found.');
if (!searchResults?.total) {
return [];
}
return searchResults.items.map(this.createBookItem);
} catch (error) {
Expand Down
40 changes: 24 additions & 16 deletions src/views/book_search_modal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ButtonComponent, Modal, Setting, TextComponent } from 'obsidian';
import { ButtonComponent, Modal, Setting, TextComponent, Notice } from 'obsidian';
import { Book } from '@models/book.model';
import { BaseBooksApiImpl, factoryServiceProvider } from '@apis/base_api';
import BookSearchPlugin from '@src/main';
Expand All @@ -17,23 +17,33 @@ export class BookSearchModal extends Modal {
this.serviceProvider = factoryServiceProvider(plugin.settings);
}

setBusy(busy: boolean) {
this.isBusy = busy;
this.okBtnRef.setDisabled(busy);
this.okBtnRef.setButtonText(busy ? 'Requesting...' : 'Search');
}

async searchBook() {
if (!this.query) {
throw new Error('No query entered.');
}

if (!this.isBusy) {
try {
this.isBusy = true;
this.okBtnRef.setDisabled(false);
this.okBtnRef.setButtonText('Requesting...');
this.setBusy(true);
const searchResults = await this.serviceProvider.getByQuery(this.query);
this.setBusy(false);

if (!searchResults?.length) {
new Notice(`No results found for "${this.query}"`); // Couldn't find the book.
return;
}

this.callback(null, searchResults);
} catch (err) {
this.callback(err);
} finally {
this.close();
}
this.close();
}
}

Expand All @@ -56,16 +66,14 @@ export class BookSearchModal extends Modal {
.inputEl.addEventListener('keydown', this.submitEnterCallback.bind(this));
});

new Setting(contentEl)
.addButton(btn => btn.setButtonText('Cancel').onClick(() => this.close()))
.addButton(btn => {
return (this.okBtnRef = btn
.setButtonText('Ok')
.setCta()
.onClick(() => {
this.searchBook();
}));
});
new Setting(contentEl).addButton(btn => {
return (this.okBtnRef = btn
.setButtonText('Search')
.setCta()
.onClick(() => {
this.searchBook();
}));
});
}

onClose() {
Expand Down

0 comments on commit 4859a52

Please sign in to comment.