Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** ReactotronConfig.js **/
  2. import Reactotron from 'reactotron-react-native';
  3. import sagaPlugin from 'reactotron-redux-saga';
  4.  
  5. if (__DEV__) {
  6.   const tron = Reactotron.configure({
  7.     host: '192.168.25.6',
  8.   }) // controls connection & communication settings
  9.     .useReactNative() // add all built-in react native plugins
  10.     .use(sagaPlugin())
  11.     .connect(); // let's connect!
  12.  
  13.   tron.clear();
  14.  
  15.   console.tron = tron;
  16. }
  17.  
  18.  
  19.  
  20. /** store.js  **/
  21. import { createStore, applyMiddleware, compose } from 'redux';
  22. import createSagaMiddleware from 'redux-saga';
  23.  
  24. import rootSaga from './sagas';
  25. import reducers from './ducks';
  26.  
  27. const middleware = [];
  28. const enhancers = [];
  29.  
  30. /* Saga */
  31. const sagaMonitor = __DEV__ ? console.tron.createSagaMonitor() : null;
  32. const sagaMiddleware = createSagaMiddleware({ sagaMonitor });
  33. middleware.push(sagaMiddleware);
  34.  
  35. enhancers.push(applyMiddleware(...middleware));
  36.  
  37. /* Store */
  38. const createAppropriateStore = __DEV__ ? console.tron.createStore : createStore;
  39. const store = createAppropriateStore(reducers, compose(...enhancers));
  40.  
  41. /* Run Saga */
  42. sagaMiddleware.run(rootSaga);
  43.  
  44. export default store;
  45.  
  46.  
  47.  
  48. /** ducks/auth.js  **/
  49. /* Action Types */
  50. export const Types = {
  51.   LOGIN: 'auth/LOGIN',
  52.   LOGOUT: 'auth/LOGOUT',
  53.   SUCCESS: 'auth/SUCCESS',
  54.   ERROR: 'auth/ERROR',
  55. };
  56.  
  57. /* Reducers */
  58. const INITIAL_STATE = {
  59.   username: '',
  60.   password: '',
  61.   requesting: false,
  62.   error: false,
  63.   msgError: '',
  64. };
  65.  
  66. export default function auth(state = INITIAL_STATE, action) {
  67.   switch (action.type) {
  68.     case Types.SUCCESS:
  69.       return [...state, action.payload.data];
  70.     default:
  71.       return state;
  72.   }
  73. }
  74.  
  75. /* Action Creators */
  76. export function authenticate(username, password) {
  77.   return {
  78.     type: Types.LOGIN,
  79.     payload: {
  80.       username,
  81.       password,
  82.     },
  83.   };
  84. }
  85.  
  86.  
  87. /** ducks/index.js **/
  88. import { combineReducers } from 'redux';
  89. import auth from './auth';
  90.  
  91. export default combineReducers({ auth });
  92.  
  93.  
  94.  
  95.  
  96. /** sagas/auth.js **/
  97. import api from 'services/api';
  98. import { call, put } from 'redux-saga/effects';
  99. import { Types } from 'redux/ducks/auth';
  100.  
  101. export function* authenticateUser(action) {
  102.   const { username, password } = action.payload;
  103.   const response = yield call(
  104.     api.post,
  105.     {},
  106.     { headers: { Authorization: `Basic ${btoa(`${username}:${password}`)}` } },
  107.   );
  108.  
  109.   yield put({ type: Types.SUCCESS, payload: { data: response.data } });
  110. }
  111.  
  112.  
  113. /** sagas/index.js  **/
  114. import { takeLatest } from 'redux-saga/effects';
  115.  
  116. import { authenticateUser } from 'redux/sagas/auth';
  117.  
  118. import { Types as AuthTypes } from 'redux/ducks/auth';
  119.  
  120. export default function* root() {
  121.   yield [takeLatest(AuthTypes.LOGIN, authenticateUser)];
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement