Skip to content

Commit

Permalink
Merge pull request #56 from TEAM-Hearus/develop
Browse files Browse the repository at this point in the history
Feat: ํšŒ์˜ ์ „ ๋ฐฐํฌ
  • Loading branch information
Nangniya authored Aug 20, 2024
2 parents f20e26d + 9f0d788 commit 02ee89c
Show file tree
Hide file tree
Showing 26 changed files with 340 additions and 237 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Hearus: ๋ชจ๋‘์˜ ๋“ค์„ ๊ถŒ๋ฆฌ๋ฅผ ์œ„ํ•˜์—ฌ" />
<title>Hearus</title>
</head>
<body>
Expand Down
33 changes: 11 additions & 22 deletions src/apis/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API_URL } from '.';
import { API_URL, IApiResponse } from '.';

interface IEmailSignUpParams {
userEmail: string;
Expand All @@ -11,34 +11,30 @@ interface IEmailLoginParams {
userPassword: string;
}

interface IGoogleLoginParams {
interface ISocialLoginParams {
social: string;
state: string;
code: string;
}

interface ILoginResponse {
status: string;
msg: string;
object: ITokens;
}
interface ILoginResponse extends IApiResponse<ITokens> {}

interface IEmailSignupResponse {
status: string;
msg: string;
object: null;
success: boolean;
}
interface IEmailSignupResponse extends IApiResponse<null> {}

interface ITokens {
grantType: string;
accessToken: string;
refreshToken: string;
}

export const googleLogin = async ({ state, code }: IGoogleLoginParams) => {
export const OAuthLogin = async ({
social,
state,
code,
}: ISocialLoginParams) => {
try {
const res = await fetch(
`${API_URL}/login/oauth2/code/google?state=${state}&code=${code}`,
`${API_URL}/login/oauth2/code/${social}?state=${state}&code=${code}`,
{
credentials: 'include',
},
Expand Down Expand Up @@ -66,9 +62,6 @@ export const emailLogin = async ({
userIsOAuth: false,
}),
});
if (!res.ok) {
throw new Error('Login failed');
}
const data: ILoginResponse = await res.json();
return data.object;
} catch (error) {
Expand All @@ -95,10 +88,6 @@ export const emailSignUp = async ({
}),
});
const data: IEmailSignupResponse = await res.json();

if (!res.ok) {
throw new Error('SignUp failed');
}
return data;
} catch (error) {
throw error;
Expand Down
11 changes: 11 additions & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
/* ๊ฒฝ๋กœ */
export const API_URL: string = import.meta.env.VITE_API_URL;

export const SOCKETURL = import.meta.env.VITE_SOCKETIO_HOST;

/* API ๊ณตํ†ต */
export const getToken = () => {
const token = localStorage.getItem('token');
return token;
};

export interface IApiResponse<T> {
status: string;
msg: string;
object: T;
success: boolean;
}
3 changes: 0 additions & 3 deletions src/apis/record.ts

This file was deleted.

67 changes: 40 additions & 27 deletions src/apis/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { API_URL } from '.';
import { API_URL, IApiResponse, getToken } from '.';
import { IScheduleElement } from '../constants/schedule';
import { IScheduleElementDTO } from '../utils/schedule';
import { getToken } from './';
import { IScheduleElementDTO } from '../constants/schedule';

interface IGetScheduleResponse {
status: string;
msg: string;
object: {
id: number;
scheduleElements: IScheduleElement[];
name: string;
userId: string | null;
};
success: boolean;
interface IGetScheduleResponse extends IApiResponse<IGetScheduleObject> {}

interface IGetScheduleObject {
id: number;
scheduleElements: IScheduleElement[];
name: string;
userId: string | null;
}

export const getSchedule = async (
Expand All @@ -29,18 +25,18 @@ export const getSchedule = async (
},
);
const data: IGetScheduleResponse = await res.json();
if (data.msg === 'Schedule not found with name') {
await createNewScheduleName(name);
return getSchedule(name);
}
return data.object['scheduleElements'];
} catch (error) {
throw error;
}
};

interface IGetLectureByScheduleElementResponse {
status: string;
msg: string;
object: ILectureItem[];
success: boolean;
}
interface IGetLectureByScheduleElementResponse
extends IApiResponse<ILectureItem[]> {}

interface ILectureItem {
id: string;
Expand Down Expand Up @@ -70,12 +66,7 @@ export const getLectureByScheduleElement = async (id: number) => {
}
};

interface IPOSTScheduleElementResponse {
status: string;
msg: string;
object: null;
success: boolean;
}
interface IPOSTScheduleElementResponse extends IApiResponse<null> {}

export const addScheduleElement = async (
inputData: IScheduleElementDTO,
Expand All @@ -99,7 +90,7 @@ export const addScheduleElement = async (
const data: IPOSTScheduleElementResponse = await res.json();
return data.success;
} catch (error) {
throw new Error('Failed to delete schedule element');
throw error;
}
};

Expand Down Expand Up @@ -127,6 +118,28 @@ export const deleteScheduleElement = async (
const data: IPOSTScheduleElementResponse = await res.json();
return data.success;
} catch (error) {
throw new Error('Failed to delete schedule element');
throw error;
}
};

const createNewScheduleName = async (name: string) => {
const token = getToken();
try {
const res = await fetch(`${API_URL}/api/v1/schedule/addSchedule`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
name,
}),
});
const data = await res.json();
if (!data.success) {
throw new Error(data.msg);
}
} catch (error) {
throw error;
}
};
16 changes: 3 additions & 13 deletions src/apis/script.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { API_URL, getToken } from '.';
import { API_URL, getToken, IApiResponse } from '.';

interface IGetAllScriptResponse {
status: string;
msg: string;
object: IScriptInList[];
success: boolean;
}
interface IGetAllScriptResponse extends IApiResponse<IScriptInList[]> {}

interface IGetScriptDetailResponse {
status: string;
msg: string;
object: IScriptDetail;
success: boolean;
}
interface IGetScriptDetailResponse extends IApiResponse<IScriptDetail> {}

export interface IScriptInList {
id: string;
Expand Down
11 changes: 3 additions & 8 deletions src/apis/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { API_URL, getToken } from '.';
import { API_URL, getToken, IApiResponse } from '.';

interface IGenerateProblemResponse {
status: string;
msg: string;
object: IQuestion[];
success: boolean;
}
interface IGenerateProblemResponse extends IApiResponse<IQuestion[]> {}

export interface IQuestion {
type: string;
Expand Down Expand Up @@ -33,7 +28,7 @@ export const generateProblem = async (inputData: IProblemInput) => {
body: JSON.stringify(inputData),
});
const data: IGenerateProblemResponse = await res.json();
return data.object;
return data;
} catch (error) {
throw error;
}
Expand Down
9 changes: 2 additions & 7 deletions src/apis/user.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { API_URL, getToken } from '.';
import { API_URL, getToken, IApiResponse } from '.';

interface IGetUserInfoResponse {
status: string;
msg: string;
object: IUserInfo;
success: boolean;
}
interface IGetUserInfoResponse extends IApiResponse<IUserInfo> {}

export interface IUserInfo {
userId: string;
Expand Down
35 changes: 26 additions & 9 deletions src/components/molecules/RecordTagDropDown/RecordTagDropDown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMemo, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import useRecordModalStore from '../../../store/useRecordModalStore';
import useRecordModalStore, {
ITagItem,
} from '../../../store/useRecordModalStore';
import { IScheduleElement } from '../../../constants/schedule';
import { getSchedule } from '../../../apis/schedule';
import styles from './RecordTagDropDown.module.scss';
Expand All @@ -21,18 +23,33 @@ const RecordTagDropDown = () => {
const TAGS = useMemo(() => {
if (!data) return [];

return Array.from(new Set(data.map((item) => item.name)));
const tagObject: { [key: string]: number } = {};

data.forEach((item) => {
if (!tagObject.hasOwnProperty(item.name)) {
tagObject[item.name] = item.scheduleId;
}
});

return Object.entries(tagObject).map(([name, id]) => ({
name,
scheduleId: id,
}));
}, [data]);

const handleTagBtnClick = () => {
setIsTagBtnClicked((prev) => !prev);
};

const handleLiClick = (name: string) => {
updateRecordData({ tag: name });
const handleLiClick = (item: ITagItem) => {
updateRecordData({ tag: item.name, scheduleId: item.scheduleId });
setIsTagBtnClicked(false);
};

useEffect(() => {
console.log(recordData);
}, [recordData]);

return (
<div className={styles.wrapper}>
<button
Expand All @@ -43,14 +60,14 @@ const RecordTagDropDown = () => {
</button>
{isTagBtnClicked && (
<ul className={styles.tagsUl}>
{TAGS.map((name) => (
{TAGS.map((item) => (
<li
key={name}
key={item.name}
className={styles.tagLi}
onClick={() => handleLiClick(name)}
onClick={() => handleLiClick(item)}
role="option"
>
{name}
{item.name}
</li>
))}
</ul>
Expand Down
4 changes: 3 additions & 1 deletion src/components/molecules/ScriptToolTip/ScriptToolTip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const ScriptToolTip = ({ id, scheduleName }: IProps) => {
const deleteMutation = useMutation({
mutationFn: (id: number) => deleteScheduleElement(id, userInfo.userName),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['schedule', name] });
queryClient.invalidateQueries({
queryKey: ['schedule', userInfo.userName],
});
},
onError: () => {
alert('์‹œ๊ฐ„ํ‘œ ์‚ญ์ œ๋ฅผ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
Expand Down
Loading

0 comments on commit 02ee89c

Please sign in to comment.