Skip to content

Commit

Permalink
Deserialize newParticipant on the front end
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasnaphas committed Mar 28, 2022
1 parent 4a5238e commit ee72295
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions frontend/src/components/RosterPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,24 @@ class RosterPage extends Component {
`gamename=${encodeURIComponent(gameName)}`
);
webSocket.addEventListener("message", (event) => {
if (event.data.newParticipant) {
this.setState((state, props) => {
return {
participants: state.participants.concat([
event.data.newParticipant,
]),
};
});
if (!event) {
return;
}
if (!event.data) {
return;
}
const eventData = JSON.parse(event.data);
if (!eventData) {
return;
}
if (!eventData.newParticipant) {
return;
}
this.setState((state, props) => {
return {
participants: state.participants.concat([eventData.newParticipant]),
};
});
});
}
componentWillUnmount() {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/RosterPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe("RosterPage", () => {
);
expect(mockWebSocketConstructorCalls).toEqual(1);
const testMessage = new MessageEvent("message", {
data: { newParticipant: "Em Three" },
data: `{"newParticipant":"Em Three"}`,
});
let table = await screen.findByRole("table");
await findByText(table, "Je Teste");
Expand Down

0 comments on commit ee72295

Please sign in to comment.