Skip to content

Commit

Permalink
feat: 신규 유저일 경우 정보를 서버에 저장하고 불러오도록 대응
Browse files Browse the repository at this point in the history
  • Loading branch information
eunbae0 committed Feb 13, 2024
1 parent 4a7edcc commit 1e7c5e1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import uoslifeBridge from '../bridge';
import { FetchMethodType } from './types';

export default class APIClient {
static async APIclient<ResponseType>({
url,
method = 'GET',
}: {
url: string;
method?: FetchMethodType;
}): Promise<ResponseType> {
try {
const response = await this.fetchWithToken(`/core/${url}`);
const response = await this.fetchWithToken(`/core/${url}`, method);
return await response?.json();
} catch (error) {
throw Error();
}
}
static async fetchWithToken(input: RequestInfo | URL) {
static async fetchWithToken(
input: RequestInfo | URL,
method: FetchMethodType,
) {
const { accessToken } = await uoslifeBridge.getAccessToken();
if (accessToken)
return await fetch(input, {
headers: { Authorization: `Bearer ${accessToken}` },
method,
});
}
}
6 changes: 6 additions & 0 deletions src/api/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export default class APIService extends APIClient {
url: 'library-histories/recap?year=2023',
});
}
static async saveLibraryHistories() {
return await this.APIclient<Type.GetLibraryHistoriesResponse>({
url: 'library-histories/save?year=2023',
method: 'POST',
});
}
}
2 changes: 2 additions & 0 deletions src/api/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export type RecapInfoType = {

export type GetLibraryHistoriesResponse = RecapInfoType &
Partial<ErrorResponseType>;

export type FetchMethodType = 'GET' | 'POST';
10 changes: 7 additions & 3 deletions src/hooks/useInitApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ const useInitApp = () => {
const handleRecapInfo = async () => {
const recapInfo = await APIService.getLibraryHistories();
if (recapInfo.status === 400) {
alert('2023년 도서관 이용 기록이 없어요.');
await uoslifeBridge.goBack();
return;
await APIService.saveLibraryHistories();
const retryRecapInfo = await APIService.getLibraryHistories();
if (retryRecapInfo.status === 400) {
alert('2023년 도서관 이용 기록이 없어요.');
await uoslifeBridge.goBack();
return;
}
}
if (recapInfo.status === 401) {
alert('서비스를 이용하기 위해 앱 재접속이 필요합니다.');
Expand Down

0 comments on commit 1e7c5e1

Please sign in to comment.