Skip to content

Commit d70117f

Browse files
authored
Merge pull request #22 from team-Ollie/main
feat: 마이페이지, 캘린더 api 연결
2 parents 0396c64 + 38e3be2 commit d70117f

Some content is hidden

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

45 files changed

+2076
-234
lines changed

.github/workflows/nextjs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
push:
1010
branches: ["deploy"]
1111
pull_request:
12-
branches: ["deploy"]
12+
branches: ["deploy", "main"]
1313

1414
# Allows you to run this workflow manually from the Actions tab
1515
workflow_dispatch:

apis/auth.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import client from "./client";
2+
3+
export const SignIn = async (userData: {
4+
loginId: string;
5+
password: string;
6+
}) => {
7+
try {
8+
const response = await client.post(
9+
"/users/login",
10+
{
11+
loginId: userData.loginId,
12+
password: userData.password,
13+
},
14+
{
15+
headers: {
16+
"Content-Type": "application/json",
17+
},
18+
},
19+
);
20+
return response.data;
21+
} catch (error) {
22+
if (error.response) {
23+
// 200 이외
24+
console.error("서버 응답 오류:", error.response.data);
25+
} else if (error.request) {
26+
// 요청이 전송되었으나 응답을 받지 못한 경우
27+
console.error("응답 없음:", error.request);
28+
} else {
29+
// 요청을 설정하는 도중에 발생한 오류
30+
console.error("요청 설정 오류:", error.message);
31+
}
32+
throw error;
33+
}
34+
};

apis/calendar.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import client, { ResponseBody } from "./client";
2+
3+
interface GetMonthCalendarResponse extends ResponseBody {
4+
result: MonthCalendarProps[];
5+
}
6+
7+
export interface MonthCalendarProps {
8+
programIdx: number;
9+
name: string;
10+
openDate: {
11+
year: number;
12+
month: number;
13+
day: number;
14+
};
15+
dueDate: {
16+
year: number;
17+
month: number;
18+
day: number;
19+
};
20+
}
21+
22+
type CalendarDate = {
23+
year: number;
24+
month: number;
25+
day: number;
26+
};
27+
28+
interface GetProgramDetailBody {
29+
programIdx: number;
30+
name: string;
31+
openDate: CalendarDate;
32+
dueDate: CalendarDate;
33+
location: string;
34+
host: string;
35+
schedule: string;
36+
description: string;
37+
}
38+
39+
// 챌린지 월별 조회
40+
export const getMonthCalendar = async (): Promise<GetMonthCalendarResponse> => {
41+
const response = await client.get("/programs");
42+
// console.log("calenderData", response.data.result);
43+
return response.data.result;
44+
};
45+
46+
export const getProgramDetail = async (
47+
programIdx: number,
48+
): Promise<GetProgramDetailBody> => {
49+
// const response = await client.get(`/programs/${programIdx}`);
50+
const response = await client.get(`/programs/2`);
51+
// console.log("calenderDetail", response.data.result);
52+
return response.data.result;
53+
};

apis/challenge.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import client, { ResponseBody } from "./client";
2+
3+
interface GetMyChallengeListResponse extends ResponseBody {
4+
result: Challenge[];
5+
}
6+
7+
export interface Challenge {
8+
challengeIdx: number;
9+
name: string;
10+
participantsNum: number;
11+
location: string;
12+
schedule: string;
13+
attendanceRate: number;
14+
totalAttendanceRate: number;
15+
}
16+
async function getMyChallengeList(): Promise<GetMyChallengeListResponse> {
17+
const { data } = await client.get(`/challenges`);
18+
return data;
19+
}
20+
21+
export { getMyChallengeList };

apis/client.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,52 @@
11
import axios from "axios";
22

3+
interface ResponseBody {
4+
isSuccess: boolean;
5+
code: number;
6+
message: string;
7+
}
8+
9+
export const setTokenFromLocalStorage = (access_token: string) => {
10+
localStorage.setItem("access_token", access_token);
11+
};
12+
13+
const getTokenFromLocalStorage = () => {
14+
const accessToken = localStorage.getItem("access_token");
15+
if (!accessToken) {
16+
return null;
17+
}
18+
return accessToken;
19+
};
20+
321
const client = axios.create({
422
baseURL: process.env.NEXT_PUBLIC_API_URL,
523
withCredentials: true,
24+
headers: {
25+
"Access-Control-Allow-Origin": "http://localhost:3000",
26+
"Access-Control-Allow-Credentials": "true",
27+
},
28+
validateStatus: (status) => {
29+
return status < 300;
30+
},
631
});
732

33+
client.interceptors.request.use(
34+
async (config) => {
35+
if (typeof document !== "undefined") {
36+
const loginUrl = "/users/login";
37+
if (!config.url.includes(loginUrl)) {
38+
const token = getTokenFromLocalStorage();
39+
if (token) {
40+
config.headers.set("Authorization", `Bearer ${token}`);
41+
}
42+
}
43+
}
44+
return config;
45+
},
46+
(error) => {
47+
return Promise.reject(error);
48+
},
49+
);
50+
851
export default client;
52+
export type { ResponseBody };

apis/hooks/calendar.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useQuery } from "@tanstack/react-query";
2+
import { getMonthCalendar, getProgramDetail } from "../calendar";
3+
4+
const useGetMonthCalendar = () => {
5+
const { data } = useQuery({
6+
queryKey: ["getMonthCalendar"],
7+
queryFn: getMonthCalendar,
8+
});
9+
// console.log("isLoading", isLoading);
10+
console.log("Query Data", data);
11+
return { data };
12+
};
13+
14+
export { useGetMonthCalendar };
15+
16+
// const useGetProgramDetail = () => {
17+
// const { data } = useQuery({
18+
// queryKey: ["getProgramDetail"],
19+
// queryFn: getProgramDetail,
20+
// });
21+
// // console.log("isLoading", isLoading);
22+
// console.log("Query Data", data);
23+
// return { data };
24+
// };
25+
26+
// export { useGetProgramDetail };

apis/hooks/challenge.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getMyChallengeList } from "../challenge";
2+
import { useQuery } from "@tanstack/react-query";
3+
4+
function useGetMyChallengeList() {
5+
const { data } = useQuery({
6+
queryKey: ["getMyChallengeList"],
7+
queryFn: getMyChallengeList,
8+
});
9+
10+
return { data };
11+
}
12+
13+
export { useGetMyChallengeList };

apis/hooks/mypage.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { useQuery, useMutation } from "@tanstack/react-query";
2+
import {
3+
getMyInfo,
4+
patchLogout,
5+
patchNicknameChange,
6+
patchPasswordChange,
7+
patchQuitAccount,
8+
postNicknameCheck,
9+
} from "../mypage";
10+
import { useRouter } from "next/router";
11+
import { InputError } from "@/pages/mypage/password";
12+
13+
function useGetMyInfo() {
14+
const { data } = useQuery({
15+
queryKey: ["getMyInfo"],
16+
queryFn: getMyInfo,
17+
});
18+
19+
return { data };
20+
}
21+
22+
function usePatchLogout() {
23+
const router = useRouter();
24+
const { mutate } = useMutation({
25+
mutationKey: ["patchLogout"],
26+
mutationFn: patchLogout,
27+
onSuccess: () => {
28+
localStorage.removeItem("access_token");
29+
router.push("/main");
30+
},
31+
onError: () => router.push("/404"),
32+
});
33+
34+
return { mutate };
35+
}
36+
37+
function usePatchQuitAccount(
38+
setPwError: React.Dispatch<React.SetStateAction<InputError>>,
39+
) {
40+
const router = useRouter();
41+
const { mutate } = useMutation({
42+
mutationKey: ["patchQuitAccount"],
43+
mutationFn: (password: string) => patchQuitAccount(password),
44+
onSuccess: () => {
45+
localStorage.removeItem("access_token");
46+
router.push("/main");
47+
},
48+
onError: () =>
49+
setPwError({ status: true, text: "비밀번호가 올바르지 않습니다." }),
50+
});
51+
52+
return { mutate };
53+
}
54+
55+
function usePatchPasswordChange() {
56+
const router = useRouter();
57+
const { mutate } = useMutation({
58+
mutationKey: ["patchPasswordChange"],
59+
mutationFn: (body: { password: string; newPassword: string }) =>
60+
patchPasswordChange(body),
61+
onSuccess: () => router.push("/mypage/password/success"),
62+
onError: () => router.push("/404"),
63+
});
64+
65+
return { mutate };
66+
}
67+
68+
function usePatchNicknameChange(nickname: string) {
69+
const router = useRouter();
70+
const { mutate } = useMutation({
71+
mutationKey: ["postNicknameCheck", nickname],
72+
mutationFn: () => patchNicknameChange(nickname),
73+
onSuccess: () => router.push("/mypage/nickname/success"),
74+
onError: () => router.push("/404"),
75+
});
76+
77+
return { mutate };
78+
}
79+
80+
function usePostNicknameCheck(
81+
nickname: string,
82+
setNameError: React.Dispatch<React.SetStateAction<InputError>>,
83+
) {
84+
const { mutate } = useMutation({
85+
mutationKey: ["postNicknameCheck", nickname],
86+
mutationFn: () => postNicknameCheck(nickname),
87+
onSuccess: () => setNameError({ status: false, text: "" }),
88+
onError: () => {
89+
setNameError({ status: true, text: "이미 사용 중인 닉네임입니다." });
90+
},
91+
});
92+
93+
return { mutate };
94+
}
95+
96+
export {
97+
useGetMyInfo,
98+
usePatchLogout,
99+
usePatchQuitAccount,
100+
usePatchPasswordChange,
101+
usePatchNicknameChange,
102+
usePostNicknameCheck,
103+
};

apis/mypage.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import client, { ResponseBody } from "./client";
2+
3+
interface GetMyInfoResponse extends ResponseBody {
4+
result: {
5+
nickname: string;
6+
level: number;
7+
loginId: string;
8+
isAdmin: boolean;
9+
};
10+
}
11+
12+
interface PatchResponse {
13+
isSuccess: boolean;
14+
message: string;
15+
}
16+
17+
interface QuitAccountResponseBody {
18+
timestamp: string;
19+
status: number;
20+
error: string;
21+
code: string;
22+
message: string;
23+
}
24+
25+
async function getMyInfo(): Promise<GetMyInfoResponse> {
26+
const { data } = await client.get(`/users/myPage`);
27+
return data;
28+
}
29+
30+
async function patchLogout(): Promise<PatchResponse> {
31+
const { data } = await client.patch(`/users/logout`);
32+
return data;
33+
}
34+
35+
async function patchQuitAccount(
36+
password: string,
37+
): Promise<QuitAccountResponseBody> {
38+
const { data } = await client.patch(`/users/signout`, { password });
39+
return data;
40+
}
41+
42+
async function patchPasswordChange(body: {
43+
password: string;
44+
newPassword: string;
45+
}): Promise<PatchResponse> {
46+
const { data } = await client.patch(`/users/editPassword`, body);
47+
return data;
48+
}
49+
50+
async function patchNicknameChange(
51+
nickname: string,
52+
): Promise<QuitAccountResponseBody> {
53+
const { data } = await client.patch(`/users/editNickname`, { nickname });
54+
return data;
55+
}
56+
57+
async function postNicknameCheck(
58+
nickname: string,
59+
): Promise<QuitAccountResponseBody> {
60+
const { data } = await client.post(`/users/nickname`, { nickname });
61+
return data;
62+
}
63+
64+
export {
65+
getMyInfo,
66+
patchLogout,
67+
patchQuitAccount,
68+
patchPasswordChange,
69+
patchNicknameChange,
70+
postNicknameCheck,
71+
};

0 commit comments

Comments
 (0)