11import { camelizeKeys } from "humps" ;
22
3- import { channelMethods } from "../../socket" ;
3+ import { channelMethods , channelTopics } from "../../socket" ;
44import { actions } from "../slices" ;
55
66import Channel from "./Channel" ;
@@ -12,7 +12,7 @@ export const setTournamentChannel = (tournamentId) => {
1212 return channel . setupChannel ( newChannelName ) ;
1313} ;
1414
15- export const connectToTournament = ( ) => ( dispatch ) => {
15+ export const connectToTournament = ( currentUserId ) => ( dispatch ) => {
1616 if ( ! channel ) {
1717 console . error ( "Channel not initialized" ) ;
1818 return ;
@@ -65,6 +65,29 @@ export const connectToTournament = () => (dispatch) => {
6565
6666 channel . join ( ) . receive ( "ok" , onJoinSuccess ) . receive ( "error" , onJoinFailure ) ;
6767
68+ const handleRunUpdated = ( response ) => {
69+ const latestSolutionEntry = response . solution || null ;
70+ const currentUserSolution =
71+ latestSolutionEntry && latestSolutionEntry . userId === currentUserId
72+ ? latestSolutionEntry
73+ : null ;
74+
75+ dispatch (
76+ actions . applyRunUpdate ( {
77+ groupTournament : response . groupTournament ,
78+ run : response . run ,
79+ latestSolutionEntry,
80+ solution : currentUserSolution ,
81+ } ) ,
82+ ) ;
83+
84+ if ( response . run ?. id ) {
85+ dispatch ( actions . addLog ( `Run #${ response . run . id } ${ response . run . status } ` ) ) ;
86+ }
87+ } ;
88+
89+ channel . addListener ( channelTopics . groupTournamentRunUpdated , handleRunUpdated ) ;
90+
6891 return channel ;
6992} ;
7093
0 commit comments