Skip to content

Commit f389361

Browse files
authored
Merge pull request #336 from JEOLLOGA/feat/#334/test-api
[FEAT] 테스트 기능 구현
2 parents ec0c8b5 + 99dc753 commit f389361

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+543
-159
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@vanilla-extract/vite-plugin": "^4.0.19",
2323
"axios": "^1.7.9",
2424
"cookies-next": "^6.0.0",
25+
"html-to-image": "^1.11.13",
2526
"jotai": "^2.11.1",
2627
"motion": "^12.10.1",
2728
"next": "^15.2.4",
82.5 KB
Loading
36.6 KB
Loading

public/test_og_img.png

144 KB
Loading

src/apis/auth/axios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import instance from '@apis/instance';
22

33
export const getKakaoLogin = async (code: string) => {
4-
const res = await instance.get(`v2/auth/login?code=${encodeURIComponent(code)}`);
4+
const res = await instance.get(`v2/auth/login/new?code=${encodeURIComponent(code)}`);
55

66
return res;
77
};

src/apis/auth/index.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { getKakaoLogin, postLogout, postWithdraw } from '@apis/auth/axios';
2+
import { useSaveTestResult } from '@apis/test';
3+
import { TestType } from '@constants/test';
24
import { useMutation } from '@tanstack/react-query';
35
import { deleteCookie, setCookie } from 'cookies-next';
46
import { useRouter } from 'next/navigation';
57

68
export const useGetKakaoLogin = () => {
79
const router = useRouter();
10+
const saveTestResultMutation = useSaveTestResult();
811

912
return useMutation({
1013
mutationFn: ({ code }: { code: string }) => getKakaoLogin(code),
11-
onSuccess: (response) => {
14+
onSuccess: async (response) => {
1215
const userNickname = response.data.data.nickname;
13-
const userId = response.data.data.userId;
14-
const userInfo = response.data.data.userInfo;
16+
const hasType = response.data.data.hasType;
1517

1618
setCookie('userNickname', userNickname, {
1719
httpOnly: false,
@@ -21,13 +23,25 @@ export const useGetKakaoLogin = () => {
2123
maxAge: 1209600, // 14일
2224
});
2325

24-
localStorage.setItem('userId', userId);
26+
setCookie('hasType', hasType, {
27+
httpOnly: false,
28+
secure: process.env.NODE_ENV === 'production',
29+
sameSite: 'lax',
30+
path: '/',
31+
maxAge: 1209600, // 14일
32+
});
2533

26-
if (!userInfo) {
27-
router.push('/onboarding');
28-
} else {
29-
router.push('/');
34+
// 비회원 상태에서 테스트 완료 후 로그인한 경우 테스트 결과 저장
35+
const type = sessionStorage.getItem('type') as TestType;
36+
37+
if (type) {
38+
try {
39+
await saveTestResultMutation.mutateAsync(type);
40+
} finally {
41+
sessionStorage.removeItem('type');
42+
}
3043
}
44+
router.push('/');
3145
},
3246
onError: (error) => {
3347
console.error(error);
@@ -43,6 +57,7 @@ export const usePostLogout = () => {
4357
onSuccess: () => {
4458
localStorage.clear();
4559
deleteCookie('userNickname');
60+
deleteCookie('hasType');
4661

4762
router.push('/');
4863
},
@@ -61,6 +76,7 @@ export const usePostWithdraw = () => {
6176
onSuccess: () => {
6277
localStorage.clear();
6378
deleteCookie('userNickname');
79+
deleteCookie('hasType');
6480
router.push('/');
6581
window.location.reload();
6682
},

src/apis/instance.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ instance.interceptors.response.use(
6868

6969
localStorage.clear();
7070
deleteCookie('userNickname');
71+
deleteCookie('hasType');
7172
console.error(error);
7273
alert(MESSAGES.EXPIRED);
7374
window.location.replace('/');

src/apis/test/axios.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import instance from '@apis/instance';
2+
import { ApiResponse } from '@apis/response';
3+
import { TestResponse } from '@apis/test/type';
4+
5+
export const postJbtiTest = async (result: string): Promise<TestResponse> => {
6+
const response = await instance.post<ApiResponse<TestResponse>>(`/v2/api/templestay/type`, {
7+
result,
8+
});
9+
10+
return response.data.data;
11+
};
12+
13+
export const postTestResult = async (type: string) => {
14+
const response = await instance.post('/v2/user/type', {
15+
type,
16+
});
17+
18+
return response;
19+
};

src/apis/test/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { postJbtiTest, postTestResult } from '@apis/test/axios';
2+
import { TestType } from '@constants/test';
3+
import { useMutation, useQueryClient } from '@tanstack/react-query';
4+
import { setCookie } from 'cookies-next';
5+
import { useRouter } from 'next/navigation';
6+
7+
export const usePostTestResult = () => {
8+
const router = useRouter();
9+
const queryClient = useQueryClient();
10+
11+
return useMutation({
12+
mutationFn: async (result: string) => {
13+
const [data] = await Promise.all([
14+
postJbtiTest(result),
15+
new Promise((resolve) => setTimeout(resolve, 2500)),
16+
]);
17+
18+
return data;
19+
},
20+
onSuccess: (data) => {
21+
setCookie('hasType', true, {
22+
httpOnly: false,
23+
secure: process.env.NODE_ENV === 'production',
24+
sameSite: 'lax',
25+
path: '/',
26+
maxAge: 1209600,
27+
});
28+
29+
queryClient.setQueryData(['test-result'], data);
30+
router.push(`/test/result`);
31+
},
32+
onError: (error) => {
33+
console.error(error);
34+
},
35+
});
36+
};
37+
38+
export const useSaveTestResult = () => {
39+
return useMutation({
40+
mutationFn: (type: TestType) => postTestResult(type),
41+
onSuccess: () => {
42+
setCookie('hasType', true, {
43+
httpOnly: false,
44+
secure: process.env.NODE_ENV === 'production',
45+
sameSite: 'lax',
46+
path: '/',
47+
maxAge: 1209600,
48+
});
49+
},
50+
});
51+
};

src/apis/test/type.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { TestType } from '@constants/test';
2+
3+
export interface TestResponse {
4+
code: TestType;
5+
tagline: string;
6+
description: string;
7+
requirement: string;
8+
bestMate: string;
9+
worstMate: string;
10+
}

0 commit comments

Comments
 (0)