This file contains 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 {delay} from 'redux-saga'; | |
import {put, call, takeEvery} from 'redux-saga/effects'; | |
import axios from "axios"; | |
import * as actions from '../actions/authorization' | |
export default function* watchAuthSaga() {...} | |
function* authUser(action) { | |
yield put(actions.authStart()); |
This file contains 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
// Action and types | |
export const FETCH_ITEMS_START = 'FETCH_ITEMS_START'; | |
export const FETCH_ITEMS_SUCCESS = 'FETCH_ITEMS_SUCCESS'; | |
export const fetchItemsSuccess = (items) => { | |
return { | |
type: actionTypes.FETCH_ITEMS_SUCCESS, | |
payload: items | |
} | |
}; |
This file contains 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
// Actin types & Actions | |
export const GET_ITEMS = 'GET_ITEMS'; | |
export const SET_ITEMS = 'SET_ITEMS'; | |
export class GetItems implements Action { | |
readonly type = GET_ITEMS; | |
} | |
export class SetItems implements Action { | |
readonly type = SET_ITEMS; |
This file contains 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
//... | |
const rootReducer = combineReducers({ | |
// ... | |
}); | |
const sagaMiddleware = createSagaMiddleware(); | |
// we apply saga as a simple middleware (Redux-Thunk for example) | |
const store = createStore(rootReducer, composeEnhancers( | |
applyMiddleware(sagaMiddleware) |
This file contains 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
// same logic implemented with setTimeout() | |
export const checkAuthTimeout = (expirationTime) => { | |
return dispatch => { | |
setTimeout(() => { | |
dispatch(logout()) | |
}, expirationTime * 1000) | |
} | |
}; |
This file contains 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 {delay} from 'redux-saga'; | |
import {put, call, takeEvery} from 'redux-saga/effects'; | |
import axios from "axios"; | |
import * as actions from '../actions/authorization' | |
export default function* watchAuthSaga() { | |
yield takeEvery('AUTH_USER', authUser); | |
yield takeEvery('AUTH_INITIATE_LOGOUT', logout); | |
yield takeEvery('AUTH_CHECK_TIMEOUT', checkAuthTimeout); |
This file contains 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
export const auth = (email, password, signin) => { | |
return { | |
type: 'AUTH_USER', | |
email: email, | |
password: password, | |
signin: signin | |
} | |
}; | |
export const authStart = () => { |
This file contains 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 axios from 'axios'; | |
export const authStart = () => { | |
return { | |
type: AUTH_START | |
} | |
}; | |
export const authSuccess = (token, userId) => { | |
return { |
This file contains 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
const INITIAL_STATE = { | |
token: null, | |
userId: null, | |
error: null, | |
loading: false, | |
}; | |
// ... | |
const reducer = (state = INITIAL_STATE, action) => { |
This file contains 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
try { | |
const successResponse = yield fetch('url'); | |
const parsedResponse = yield successResponse.json(); | |
console.log(successResponse); | |
} catch(error) { | |
console.log(error) | |
} |
NewerOlder