Skip to content

Commit

Permalink
update: BookSearchModal
Browse files Browse the repository at this point in the history
  • Loading branch information
anpigon committed Aug 17, 2022
1 parent 4ac843d commit 02a70f2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/apis/base_api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Book } from '@models/book.model';
import { BookSearchPluginSettings } from '@settings/settings';
import { ServiceProvider } from '@src/constants';
import { request } from 'obsidian';
import { GoogleBooksApi } from './google_books_api';
import { NaverBooksApi } from './naver_books_api';

export interface BaseBooksApiImpl {
getByQuery(query: string): Promise<Book[]>;
}

export function getServiceProvider(settings: BookSearchPluginSettings): BaseBooksApiImpl {
if (settings.serviceProvider === ServiceProvider.google) {
return new GoogleBooksApi();
}
if (settings.serviceProvider === ServiceProvider.naver) {
return new NaverBooksApi(settings.naverClientId, settings.naverClientSecret);
}
}

export class BaseBooksApi implements BaseBooksApiImpl {
getByQuery(_query: string): Promise<Book[]> {
throw new Error('Method not implemented.');
Expand Down
3 changes: 3 additions & 0 deletions src/apis/naver_books_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export class NaverBooksApi extends BaseBooksApi {

constructor(private readonly clientId, private readonly clientSecret: string) {
super();
if (!clientId || !clientSecret) {
throw new Error('네이버 개발자센터에서 발급받은 `Client ID`와 `Client Secret`이 설정되지 않았습니다.');
}
}

async getByQuery(query: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class BookSearchPlugin extends Plugin {

async openBookSearchModal(query = ''): Promise<Book> {
return new Promise((resolve, reject) => {
new BookSearchModal(this.app, query, (error, results) => {
new BookSearchModal(this, query, (error, results) => {
if (error) return reject(error);
new BookSuggestModal(this.app, results, (error2, selectedBook) => {
if (error2) return reject(error2);
Expand Down
13 changes: 8 additions & 5 deletions src/views/book_search_modal.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { App, ButtonComponent, Modal, Setting, TextComponent } from 'obsidian';
import { GoogleBooksApi } from '@apis/google_books_api';
import { ButtonComponent, Modal, Setting, TextComponent } from 'obsidian';
import { Book } from '@models/book.model';
import { BaseBooksApiImpl, getServiceProvider } from '@apis/base_api';
import BookSearchPlugin from '@src/main';

export class BookSearchModal extends Modal {
query: string;
isBusy: boolean;
okBtnRef: ButtonComponent;
onSubmit: (err: Error, result?: Book[]) => void;
serviceProvider: BaseBooksApiImpl;

constructor(app: App, query: string, onSubmit?: (err: Error, result?: Book[]) => void) {
super(app);
constructor(context: BookSearchPlugin, query: string, onSubmit?: (err: Error, result?: Book[]) => void) {
super(context.app);
this.query = query;
this.onSubmit = onSubmit;
this.serviceProvider = getServiceProvider(context.settings);
}

async searchBook() {
Expand All @@ -24,7 +27,7 @@ export class BookSearchModal extends Modal {
this.isBusy = true;
this.okBtnRef.setDisabled(false);
this.okBtnRef.setButtonText('Requesting...');
const searchResults = await new GoogleBooksApi().getByQuery(this.query);
const searchResults = await this.serviceProvider.getByQuery(this.query);

this.onSubmit(null, searchResults);
} catch (err) {
Expand Down

0 comments on commit 02a70f2

Please sign in to comment.