summaryrefslogtreecommitdiff
path: root/sw-ui/src/sagas/home.ts
blob: 585c536e39dcba68b9ad6cea4ce26d8227bb4c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { push } from 'react-router-redux';
import { eventChannel, SagaIterator } from 'redux-saga';
import { all, apply, call, put, take } from 'redux-saga/effects';
import { ApiPlayer } from '../api/model';
import { SevenWondersSession } from '../api/sevenWondersApi';
import { actions, REQUEST_CHOOSE_USERNAME } from '../redux/actions/user';

function* sendUsername(session: SevenWondersSession): SagaIterator {
  while (true) {
    const { username } = yield take(REQUEST_CHOOSE_USERNAME);
    // $FlowFixMe
    yield apply(session, session.chooseName, [username]);
  }
}

function* validateUsername(session: SevenWondersSession): any {
  const usernameChannel = yield eventChannel(session.watchNameChoice());
  while (true) {
    const user: ApiPlayer = yield take(usernameChannel);
    yield put(actions.setCurrentPlayer(user));
    yield apply(usernameChannel, usernameChannel.close);
    yield put(push('/games'));
  }
}

export function* homeSaga(session: SevenWondersSession): SagaIterator {
  yield all([call(sendUsername, session), call(validateUsername, session)]);
}
bgstack15