Guest User

Untitled

a guest
May 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. //App.JS
  2. ....
  3. import configureStore from './src/store/configureStore';
  4. const store = configureStore();
  5. class App extends Component {
  6. render() {
  7. return (
  8. <Provider store={store}>
  9. <View style={{ flex: 1 }}>
  10. <StatusBar backgroundColor="#285576" />
  11. <MyForm />
  12. </View>
  13. </Provider>
  14. );
  15. }
  16. }
  17. export default App;
  18.  
  19. import { applyMiddleware, createStore } from 'redux';
  20. import createSagaMiddleware from 'redux-saga';
  21. import reducers from './../reducers';
  22. import rootSaga from './../sagas/rootSaga';
  23.  
  24. export default function configureStore() {
  25. const sagaMiddleware = createSagaMiddleware();
  26. const store = createStore(reducers, applyMiddleware(sagaMiddleware));
  27.  
  28. sagaMiddleware.run(rootSaga);
  29. return store;
  30. }
  31.  
  32. ...
  33. <TouchableOpacity style={{ backgroundColor: '#4368b2', borderColor: '#3464c8', borderRadius: 5 }}
  34. onPress={login(this.props.email,this.props.password)}> //call login action
  35. <Text style={commonStyle.buttonText}>{this.props.type}</Text>
  36. </TouchableOpacity>
  37. ...
  38.  
  39. export function login(email, password) {
  40. console.log('action-login');
  41. return {
  42. type: LOGIN_ACTION,
  43. username: email,
  44. password
  45. };
  46. }
  47.  
  48. import { all, fork } from 'redux-saga/effects';
  49. import { loginFlow } from './AuthSagas';
  50.  
  51. export default function* rootSaga() {
  52. yield fork(loginFlow);
  53. }
  54.  
  55. import { put, call, take } from 'redux-saga/effects';
  56. import { takeEvery } from 'redux-saga';
  57. import auth from './../auth';
  58. import { LOGIN_ACTION } from './../action/types';
  59. import { setLoginSuccess, setLoginError } from './../action';
  60.  
  61. function* authorize(credentials) {
  62. try {
  63. const token = yield call(auth.login(credentials));
  64. console.log(token);
  65. if (!token.error) {
  66. yield put(setLoginSuccess(token, credentials.username, credentials.password));
  67. return token;
  68. }
  69. yield put(setLoginError(token));
  70. return undefined;
  71. } catch (error) {
  72. console.log(error);
  73. return undefined;
  74. }
  75. }
  76.  
  77. export function* loginFlow() {
  78. console.log('saga-alert');
  79. const { username, password } = yield take(LOGIN_ACTION);
  80. yield call(authorize, { username, password });
  81. }
Add Comment
Please, Sign In to add comment