Skip to content

Commit b6f5da5

Browse files
committed
Fix invites
1 parent d32be91 commit b6f5da5

File tree

1 file changed

+63
-51
lines changed
  • services/app/apps/codebattle/assets/js/widgets/middlewares

1 file changed

+63
-51
lines changed

services/app/apps/codebattle/assets/js/widgets/middlewares/Invite.js

+63-51
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const camelizeKeysAndDispatch = (dispatch, actionCreator) => data => dispatch(ac
1313
const getRecipientName = data => data.invite.recipient.name;
1414
const getCreatorName = data => data.invite.creator.name;
1515
const getOpponentName = (data, userId) => {
16-
if (userId === data.invite.creator_id) {
16+
if (userId === data.invite.creatorId) {
1717
return getRecipientName(data);
1818
}
1919

20-
if (userId === data.invite.recipient_id) {
20+
if (userId === data.invite.recipientId) {
2121
return getCreatorName(data);
2222
}
2323

@@ -28,22 +28,26 @@ export const initInvites = currentUserId => dispatch => {
2828
const onJoinSuccess = () => {
2929
channel.addListener(channelTopics.invitesInitTopic, data => {
3030
if (data.invites.length > 0) {
31-
const message = getSystemMessage({ text: `You have (${data.invites.length}) invites to battle. Check ` });
31+
const message = getSystemMessage({
32+
text: `You have (${data.invites.length}) invites to battle. Check `,
33+
});
3234
setTimeout(() => dispatch(actions.newChatMessage(message)), 100);
3335
}
3436
dispatch(actions.setInvites(data));
3537
});
3638

3739
channel.addListener(channelTopics.invitesCreatedTopic, data => {
38-
if (data.invite.creator_id !== currentUserId) {
39-
const message = getSystemMessage({ text: `You received battle invite (from ${getCreatorName(data)})` });
40+
if (data.invite.creatorId !== currentUserId) {
41+
const message = getSystemMessage({
42+
text: `You received battle invite (from ${getCreatorName(data)})`,
43+
});
4044
dispatch(actions.newChatMessage(message));
4145
}
4246

4347
dispatch(actions.addInvite(data));
4448
});
4549
channel.addListener(channelTopics.invitesCanceledTopic, data => {
46-
if (data.invite.executor_id !== currentUserId) {
50+
if (data.invite.executorId !== currentUserId) {
4751
const message = getSystemMessage({
4852
text: `Invite has been canceled (Opponent ${getOpponentName(data, currentUserId)})`,
4953
status: 'failure',
@@ -54,14 +58,16 @@ export const initInvites = currentUserId => dispatch => {
5458
dispatch(actions.updateInvite(data));
5559
});
5660
channel.addListener(channelTopics.invitesAcceptedTopic, data => {
57-
if (data.invite.executor_id !== currentUserId) {
61+
if (data.invite.executorId !== currentUserId) {
5862
const message = getSystemMessage({
5963
text: `Invite has been accepted (Opponent ${getOpponentName(data, currentUserId)})`,
6064
status: 'success',
6165
});
6266
dispatch(actions.newChatMessage(message));
6367
}
64-
setTimeout(() => { window.location.href = `/games/${data.invite.game_id}`; }, 250);
68+
setTimeout(() => {
69+
window.location.href = `/games/${data.invite.gameId}`;
70+
}, 250);
6571

6672
dispatch(actions.updateInvite(data));
6773
});
@@ -85,57 +91,63 @@ export const initInvites = currentUserId => dispatch => {
8591
});
8692
};
8793

88-
channel
89-
.join()
90-
.receive('ok', onJoinSuccess);
94+
channel.join().receive('ok', onJoinSuccess);
9195
};
9296

9397
export const createInvite = params => dispatch => channel
94-
.push(channelMethods.invitesCreate, params)
95-
.receive('ok', data => {
96-
const message = getSystemMessage({ text: `You invite ${params.recipient_name} to battle. Wait for his reply` });
97-
dispatch(actions.newChatMessage(message));
98+
.push(channelMethods.invitesCreate, params)
99+
.receive('ok', data => {
100+
const message = getSystemMessage({
101+
text: `You invite ${data?.invite?.recipient?.name} to battle. Wait for his reply`,
102+
});
103+
dispatch(actions.newChatMessage(message));
98104

99-
dispatch(actions.addInvite(data));
100-
})
101-
.receive('error', ({ reason }) => {
102-
throw new Error(reason);
103-
});
105+
dispatch(actions.addInvite(data));
106+
})
107+
.receive('error', ({ reason }) => {
108+
throw new Error(reason);
109+
});
104110

105111
export const acceptInvite = id => dispatch => channel
106-
.push(channelMethods.invitesAccept, { id })
107-
.receive('ok', data => {
108-
setTimeout(() => { window.location.href = `/games/${data.invite.game_id}`; }, 250);
112+
.push(channelMethods.invitesAccept, { id })
113+
.receive('ok', data => {
114+
setTimeout(() => {
115+
window.location.href = `/games/${data.invite.gameId}`;
116+
}, 250);
109117

110-
dispatch(actions.updateInvite(data));
111-
})
112-
.receive('error', ({ reason }) => {
113-
dispatch(actions.updateInvite({ id, state: 'invalid' }));
114-
throw new Error(reason);
115-
});
118+
dispatch(actions.updateInvite(data));
119+
})
120+
.receive('error', ({ reason }) => {
121+
dispatch(actions.updateInvite({ id, state: 'invalid' }));
122+
throw new Error(reason);
123+
});
116124

117125
export const declineInvite = (id, opponentName) => dispatch => channel
118-
.push(channelMethods.invitesCancel, { id })
119-
.receive('ok', data => {
120-
const message = getSystemMessage({ text: `You decline battle invite [Opponent ${opponentName}]` });
121-
dispatch(actions.newChatMessage(message));
122-
123-
camelizeKeysAndDispatch(dispatch, actions.updateInvite)(data);
124-
})
125-
.receive('error', ({ reason }) => {
126-
dispatch(actions.updateInvite({ id, state: 'invalid' }));
127-
throw new Error(reason);
128-
});
126+
.push(channelMethods.invitesCancel, { id })
127+
.receive('ok', data => {
128+
const message = getSystemMessage({
129+
text: `You decline battle invite [Opponent ${opponentName}]`,
130+
});
131+
dispatch(actions.newChatMessage(message));
132+
133+
camelizeKeysAndDispatch(dispatch, actions.updateInvite)(data);
134+
})
135+
.receive('error', ({ reason }) => {
136+
dispatch(actions.updateInvite({ id, state: 'invalid' }));
137+
throw new Error(reason);
138+
});
129139

130140
export const cancelInvite = (id, opponentName) => dispatch => channel
131-
.push(channelMethods.invitesCancel, { id })
132-
.receive('ok', data => {
133-
const message = getSystemMessage({ text: `You cancel battle invite [Opponent ${opponentName}]` });
134-
dispatch(actions.newChatMessage(message));
135-
136-
camelizeKeysAndDispatch(dispatch, actions.updateInvite)(data);
137-
})
138-
.receive('error', ({ reason }) => {
139-
dispatch(actions.updateInvite({ id, state: 'invalid' }));
140-
throw new Error(reason);
141-
});
141+
.push(channelMethods.invitesCancel, { id })
142+
.receive('ok', data => {
143+
const message = getSystemMessage({
144+
text: `You cancel battle invite [Opponent ${opponentName}]`,
145+
});
146+
dispatch(actions.newChatMessage(message));
147+
148+
camelizeKeysAndDispatch(dispatch, actions.updateInvite)(data);
149+
})
150+
.receive('error', ({ reason }) => {
151+
dispatch(actions.updateInvite({ id, state: 'invalid' }));
152+
throw new Error(reason);
153+
});

0 commit comments

Comments
 (0)