Skip to content

Commit

Permalink
Merge pull request #192 from prgrms-web-devcourse-final-project/feat/#…
Browse files Browse the repository at this point in the history
…188

✨Feat : 메이트 채팅방 동작 처리
  • Loading branch information
w-ho-choo authored Dec 9, 2024
2 parents a679481 + 2287d2f commit 3db4ab7
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 66 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@stomp/stompjs": "^7.0.0",
"@tanstack/react-query": "^5.60.2",
"dayjs": "^1.11.13",
"history": "^5.3.0",
"ky": "^1.7.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
2 changes: 0 additions & 2 deletions src/apis/mateChatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const mateChatService = {

// 메이트 채팅방 상세 조회
getMateChatRoomDetail: async (chatroomId: string) => {
console.log(123, chatroomId)

const response = await fetchApi
.get<MateChatRoomDetail>(`mates/chat/${chatroomId}/messages?page=0`)
.json()
Expand Down
22 changes: 15 additions & 7 deletions src/hooks/useChatExit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import goodsChatService from '@apis/goodsChatService'
import mateChatService from '@apis/mateChatService'
import queryClient, { QUERY_KEY } from '@apis/queryClient'
import { useMutation } from '@tanstack/react-query'
import { useNavigate } from 'react-router-dom'
import { toast } from 'react-toastify'

export const useGoodsChatExit = (chatRoomId: string) => {
const { mutate, isPending, isError, error } = useMutation({
Expand All @@ -11,11 +13,8 @@ export const useGoodsChatExit = (chatRoomId: string) => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEY.GOODS_CHAT_LIST] })
},

onSettled: (data, error) => {
if (error) {
console.error(error)
}
console.log(data)
onError: (error) => {
toast.error(error.message)
},
})

Expand All @@ -27,13 +26,22 @@ export const useGoodsChatExit = (chatRoomId: string) => {
}
}

export const useMateChatExit = (chatRoomId: string) => {
console.log(chatRoomId)
export const useMateChatExit = (chatRoomId: string, isChatRoom?: boolean) => {
const navigate = useNavigate()

const { mutate, isPending, isError, error } = useMutation({
mutationFn: () => mateChatService.exitMateChat(chatRoomId),

onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEY.MATE_CHAT_LIST] })

if (isChatRoom) {
navigate('/chat')
}
},

onError: (error) => {
toast.error(error.message)
},
})

Expand Down
11 changes: 10 additions & 1 deletion src/hooks/useCompleteMate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { useMutation } from '@tanstack/react-query'
import { RecruitStatus } from '@pages/ChatRoom/Rooms/MateChatRoom/RecruitStatusSection'
import queryClient, { QUERY_KEY } from '@apis/queryClient'
import { useLocation } from 'react-router-dom'
import { toast } from 'react-toastify'

interface RecruitData {
status: RecruitStatus
participantIds: number[]
}

export const useCompleteMate = () => {
export const useChangeMateRecruitStatus = () => {
const {
state: { postId },
} = useLocation()
Expand All @@ -22,6 +23,10 @@ export const useCompleteMate = () => {
queryClient.invalidateQueries({
queryKey: [QUERY_KEY.MATE_POST, postId],
})

toast.success('모임 상태가 변경되었습니다.', {
position: 'top-right',
})
},
})

Expand All @@ -38,6 +43,10 @@ export const useCompleteMatePost = (postId: string) => {
mutationFn: (data: { participantIds: number[] }) =>
mateChatService.completeMate(postId, data),

onError: (error) => {
toast.error(error.message)
},

onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [QUERY_KEY.MATE_POST, postId],
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/useCreateChatRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import { useMutation } from '@tanstack/react-query'
import mateChatService from '@apis/mateChatService'
import queryClient, { QUERY_KEY } from '@apis/queryClient'
import goodsChatService from '@apis/goodsChatService'
import { useNavigate } from 'react-router-dom'

export const useCreateMateChatRoom = (matePostId: string) => {
const navigate = useNavigate()
const { mutate, isPending, isError, error } = useMutation({
mutationFn: () => mateChatService.createMateChat(matePostId),

onSuccess: () => {
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEY.MATE_CHAT_LIST] })

if (data.status === 'SUCCESS') {
navigate(`/chat-room/메이트/${data.data.roomId}`, {
state: { postId: matePostId },
})
}
},

onSettled: (data, error) => {
Expand Down
6 changes: 1 addition & 5 deletions src/hooks/useNavigatChatRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ const useNavigatChatRoom = (currentTab: ChatType) => {
if (currentTab === '굿즈') {
navigate(`/chat-room/${currentTab}/${chatRoomId}`)
}
if (currentTab === '일반') {
navigate(`/chat-room/${currentTab}/${chatRoomId}`)
}
}

const isGoods = currentTab === '굿즈'
const isGeneral = currentTab === '일반'

return { handleChatCardClick, isGoods, isGeneral }
return { handleChatCardClick, isGoods }
}

export default useNavigatChatRoom
6 changes: 1 addition & 5 deletions src/hooks/useSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ export const useSocket = ({
const client = new Client({
webSocketFactory: () => new SockJS(wsUrl),

debug: (str: string) => console.log('STOMP Debug:', str),

heartbeatIncoming: 0,
heartbeatOutgoing: 0,

onConnect: (frame) => {
console.log('연결 성공:', frame)

onConnect: () => {
// 연결 성공 후 구독 설정
client.subscribe(subscribePoint, (message) => {
const receivedMessage = JSON.parse(message.body)
Expand Down
2 changes: 2 additions & 0 deletions src/layouts/SubLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SubGlobalContainer } from '@styles/globalStyle'
import { Outlet } from 'react-router-dom'
import { ToastContainer } from 'react-toastify'

const SubLayout = () => {
return (
<>
<SubGlobalContainer>
<ToastContainer />
<Outlet />
</SubGlobalContainer>
</>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ChatPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useGoodsChatExit, useMateChatExit } from '@hooks/useChatExit'
import MateCardList from './ChatPageList/MateCardList'
import { useMateChatStore } from '@store/useMateChatStore'

export const CHAT_TAB_LIST = ['메이트', '굿즈', '일반'] as const
export const CHAT_TAB_LIST = ['메이트', '굿즈'] as const
export type ChatType = (typeof CHAT_TAB_LIST)[number]

const ChatPage = () => {
Expand Down
25 changes: 14 additions & 11 deletions src/pages/ChatRoom/Rooms/MateChatRoom/MateModalContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const MateModalContent = ({ handleAlertClick }: ChatBottomModalProps) => {
const {
isOwner,
recruitStatus,
currentPostStatus,
setCurrentAlertStatus,
setParticipants,
setMateChatRoomId,
Expand Down Expand Up @@ -55,8 +56,10 @@ const MateModalContent = ({ handleAlertClick }: ChatBottomModalProps) => {
* 추후 로그인 기능 추가 시 변경 필요
*/
const cuurentMemberList = members
.map((member) => member.memberId)
.filter((memberId) => memberId !== Number(localStorage.getItem('memberId')))
.map((member: any) => member.memberId)
.filter(
(memberId: any) => memberId !== Number(localStorage.getItem('memberId')),
)

/**
* 각 버튼 클릭 시 현재 알럿창 상태 변경
Expand All @@ -66,6 +69,7 @@ const MateModalContent = ({ handleAlertClick }: ChatBottomModalProps) => {
switch (recruitStatus) {
case '모집완료':
setCurrentAlertStatus({ type: 'MATE_COMPLETE' })
setParticipants(cuurentMemberList)
break

case '모집중':
Expand All @@ -88,24 +92,23 @@ const MateModalContent = ({ handleAlertClick }: ChatBottomModalProps) => {
<Section>
<h2>대화상대</h2>
<div>
{members.map((member) => (
{members.map((member: any) => (
<MateUserCard
key={member.memberId}
member={member}
handleAlertClick={handleAlertClick}
/>
))}
</div>
</Section>

<SubmitButtonContainer $isOwner={isOwner}>
<SubmitButtonContainer
$isOwner={isOwner}
$isRecruitStatus={currentPostStatus === recruitStatus}
>
<button onClick={handleExitChatClick}>채팅방 나가기</button>
<button
disabled={!isOwner}
onClick={handleCompleteClick}
>
{recruitStatus}
</button>
{currentPostStatus !== recruitStatus && (
<button onClick={handleCompleteClick}>{recruitStatus}</button>
)}
</SubmitButtonContainer>
</ChatBottomModalContainer>
)
Expand Down
31 changes: 18 additions & 13 deletions src/pages/ChatRoom/Rooms/MateChatRoom/MateUserCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import { MateChatMember } from '@typings/mateChat'

interface MateUserCardProps {
member: MateChatMember
handleAlertClick: () => void
}

const MateUserCard = ({ member, handleAlertClick }: MateUserCardProps) => {
const MateUserCard = ({ member }: MateUserCardProps) => {
const {
isOwner,
recruitStatus,
setCurrentAlertStatus,
confirmedParticipants,
setConfirmedParticipants,
isOwner,
} = useMateChatStore()

const { imageUrl, nickname, memberId } = member

// 추후 API 연동 시 추가 예정
Expand All @@ -41,7 +40,9 @@ const MateUserCard = ({ member, handleAlertClick }: MateUserCardProps) => {
}
}

const isUser = memberId === Number(localStorage.getItem('memberId'))
const isCompleteRecruit = isOwner && recruitStatus === '직관완료'
const isMemberOwner = memberId === Number(localStorage.getItem('memberId'))

return (
<UserListCardContainer>
Expand All @@ -53,20 +54,24 @@ const MateUserCard = ({ member, handleAlertClick }: MateUserCardProps) => {
isChat
/>
<p>
{nickname} {isOwner && <span>(모임장)</span>}
{nickname} {isUser && <span>()</span>}
</p>
</UserInfo>
<ButtonContainer>
{isCompleteRecruit && (
<ConfirmationContainer>
<input
type='checkbox'
id={`confirm-${memberId}`}
name={`confirm-${memberId}`}
checked={confirmedParticipants.includes(memberId)}
onChange={handleConfirmation}
/>
<label htmlFor={`confirm-${memberId}`}>참가확인</label>
{!isMemberOwner && (
<>
<input
type='checkbox'
id={`confirm-${memberId}`}
name={`confirm-${memberId}`}
checked={confirmedParticipants.includes(memberId)}
onChange={handleConfirmation}
/>
<label htmlFor={`confirm-${memberId}`}>참가확인</label>
</>
)}
</ConfirmationContainer>
)}
</ButtonContainer>
Expand Down
Loading

0 comments on commit 3db4ab7

Please sign in to comment.