Skip to content

Commit bc8b2ae

Browse files
committed
Add url parameters to join as HOST/GUEST automatically if possible
1 parent f500720 commit bc8b2ae

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/components/draft/RoleModal.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import * as React from "react";
22
import {Trans, WithTranslation, withTranslation} from "react-i18next";
33
import Player from "../../constants/Player";
44
import CopyableInput from "./CopyableInput";
5+
import {RouteComponentProps} from "react-router";
6+
import {withRouter} from "react-router-dom";
57

6-
interface IProps extends WithTranslation {
8+
interface IProps extends WithTranslation, RouteComponentProps<any> {
79
visible: boolean;
810
setRoleCallback: (role: Player) => void;
911
name: string | null;
@@ -13,6 +15,29 @@ interface IProps extends WithTranslation {
1315

1416
class RoleModal extends React.Component<IProps, object> {
1517

18+
componentDidUpdate(prevProps: Readonly<IProps>, prevState: Readonly<object>, snapshot?: any) {
19+
if (this.props.visible) {
20+
let query = new URLSearchParams(this.props.location.search);
21+
const asHost = query.get('as') === 'host' || false;
22+
const asGuest = query.get('as') === 'guest' || false;
23+
if (!this.props.hostConnected && asHost) {
24+
this.deleteAsFromParams(query);
25+
this.props.setRoleCallback(Player.HOST);
26+
}
27+
if (!this.props.guestConnected && asGuest) {
28+
this.deleteAsFromParams(query);
29+
this.props.setRoleCallback(Player.GUEST);
30+
}
31+
}
32+
}
33+
34+
private deleteAsFromParams(query: URLSearchParams) {
35+
query.delete('as');
36+
this.props.history.replace({
37+
search: query.toString()
38+
});
39+
}
40+
1641
public render() {
1742
if (this.props.visible) {
1843
return (
@@ -87,4 +112,4 @@ class RoleModal extends React.Component<IProps, object> {
87112
}
88113
}
89114

90-
export default withTranslation()(RoleModal);
115+
export default withTranslation()(withRouter(RoleModal));

0 commit comments

Comments
 (0)