Skip to content

Commit

Permalink
Merge pull request #76 from KUIT-Space/feat#51-chat_detail
Browse files Browse the repository at this point in the history
Feat#51 chat detail
  • Loading branch information
YangJJune authored Aug 16, 2024
2 parents 9658c04 + 3036d83 commit 420b75b
Show file tree
Hide file tree
Showing 48 changed files with 1,088 additions and 143 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ dist-ssr
*.sw?
localhost-key.pem
localhost.pem
.env
*_mockChat.tsx
46 changes: 46 additions & 0 deletions src/apis/ChatroomCreateApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createRequestOptionsFORM_AUTH, fetchApi } from "@/apis/_createRequestOptions";

interface CreateChatroomApiResponseType {
code: number;
status: number;
message: string;
result: {
chatRoomId: number;
};
}

interface CreateChatroomApiRequestType {
img?: File | null; // ์ด๋ฏธ์ง€ ํŒŒ์ผ์€ ์„ ํƒ์ 
name: string;
memberList: number[];
}

const createChatroomFormData = (body: CreateChatroomApiRequestType): FormData => {
const formData = new FormData();
formData.append("name", body.name);

if (body.img) {
formData.append("img", body.img);
}

body.memberList.forEach((memberId) => {
formData.append("memberList", memberId.toString());
});

return formData;
};

export const createChatroomApi = async (
spaceId: number,
name: string,
memberList: number[],
img?: File | null,
) => {
const formData = createChatroomFormData({ name, memberList, img: img || null });
const requestOptions = createRequestOptionsFORM_AUTH("POST", formData);

if (!requestOptions) return null;

const url = `${import.meta.env.VITE_API_BACK_URL}/space/${spaceId}/chat/chatroom`;
return await fetchApi<CreateChatroomApiResponseType>(url, requestOptions);
};
25 changes: 25 additions & 0 deletions src/apis/ChatroomSearchAllApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createRequestOptionsJSON_AUTH, fetchApi } from "@/apis/_createRequestOptions";

export interface Chatroom {
id: number;
name: string;
imgUrl: string;
lastMsg: string;
lastTime: string;
unreadMsgCount: number;
}

interface ChatroomSearchApiResponse {
code: number;
status: number;
message: string;
result: { chatRoomList: Chatroom[] };
}

export const chatroomSearchAllApi = async (spaceId: number) => {
const requestOptions = createRequestOptionsJSON_AUTH("GET");
if (!requestOptions) return null;

const url = `${import.meta.env.VITE_API_BACK_URL}/space/${spaceId}/chat/chatroom`;
return await fetchApi<ChatroomSearchApiResponse>(url, requestOptions);
};
2 changes: 1 addition & 1 deletion src/apis/LoginApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRequestOptionsJSON, RequestOptions } from "./_createRequestOptions";
import { createRequestOptionsJSON, RequestOptions } from "@/apis/_createRequestOptions";

interface LoginApiResponseType {
code: number;
Expand Down
51 changes: 51 additions & 0 deletions src/apis/SpaceCreateApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* POST /space
* @summary Create a Space
* @body json์ด ์•„๋‹Œ form-data๋กœ ์ „์†กํ•ด์•ผ ํ•จ
* * spaceName Text(String) 1~10 ์ž ์ด๋‚ด์˜ ๋ฌธ์ž์—ด
* * spaceProfileImg File nullable / ์ด๋ฏธ์ง€ ํŒŒ์ผ ํ™•์žฅ์ž ํ˜•์‹์„ ์ง€ํ‚ค๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ
* @return status OK - success response
*/
import { createRequestOptionsFORM_AUTH, RequestOptions } from "@/apis/_createRequestOptions";

interface CreateSpaceApiResponseType {
code: number;
message: string;
status: string;
timestamp?: string;
}

interface CreateSpaceApiRequestType {
spaceName: string;
spaceProfileImg?: File | null; // ์ด๋ฏธ์ง€ ํŒŒ์ผ์€ ์„ ํƒ์ 
}

const fetchCreateSpaceApi = async (url: string, options: RequestOptions) => {
const response: CreateSpaceApiResponseType = await fetch(url, options)
.then((res) => res.json())
.catch((err) => {
console.error("[fetch error]", err);
throw err;
});

return response;
};

export const createSpaceApi = async (spaceName: string, spaceProfileImg?: File | null) => {
const body: CreateSpaceApiRequestType = {
spaceName: spaceName,
spaceProfileImg: spaceProfileImg || null,
};

const formData = new FormData();
formData.append("spaceName", body.spaceName);
if (body.spaceProfileImg) {
formData.append("spaceProfileImg", body.spaceProfileImg);
}

const requestOptions = createRequestOptionsFORM_AUTH("POST", formData);
const result = requestOptions
? await fetchCreateSpaceApi(`${import.meta.env.VITE_API_BACK_URL}/space`, requestOptions)
: null;

return result;
};
28 changes: 28 additions & 0 deletions src/apis/SpaceSearchAllUserApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createRequestOptionsJSON_AUTH, fetchApi } from "@/apis/_createRequestOptions";

// ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
export interface UserInfoInSpace {
userId: number;
userName: string;
profileImgUrl: string | null;
userAuth: "manager" | "normal";
}

interface SpaceSearchAllUserApiResponseType {
code: number;
status: number;
message: string;
result: {
userInfoInSpaceList: UserInfoInSpace[];
};
}

// API ํ•จ์ˆ˜ ์ •์˜
export const spaceSearchAllUserApi = async (spaceId: number) => {
const requestOptions = createRequestOptionsJSON_AUTH("GET");

if (!requestOptions) return null;

const url = `${import.meta.env.VITE_API_BACK_URL}/space/${spaceId}/all-member`;
return await fetchApi<SpaceSearchAllUserApiResponseType>(url, requestOptions);
};
Empty file added src/apis/_MOCK_
Empty file.
41 changes: 40 additions & 1 deletion src/apis/_createRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface RequestOptions {
method: "GET" | "POST" | "PATCH";
method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
body?: BodyInit;
headers?: HeadersInit;
redirect?: RequestRedirect;
Expand Down Expand Up @@ -30,6 +30,24 @@ export const createRequestOptionsJSON = (
// },
// });

export const createRequestOptionsFORM_AUTH = (
method: RequestOptions["method"],
body?: FormData,
) => {
const token = localStorage.getItem("Authorization");

return token
? ({
method: method,
body: body,
redirect: "follow",
headers: {
Authorization: token,
},
} as RequestOptions)
: null;
};

export const createRequestOptionsJSON_AUTH = (
method: RequestOptions["method"],
body?: RequestOptions["body"],
Expand All @@ -48,3 +66,24 @@ export const createRequestOptionsJSON_AUTH = (
}
: null;
};

/** Generic fetch API
*
*/
export const fetchApi = async <T>(url: string, options: RequestOptions): Promise<T> => {
const response: T = await fetch(url, options)
.then((res) => {
// 401 Unauthorized ์‹œ ์žฌ๋กœ๊ทธ์ธ ์š”์ฒญ
if (res.status === 401) {
alert("๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.");
}

return res.json();
})
.catch((err) => {
console.error("[fetch error]", err);
throw err;
});

return response;
};
3 changes: 3 additions & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "@/apis/LoginApi";
export * from "@/apis/ChatroomSearchAllApi";
export * from "@/apis/ChatroomCreateApi";
export * from "@/apis/SpaceCreateApi";
18 changes: 18 additions & 0 deletions src/assets/Characters/blue_1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions src/assets/Characters/blue_2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/assets/Characters/blue_3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/assets/Characters/blue_4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/assets/Characters/blue_5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/assets/Characters/green_1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 420b75b

Please sign in to comment.