diff --git a/src/components/draft/Draft.tsx b/src/components/draft/Draft.tsx index 1d57369..00d3080 100644 --- a/src/components/draft/Draft.tsx +++ b/src/components/draft/Draft.tsx @@ -84,18 +84,24 @@ class Draft extends React.Component { this.setState({joined: true}); } } else if (this.props.whoAmI !== Player.SPEC && !this.state.joined) { - let username: string | null = NameGenerator.getNameFromLocalStorage(this.props.ownName); - console.log("componentDidMount", this.props.triggerSetRole, username); - if (username !== null) { - this.props.triggerSetRole(username, this.props.whoAmI); - this.setState({joined: true}); - } else { - this.props.showNameModal(); - } + this.updateRole(this.props.whoAmI); + } else if (prevProps.whoAmI !== undefined && prevProps.whoAmI !== this.props.whoAmI && !this.state.joined) { + // Role was already set before we connected, must have been set via direct link, inform the server + this.updateRole(prevProps.whoAmI); } this.updateTitle(); } + private updateRole(player: Player) { + let username: string | null = NameGenerator.getNameFromLocalStorage(this.props.ownName); + if (username !== null) { + this.props.triggerSetRole(username, player); + this.setState({joined: true}); + } else { + this.props.showNameModal(); + } + } + private updateTitle() { const title = this.getTitle(); if (document.title !== title) { diff --git a/src/components/draft/GuestDraft.tsx b/src/components/draft/GuestDraft.tsx new file mode 100644 index 0000000..4328346 --- /dev/null +++ b/src/components/draft/GuestDraft.tsx @@ -0,0 +1,27 @@ +import * as React from "react"; +import Player from "../../constants/Player"; +import "../../types/DraftEvent"; +import {WithTranslation, withTranslation} from "react-i18next"; +import {Redirect} from "react-router"; +import {Util} from "../../util/Util"; + +interface IProps extends WithTranslation { + setOwnRole: (role: Player) => void; +} + + +class GuestDraft extends React.Component { + + componentDidMount(): void { + this.props.setOwnRole(Player.GUEST); + } + + public render() { + const draftId = Util.getIdFromUrl(); + return ( + + ); + } +} + +export default withTranslation()(GuestDraft); diff --git a/src/components/draft/HostDraft.tsx b/src/components/draft/HostDraft.tsx new file mode 100644 index 0000000..9988905 --- /dev/null +++ b/src/components/draft/HostDraft.tsx @@ -0,0 +1,27 @@ +import * as React from "react"; +import Player from "../../constants/Player"; +import "../../types/DraftEvent"; +import {WithTranslation, withTranslation} from "react-i18next"; +import {Redirect} from "react-router"; +import {Util} from "../../util/Util"; + +interface IProps extends WithTranslation { + setOwnRole: (role: Player) => void; +} + + +class HostDraft extends React.Component { + + componentDidMount(): void { + this.props.setOwnRole(Player.HOST); + } + + public render() { + const draftId = Util.getIdFromUrl(); + return ( + + ); + } +} + +export default withTranslation()(HostDraft); diff --git a/src/components/draft/SpectateDraft.tsx b/src/components/draft/SpectateDraft.tsx index 1df5448..6df2f0d 100644 --- a/src/components/draft/SpectateDraft.tsx +++ b/src/components/draft/SpectateDraft.tsx @@ -24,4 +24,4 @@ class SpectateDraft extends React.Component { } } -export default withTranslation()(SpectateDraft); \ No newline at end of file +export default withTranslation()(SpectateDraft); diff --git a/src/containers/GuestDraft.tsx b/src/containers/GuestDraft.tsx new file mode 100644 index 0000000..0dd7afa --- /dev/null +++ b/src/containers/GuestDraft.tsx @@ -0,0 +1,20 @@ +import * as actions from '../actions/'; +import {ApplicationState} from '../types'; +import {connect} from 'react-redux'; +import {Dispatch} from 'redux'; +import Player from "../constants/Player"; +import DraftGuest from "../components/draft/GuestDraft"; + +export function mapStateToProps(state: ApplicationState) { + return {} +} + +export function mapDispatchToProps(dispatch: Dispatch) { + return { + setOwnRole: (role: Player) => dispatch(actions.setOwnRole(role)), + triggerConnect: () => dispatch(actions.connect()), + triggerSetRole: (name: string, role: Player) => dispatch(actions.setRole(name, role)), + } +} + +export default connect(mapStateToProps, mapDispatchToProps)(DraftGuest); diff --git a/src/containers/HostDraft.tsx b/src/containers/HostDraft.tsx new file mode 100644 index 0000000..12b3677 --- /dev/null +++ b/src/containers/HostDraft.tsx @@ -0,0 +1,20 @@ +import * as actions from '../actions/'; +import {ApplicationState} from '../types'; +import {connect} from 'react-redux'; +import {Dispatch} from 'redux'; +import Player from "../constants/Player"; +import DraftHost from "../components/draft/HostDraft"; + +export function mapStateToProps(state: ApplicationState) { + return {} +} + +export function mapDispatchToProps(dispatch: Dispatch) { + return { + setOwnRole: (role: Player) => dispatch(actions.setOwnRole(role)), + triggerConnect: () => dispatch(actions.connect()), + triggerSetRole: (name: string, role: Player) => dispatch(actions.setRole(name, role)), + } +} + +export default connect(mapStateToProps, mapDispatchToProps)(DraftHost); diff --git a/src/index.tsx b/src/index.tsx index 772d69f..8ddd1c4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -22,6 +22,8 @@ import {initialLanguageState} from "./reducers/language"; import {initialModalState} from "./reducers/modal"; import {initialPresetEditorState} from "./reducers/presetEditor"; import SpectateDraft from "./containers/SpectateDraft"; +import HostDraft from "./containers/HostDraft"; +import GuestDraft from "./containers/GuestDraft"; import {initialColorSchemeState} from "./reducers/colorScheme"; import {initialReplayState} from "./reducers/replay"; import {initialIconStyleState} from "./reducers/iconStyle"; @@ -158,6 +160,8 @@ ReactDOM.render( + + diff --git a/src/util/Util.ts b/src/util/Util.ts index 281e158..57edd3b 100644 --- a/src/util/Util.ts +++ b/src/util/Util.ts @@ -29,7 +29,7 @@ export const Util = { }, getIdFromUrl(): string { - const match: RegExpMatchArray | null = window.location.pathname.match(/\/(?:draft|spectate)\/([A-Za-z]+)\/?.*/); + const match: RegExpMatchArray | null = window.location.pathname.match(/\/(?:draft|spectate|guest|host)\/([A-Za-z]+)\/?.*/); if (match !== null) { return match[1]; }