-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1120 from woowacourse-teams/FE/dev
[FE] 🚀 페어룸 종료 상태 STOMP 구현
- Loading branch information
Showing
5 changed files
with
67 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import useSocketStore from '@/stores/socketStore'; | ||
import useToastStore from '@/stores/toastStore'; | ||
|
||
import type { PairRoomStatus } from '@/apis/http/pairRoom'; | ||
import { subscribeTopic } from '@/apis/websocket/websocket'; | ||
|
||
const usePairRoomStatusSocket = (defaultStatus: PairRoomStatus) => { | ||
const navigate = useNavigate(); | ||
|
||
const { client, isConnected, accessCode } = useSocketStore(); | ||
const { addToast } = useToastStore(); | ||
|
||
const [status, setStatus] = useState<PairRoomStatus>(defaultStatus); | ||
|
||
const handleStatus = (status: PairRoomStatus) => setStatus(status); | ||
|
||
useEffect(() => { | ||
if (defaultStatus === 'COMPLETED') { | ||
navigate(`/room/${accessCode}/completed`, { state: { valid: true }, replace: true }); | ||
} | ||
|
||
if (status === 'COMPLETED') { | ||
addToast({ status: 'WARNING', message: '페어룸이 종료되었습니다.' }); | ||
navigate(`/room/${accessCode}/retrospectForm`, { state: { valid: true } }); | ||
} | ||
}, [status]); | ||
|
||
useEffect(() => { | ||
if (client && isConnected) { | ||
subscribeTopic<{ status: PairRoomStatus }>(client, `/topic/${accessCode}/pair-room/status`, (body) => | ||
handleStatus(body.status), | ||
); | ||
} | ||
|
||
return () => { | ||
if (client && isConnected) { | ||
client.unsubscribe(`/topic/${accessCode}/pair-room/status`); | ||
} | ||
}; | ||
}, [client]); | ||
|
||
return { status }; | ||
}; | ||
|
||
export default usePairRoomStatusSocket; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters