diff options
Diffstat (limited to 'frontend/src/sagas/home.js')
-rw-r--r-- | frontend/src/sagas/home.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/frontend/src/sagas/home.js b/frontend/src/sagas/home.js new file mode 100644 index 00000000..99e6f954 --- /dev/null +++ b/frontend/src/sagas/home.js @@ -0,0 +1,29 @@ +import { apply, call, put, take } from 'redux-saga/effects' +import { createSubscriptionChannel } from '../utils/websocket' +import { push } from 'react-router-redux' + +import { actions, types } from '../redux/players' + +function *sendUsername({ socket }) { + const {username} = yield take(types.REQUEST_CHOOSE_USERNAME) + + yield apply(socket, socket.send, ['/app/chooseName', JSON.stringify({ playerName: username })]) +} + +function *validateUsername({ socket }) { + const usernameChannel = yield call(createSubscriptionChannel, socket, '/user/queue/nameChoice') + const user = yield take(usernameChannel) + yield put(actions.setCurrentPlayer(user)) + yield apply(usernameChannel, usernameChannel.close) + yield put(push('/games')) +} + +function *usernameChoiceSaga(wsConnection) { + // TODO: Run sendUsername in loop when we have the ability to cancel saga on route change + yield [ + call(sendUsername, wsConnection), + call(validateUsername, wsConnection), + ] +} + +export default usernameChoiceSaga |