|
1 | 1 | import { call, put, takeEvery } from 'redux-saga/effects';
|
2 | 2 | import API from '@utils/api';
|
3 |
| -import { INIT_CHANNELS, INIT_CHANNELS_ASYNC } from '../types/channel-types'; |
| 3 | +import { joinChatroom } from '@socket/emits/chatroom'; |
| 4 | +import { |
| 5 | + INIT_CHANNELS, |
| 6 | + INIT_CHANNELS_ASYNC, |
| 7 | + JOIN_CHANNEL, |
| 8 | + JOIN_CHANNEL_ASYNC, |
| 9 | + LOAD_NEXT_CHANNELS, |
| 10 | + LOAD_NEXT_CHANNELS_ASYNC |
| 11 | +} from '../types/channel-types'; |
| 12 | +import { ADD_CHANNEL } from '../types/chatroom-types'; |
4 | 13 |
|
5 | 14 | function* initChannelsSaga() {
|
6 | 15 | try {
|
7 |
| - const channelCount = 0; |
8 |
| - const channels = yield call(API.getChannels); |
| 16 | + const { channels, channelCount } = yield call(API.getChannels); |
9 | 17 | yield put({ type: INIT_CHANNELS, payload: { channelCount, channels } });
|
10 | 18 | } catch (e) {
|
11 | 19 | console.log(e);
|
12 | 20 | }
|
13 | 21 | }
|
14 | 22 |
|
| 23 | +function* loadNextChannels(action: any) { |
| 24 | + try { |
| 25 | + const { title } = action.payload; |
| 26 | + const nextChannels = yield call(API.getNextChannels, title); |
| 27 | + yield put({ type: LOAD_NEXT_CHANNELS, payload: { channels: nextChannels } }); |
| 28 | + } catch (e) { |
| 29 | + console.log(e); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function* joinChannel(action: any) { |
| 34 | + try { |
| 35 | + const { chatroomId } = action.payload; |
| 36 | + yield call(API.joinChannel, chatroomId); |
| 37 | + yield put({ type: JOIN_CHANNEL, payload: { chatroomId } }); |
| 38 | + const chatroom = yield call(API.getChatroom, chatroomId); |
| 39 | + const { chatType, isPrivate, title } = chatroom; |
| 40 | + const payload = { chatroomId, chatType, isPrivate, title }; |
| 41 | + joinChatroom(chatroomId); |
| 42 | + yield put({ type: ADD_CHANNEL, payload }); |
| 43 | + } catch (e) { |
| 44 | + console.log(e); |
| 45 | + } |
| 46 | +} |
| 47 | + |
15 | 48 | export function* channelSaga() {
|
16 | 49 | yield takeEvery(INIT_CHANNELS_ASYNC, initChannelsSaga);
|
| 50 | + yield takeEvery(LOAD_NEXT_CHANNELS_ASYNC, loadNextChannels); |
| 51 | + yield takeEvery(JOIN_CHANNEL_ASYNC, joinChannel); |
17 | 52 | }
|
0 commit comments