Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** ReactotronConfig.js **/
- import Reactotron from 'reactotron-react-native';
- import sagaPlugin from 'reactotron-redux-saga';
- if (__DEV__) {
- const tron = Reactotron.configure({
- host: '192.168.25.6',
- }) // controls connection & communication settings
- .useReactNative() // add all built-in react native plugins
- .use(sagaPlugin())
- .connect(); // let's connect!
- tron.clear();
- console.tron = tron;
- }
- /** store.js **/
- import { createStore, applyMiddleware, compose } from 'redux';
- import createSagaMiddleware from 'redux-saga';
- import rootSaga from './sagas';
- import reducers from './ducks';
- const middleware = [];
- const enhancers = [];
- /* Saga */
- const sagaMonitor = __DEV__ ? console.tron.createSagaMonitor() : null;
- const sagaMiddleware = createSagaMiddleware({ sagaMonitor });
- middleware.push(sagaMiddleware);
- enhancers.push(applyMiddleware(...middleware));
- /* Store */
- const createAppropriateStore = __DEV__ ? console.tron.createStore : createStore;
- const store = createAppropriateStore(reducers, compose(...enhancers));
- /* Run Saga */
- sagaMiddleware.run(rootSaga);
- export default store;
- /** ducks/auth.js **/
- /* Action Types */
- export const Types = {
- LOGIN: 'auth/LOGIN',
- LOGOUT: 'auth/LOGOUT',
- SUCCESS: 'auth/SUCCESS',
- ERROR: 'auth/ERROR',
- };
- /* Reducers */
- const INITIAL_STATE = {
- username: '',
- password: '',
- requesting: false,
- error: false,
- msgError: '',
- };
- export default function auth(state = INITIAL_STATE, action) {
- switch (action.type) {
- case Types.SUCCESS:
- return [...state, action.payload.data];
- default:
- return state;
- }
- }
- /* Action Creators */
- export function authenticate(username, password) {
- return {
- type: Types.LOGIN,
- payload: {
- username,
- password,
- },
- };
- }
- /** ducks/index.js **/
- import { combineReducers } from 'redux';
- import auth from './auth';
- export default combineReducers({ auth });
- /** sagas/auth.js **/
- import api from 'services/api';
- import { call, put } from 'redux-saga/effects';
- import { Types } from 'redux/ducks/auth';
- export function* authenticateUser(action) {
- const { username, password } = action.payload;
- const response = yield call(
- api.post,
- {},
- { headers: { Authorization: `Basic ${btoa(`${username}:${password}`)}` } },
- );
- yield put({ type: Types.SUCCESS, payload: { data: response.data } });
- }
- /** sagas/index.js **/
- import { takeLatest } from 'redux-saga/effects';
- import { authenticateUser } from 'redux/sagas/auth';
- import { Types as AuthTypes } from 'redux/ducks/auth';
- export default function* root() {
- yield [takeLatest(AuthTypes.LOGIN, authenticateUser)];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement