import React, { Component } from 'react'; import { Button, TextField, Typography, Paper, ButtonGroup } from '@material-ui/core'; import { JoinRoomApi, CreateRoomApi, PLAYER_STATUS, getPlayerSource, LocalData, updateLocalData, } from './Api'; import { goTo } from 'route-lite'; import Chat from './Chat'; import { ChevronRight, InfoRounded, ContactSupportRounded, MonetizationOnRounded, } from '@material-ui/icons'; const CONSTANTS = { USER_NAME_LENGTH: 12, NAME_LENGTH: 24, PIN_LENGTH: 9, }; export default class Create extends Component { constructor(props) { super(props); const defaultMode = LocalData().partyName ? 'join' : 'create'; this.state = { userName: LocalData().userName || '', name: LocalData().partyName || '', pin: LocalData().partyPin || '', mode: defaultMode, }; this.createCall = this.createCall.bind(this); this.joinCall = this.joinCall.bind(this); this.success = this.success.bind(this); } partyNameChange(value) { if (value.length <= CONSTANTS.NAME_LENGTH) this.setState({ name: value }); } partyPinChange(value) { if (value.length <= CONSTANTS.PIN_LENGTH) this.setState({ pin: value }); } userNameChange(value) { if (value.length <= CONSTANTS.USER_NAME_LENGTH) this.setState({ userName: value }); } createOrJoin() { this.props.updateLoading(true); const { userName, name, pin, mode } = this.state; if (!userName.replace(/\s+/g, '')) { this.props.notify('Missing user name'); this.props.updateLoading(false); return; } if (!name.replace(/\s+/g, '')) { this.props.notify('Missing party name'); this.props.updateLoading(false); return; } if (!pin.replace(/\s+/g, '')) { this.props.notify('Missing party pin'); this.props.updateLoading(false); return; } if (userName && name && pin) { switch (mode) { case 'create': this.createCall(userName, name, pin); break; case 'join': this.joinCall(userName, name, pin); break; default: this.props.updateLoading(false); break; } } else { this.props.notify('Double check your inputs'); } } createCall(userName, partyName, partyPin) { this.props.updateLoading(true); const apidata = { userName, partyName, partyPin, player: { source: getPlayerSource(), status: PLAYER_STATUS.PAUSED, time: 0, url: window.location.href, }, }; CreateRoomApi(apidata) .then((res) => res.json()) .then((createResult) => { if (createResult.status <= 299) { this.success(userName, partyName, partyPin); } else { this.props.updateLoading(false); this.props.notify(createResult.message || createResult.statusText); } }) .catch((err) => { console.error('Create Party API Error', err); this.props.notify(err.message || 'Something went wrong'); this.props.updateLoading(false); }); } joinCall(userName, partyName, partyPin) { const apidata = { userName, partyName, partyPin, source: getPlayerSource(), }; JoinRoomApi(apidata) .then((res) => res.json()) .then((joinResult) => { if (joinResult.status <= 299) { this.success(userName, partyName, partyPin); } else { this.props.updateLoading(false); this.props.notify(joinResult.message || joinResult.statusText); } }) .catch((err) => { console.error('Join Party API Error', err); this.props.notify(err.message || 'Something went wrong'); this.props.updateLoading(false); }); } success(userName, partyName, partyPin) { updateLocalData({ userName, partyName, partyPin }); this.props.updateLoading(false); goTo(Chat, this.props); } render() { const { mode, name, pin, userName } = this.state; return (