Skip to content

Commit

Permalink
Merge pull request #88 from Gamegoo-repo/fix/78
Browse files Browse the repository at this point in the history
[Fix/78] ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ API ๋ณ€๊ฒฝ์— ๋”ฐ๋ฅธ ํ”„๋ก ํŠธ ์ฝ”๋“œ ์ˆ˜์ •
  • Loading branch information
Eunjin3395 authored Sep 23, 2024
2 parents 8d30880 + 01370b5 commit 3e4bd58
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
8 changes: 6 additions & 2 deletions public/scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ async function getFriendListApi(cursor) {
}
}

async function getChatroomListApi() {
async function getChatroomListApi(cursor) {
try {
const jwtToken = localStorage.getItem("jwtToken");
const response = await fetch(`${API_SERVER_URL}/v1/member/chatroom`, {
let url = `${API_SERVER_URL}/v1/member/chatroom`;
if (cursor) {
url += `?cursor=${cursor}`;
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${jwtToken}`, // Include JWT token in header
},
Expand Down
79 changes: 77 additions & 2 deletions public/scripts/eventListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,19 @@ fetchChatroomsButton.addEventListener("click", () => {
}

// (#8-1) ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ api ์š”์ฒญ
getChatroomListApi().then((result) => {
getChatroomListApi(null).then((result) => {
if (result) {
// (#8-2) ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ ์„ฑ๊ณต ์‘๋‹ต ๋ฐ›์Œ
// ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก element ์ดˆ๊ธฐํ™”
const chatroomListElement = document.getElementById("chatroomList");
chatroomListElement.innerHTML = "";

hasNextChatroom = result.has_next;
nextChatroomCursor = result.next_cursor;

// (#8-3) ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ๋ Œ๋”๋ง
// api result data๋ฅผ ๋Œ๋ฉด์„œ html ์š”์†Œ ์ƒ์„ฑ
result.forEach((chatroom) => {
result.chatroomViewDTOList.forEach((chatroom) => {
const li = document.createElement("li");
li.classList.add("chatroom-item");
li.setAttribute("data-chatroom-uuid", chatroom.uuid); // data-chatroom-uuid ๊ฐ’ ์„ธํŒ…
Expand Down Expand Up @@ -361,6 +364,78 @@ fetchChatroomsButton.addEventListener("click", () => {
});
});

// ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ๋‚ด๋ถ€์—์„œ ์Šคํฌ๋กค์ด ๊ฐ€์žฅ ์•„๋ž˜์— ๋‹ฟ์•˜์„ ๋•Œ
document.addEventListener("DOMContentLoaded", () => {
const chatroomListElement = document.getElementById("chatroomList");

chatroomListElement.addEventListener("scroll", () => {
// ์Šคํฌ๋กค์ด ๊ฐ€์žฅ ์•„๋ž˜์— ๋‹ฟ์•˜๋Š”์ง€ ํ™•์ธ
const isScrollAtBottom = chatroomListElement.scrollTop + chatroomListElement.clientHeight >= chatroomListElement.scrollHeight;

if (isScrollAtBottom) {
// ๋” ์กฐํšŒํ•ด์˜ฌ ๋‹ค์Œ ์นœ๊ตฌ ๋ชฉ๋ก์ด ์žˆ๋‹ค๋ฉด
if (hasNextChatroom) {
fetchNextChatrooms(nextChatroomCursor);
} else {
console.log("=== End of Chatroom List, No fetch ===");
}
}
});
});

// nextChatroomCursor ๊ธฐ๋ฐ˜์œผ๋กœ ๋‹ค์Œ ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ api ์š”์ฒญ ๋ฐ ํ™”๋ฉด ๋ Œ๋”๋ง
function fetchNextChatrooms(cursor) {
const jwtToken = localStorage.getItem("jwtToken");
if (!jwtToken) {
console.error("JWT token is missing.");
return;
}

getChatroomListApi(cursor).then((result) => {
if (result) {
// (#8-2) ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ ์„ฑ๊ณต ์‘๋‹ต ๋ฐ›์Œ
// ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก element ์ดˆ๊ธฐํ™”
const chatroomListElement = document.getElementById("chatroomList");

hasNextChatroom = result.has_next;
nextChatroomCursor = result.next_cursor;

// (#8-3) ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ๋ Œ๋”๋ง
// api result data๋ฅผ ๋Œ๋ฉด์„œ html ์š”์†Œ ์ƒ์„ฑ
result.chatroomViewDTOList.forEach((chatroom) => {
const li = document.createElement("li");
li.classList.add("chatroom-item");
li.setAttribute("data-chatroom-uuid", chatroom.uuid); // data-chatroom-uuid ๊ฐ’ ์„ธํŒ…

li.innerHTML = `
<div>
<img src="${chatroom.targetMemberImg}" alt="Profile Image" width="30" height="30">
</div>
<div class="chatroom-info">
<span>${chatroom.targetMemberName}</span>
<p last-msg-text>${chatroom.lastMsg ? chatroom.lastMsg : " "}</p>
</div>
<div>
<p last-msg-time>${new Date(chatroom.lastMsgAt).toLocaleString()}</p>
<p data-new-count>${chatroom.notReadMsgCnt}</p>
<button class="enter-chatroom-btn" data-chatroom-uuid="${chatroom.uuid}">์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ</button>
</div>
`;
chatroomListElement.appendChild(li);
});

// ์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ ๋ฒ„ํŠผ์— eventListener ์ถ”๊ฐ€
const enterChatroomButtons = document.querySelectorAll(".enter-chatroom-btn");
enterChatroomButtons.forEach((button) => {
button.addEventListener("click", (event) => {
const chatroomUuid = event.target.getAttribute("data-chatroom-uuid");
enterChatroom(chatroomUuid);
});
});
}
});
}

// ์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ ์‹œ
function enterChatroom(chatroomUuid) {
console.log(`Entering chatroom with UUID: ${chatroomUuid}`);
Expand Down
2 changes: 2 additions & 0 deletions public/scripts/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ let currentViewingChatroomUuid = null; // ํ˜„์žฌ ์ด ์‚ฌ์šฉ์ž๊ฐ€ ๋ณด๊ณ  ์žˆ๋Š”
let messagesFromThisChatroom = []; // ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” ์ฑ„ํŒ…๋ฐฉ์˜ ๋ฉ”์‹œ์ง€ ๋ชฉ๋ก
let hasNextChat = false; // ์ฑ„ํŒ… ๋‚ด์—ญ ์กฐํšŒ๋ฅผ ์œ„ํ•ด, ๋‹ค์Œ ์ฑ„ํŒ…๋‚ด์—ญ์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ์ €์žฅ
let hasNextFriend = false; // ์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ๋ฅผ ์œ„ํ•ด, ๋‹ค์Œ ์นœ๊ตฌ ๋ชฉ๋ก์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ์ €์žฅ
let hasNextChatroom = false; // ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ๋ฅผ ์œ„ํ•ด, ๋‹ค์Œ ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก์ด ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ์ €์žฅ
let nextFriendCursor = null; // ๋‹ค์Œ ์นœ๊ตฌ ๋ชฉ๋ก ์กฐํšŒ๋ฅผ ์œ„ํ•œ ์ปค์„œ
let nextChatroomCursor = null; // ๋‹ค์Œ ์ฑ„ํŒ…๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ๋ฅผ ์œ„ํ•œ ์ปค์„œ
let currentSystemFlag = null; // ํ˜„์žฌ ์ฑ„ํŒ…๋ฐฉ์—์„œ ๋ฉ”์‹œ์ง€ ์ „์†ก ์‹œ ๋ณด๋‚ด์•ผํ•  systemFlag ์ €์žฅ

const loginStatus = document.getElementById("loginStatus");
Expand Down
6 changes: 6 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ p[data-new-count] {

#chatroomList {
padding-left: 5px;
flex: 1;
overflow-y: auto;
}
#chatroomsList li {
display: flex; /* Use flexbox for alignment */
align-items: center; /* Center items vertically */
}

#friendsList {
Expand Down

0 comments on commit 3e4bd58

Please sign in to comment.