diff --git a/src/hooks/useDb.ts b/src/hooks/useDb.ts index 2d6c051..77e1d63 100644 --- a/src/hooks/useDb.ts +++ b/src/hooks/useDb.ts @@ -3,6 +3,7 @@ import { client, formatError, ClientResponseError, + API_URL, } from '../pocketbase'; import { databaseGenericError } from '../pages/auth/notifications'; import { @@ -18,11 +19,17 @@ type Partial = { export type TunesRecordPartial = Partial; +type TunesResponseList = { + items: TunesResponse[]; + totalItems: number; +} + const tunesCollection = client.collection(Collections.Tunes); -const iniFilesCollection = client.collection(Collections.IniFiles); + +const customEndpoint = `${API_URL}/api/custom`; const useDb = () => { - const updateTune = async (id: string, data: TunesRecordPartial) => { + const updateTune = async (id: string, data: TunesRecordPartial): Promise => { try { await tunesCollection.update(id, data); return Promise.resolve(); @@ -34,7 +41,7 @@ const useDb = () => { } }; - const createTune = async (data: TunesRecord) => { + const createTune = async (data: TunesRecord): Promise => { try { const record = await tunesCollection.create(data); @@ -47,54 +54,41 @@ const useDb = () => { } }; - const getTune = async (tuneId: string) => { - try { - const tune = await tunesCollection.getFirstListItem( - `tuneId = "${tuneId}"`, - { - expand: 'author', - }, - ); + const getTune = async (tuneId: string): Promise => { + const response = await fetch(`${customEndpoint}/tunes/byTuneId/${tuneId}`); - return Promise.resolve(tune); - } catch (error) { - if ((error as ClientResponseError).isAbort) { - return Promise.reject(new Error('Cancelled')); - } - - if ((error as ClientResponseError).status === 404) { - return Promise.resolve(null); - } - - Sentry.captureException(error); - databaseGenericError(new Error(formatError(error))); - - return Promise.reject(error); + if (response.ok) { + return response.json(); } + + if (response.status === 404) { + return Promise.resolve(null); + } + + Sentry.captureException(response); + databaseGenericError(new Error(response.statusText)); + + return Promise.reject(response.status); }; - const getIni = async (signature: string) => { - try { - const ini = await iniFilesCollection.getFirstListItem(`signature = "${signature}"`); + const getIni = async (signature: string): Promise => { + const response = await fetch(`${customEndpoint}/iniFiles/bySignature/${signature}`); - return Promise.resolve(ini); - } catch (error) { - if ((error as ClientResponseError).isAbort) { - return Promise.reject(new Error('Cancelled')); - } - - if ((error as ClientResponseError).status === 404) { - return Promise.resolve(null); - } - - Sentry.captureException(error); - databaseGenericError(new Error(formatError(error))); - - return Promise.reject(error); + if (response.ok) { + return response.json(); } + + if (response.status === 404) { + return Promise.resolve(null); + } + + Sentry.captureException(response); + databaseGenericError(new Error(response.statusText)); + + return Promise.reject(response.status); }; - const searchTunes = async (search: string, page: number, perPage: number) => { + const searchTunes = async (search: string, page: number, perPage: number): Promise => { const phrases = search.length > 0 ? search.replace(/ +(?= )/g, '').split(' ') : []; const filter = phrases .filter((phrase) => phrase.length > 1) @@ -124,7 +118,7 @@ const useDb = () => { } }; - const getUserTunes = async (userId: string, page: number, perPage: number) => { + const getUserTunes = async (userId: string, page: number, perPage: number): Promise => { try { const list = await tunesCollection.getList(page, perPage, { sort: '-updated', @@ -148,7 +142,7 @@ const useDb = () => { } }; - const autocomplete = async (attribute: string, search: string) => { + const autocomplete = async (attribute: string, search: string): Promise => { try { const items = await tunesCollection.getFullList(10, { filter: `${attribute} ~ "${search}"`, @@ -168,13 +162,13 @@ const useDb = () => { }; return { - updateTune: (tuneId: string, data: TunesRecordPartial): Promise => updateTune(tuneId, data), - createTune: (data: TunesRecord): Promise => createTune(data), - getTune: (tuneId: string): Promise => getTune(tuneId), - getIni: (tuneId: string): Promise => getIni(tuneId), - searchTunes: (search: string, page: number, perPage: number): Promise<{ items: TunesResponse[]; totalItems: number }> => searchTunes(search, page, perPage), - getUserTunes: (userId: string, page: number, perPage: number): Promise<{ items: TunesResponse[]; totalItems: number }> => getUserTunes(userId, page, perPage), - autocomplete: (attribute: string, search: string): Promise => autocomplete(attribute, search), + updateTune, + createTune, + getTune, + getIni, + searchTunes, + getUserTunes, + autocomplete, }; };