Last active
March 29, 2018 20:08
-
-
Save agenthunt/15135c9efbabe347add5ae603d9d5cb3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { take, call, put, all, fork, race, select } from 'redux-saga/effects'; | |
import { createStar } from 'redux-nakshatra'; | |
import axios from 'axios'; | |
export const { rootSaga, types, actions, rootReducer } = createStar({ | |
name: 'users', | |
http: { | |
url: 'http://localhost:5000/users', | |
generateDefault: true | |
}, | |
custom: { | |
getUsersAndFriends: { | |
saga: function* watchGetUsersAndFriendsRequestSaga(dispatch) { | |
while (true) { | |
const request = yield take(types.getUsersAndFriends_REQUEST); | |
try { | |
yield put({ | |
type: types.getUsers_REQUEST | |
}); | |
yield race([types.getUsers_SUCCESS, types.getUsers_FAILURE]); | |
const getUsers = yield select(state => state.getUsers); | |
const userIds = getUsers.data.map(o => o.id); | |
const getFriendsForUsers = yield call(() => | |
axios({ | |
url: 'http://localhost:5000/friends', | |
data: { | |
userIds | |
} | |
}) | |
); | |
const result = getUsers.data.map((user, index) => { | |
return { | |
...user, | |
friend: getFriendsForUsers.data[index] | |
}; | |
}); | |
yield put({ | |
type: types.getUsersAndFriends_SUCCESS, | |
response: result | |
}); | |
} catch (error) { | |
yield put({ | |
type: types.getUsersAndFriends_FAILURE, | |
response: error | |
}); | |
} | |
} | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment