Last active
July 16, 2021 07:39
-
-
Save mrkiura/da8d4c7e1274c62fbee253d73621a219 to your computer and use it in GitHub Desktop.
SignupForm component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { useHistory } from 'react-router-dom'; | |
import { useGlobalState } from '../RoomContextProvider'; | |
import { Device } from '@twilio/voice-sdk'; | |
const SignupForm = ({onSubmit}) => { | |
const history = useHistory() | |
const [state, setState] = useGlobalState(); | |
const handleSubmit = e => { | |
e.preventDefault(); | |
const nickname = state.nickname; | |
setupTwilio(nickname); | |
history.push('/rooms') | |
}; | |
const updateNickname = (nickname) => { | |
setState({...state, nickname}) | |
} | |
const setupTwilio = (nickname) => { | |
fetch(`/api/token/${nickname}`) | |
.then(response => response.json()) | |
.then(data => { | |
// setup device | |
const twilioToken = data.token; | |
const device = new Device(twilioToken); | |
device.updateOptions(twilioToken, { | |
codecPreferences: ['opus', 'pcmu'], | |
fakeLocalDTMF: true, | |
maxAverageBitrate: 16000 | |
}); | |
device.on('error', (device) => { | |
console.log("error: ", device) | |
}); | |
setState((state) => { | |
return {...state, device, twilioToken} | |
}); | |
}) | |
.catch((error) => { | |
console.log(error) | |
}) | |
}; | |
return ( | |
<form onSubmit={handleSubmit}> | |
<input | |
placeholder="Enter nickname" | |
onChange={ e => updateNickname(e.target.value)} | |
/> | |
<input type="submit" value="Start chatting" /> | |
</form> | |
); | |
}; | |
export default SignupForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment