Skip to content

Commit

Permalink
Send update or remove match event for match list (when user leaves)
Browse files Browse the repository at this point in the history
  • Loading branch information
arielger committed Apr 28, 2020
1 parent 04efae2 commit dc23d77
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
1 change: 0 additions & 1 deletion client/src/screens/Match/Match.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function Match({ history, user, match: urlMatch }) {
const matchId = urlMatch.params.matchId;

const [showCreatorLeft, setShowCreatorLeft] = React.useState(false);
const [isSubscriptionSet, setIsSubscriptionSet] = React.useState(false);

const {
loading,
Expand Down
3 changes: 2 additions & 1 deletion client/src/screens/Match/WaitingState/WaitingState.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default function WaitingState({
const [joinMatch] = useMutation(JOIN_MATCH, { variables: { matchId } });
const [leaveMatch] = useMutation(LEAVE_MATCH, { variables: { matchId } });

// @TODO: Fix React.useEffect, sometimes it is not executing leaveMatch
React.useEffect(() => {
return () => {
if (joinedMatch && !showCreatorLeft) {
Expand All @@ -88,7 +89,7 @@ export default function WaitingState({
return (
<>
<Prompt
when={!showCreatorLeft}
when={joinedMatch && !showCreatorLeft}
message={`Estas seguro que deseas abandonar la partida?`}
/>
<CreatorLeftModal isVisible={showCreatorLeft} history={history} />
Expand Down
6 changes: 3 additions & 3 deletions client/src/screens/Matches/Matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ const handleMatchUpdates = (
) => {
const { type, ...matchData } = matchListUpdated;
switch (type) {
case "NEW_MATCH": {
case "MATCH_ADDED": {
return {
...prev,
matches: [...prev.matches, matchData],
};
}
case "UPDATED_MATCH": {
case "MATCH_UPDATED": {
return {
...prev,
matches: prev.matches.map((match) =>
matchData.id === match.id ? matchData : match
),
};
}
case "DELETED_MATCH": {
case "MATCH_REMOVED": {
return {
...prev,
matches: R.reject(R.propEq("id", matchData.id))(prev.matches),
Expand Down
27 changes: 20 additions & 7 deletions server/src/datasources/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class MatchAPI extends DataSource {

pubsub.publish(events.MATCH_ADDED, {
matchListUpdated: {
type: "NEW_MATCH",
type: events.MATCH_ADDED,
...newMatchData,
},
});
Expand Down Expand Up @@ -287,10 +287,13 @@ class MatchAPI extends DataSource {
});
}

const event = startGame ? events.MATCH_REMOVED : events.MATCH_UPDATED;

// If the match is full remove it from the list of matches
pubsub.publish(startGame ? events.MATCH_REMOVED : events.MATCH_UPDATED, {
pubsub.publish(event, {
matchListUpdated: {
type: startGame ? "DELETED_MATCH" : "UPDATED_MATCH",
// @TODO: There is no need to send the whole match if it's removed from list
type: event,
...updatedMatch,
},
});
Expand Down Expand Up @@ -342,21 +345,31 @@ class MatchAPI extends DataSource {
R.reject(R.equals(userId))
)(updatedMatch.players || []);

const event = isCreator
const matchDetailEvent = isCreator
? events.CREATOR_LEFT_GAME
: events.PLAYER_LEFT_GAME;

otherPlayersInGame.forEach((playerId) => {
pubsub.publish(event, {
pubsub.publish(matchDetailEvent, {
userId: playerId,
matchUpdated: {
...updatedMatch,
type: event,
type: matchDetailEvent,
},
});
});

// @TODO: If creator remove match from matches list
const matchListEvent = isCreator
? events.MATCH_REMOVED
: events.MATCH_UPDATED;

pubsub.publish(matchListEvent, {
matchListUpdated: {
// @TODO: There is no need to send the whole match if it's removed from list
type: matchListEvent,
...updatedMatch,
},
});

return {
success: true,
Expand Down
6 changes: 3 additions & 3 deletions server/src/schema/matches.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ type PlayerMatch {
}

enum MatchListUpdateType {
NEW_MATCH
UPDATED_MATCH
DELETED_MATCH
MATCH_ADDED
MATCH_UPDATED
MATCH_REMOVED
}

type MatchListUpdate {
Expand Down
1 change: 1 addition & 0 deletions server/src/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { PubSub } = require("apollo-server");
module.exports = {
pubsub: new PubSub(),
events: {
// Matches list
MATCH_ADDED: "MATCH_ADDED",
MATCH_UPDATED: "MATCH_UPDATED",
MATCH_REMOVED: "MATCH_REMOVED",
Expand Down

0 comments on commit dc23d77

Please sign in to comment.