Skip to content

Instantly share code, notes, and snippets.

View OleksivO's full-sized avatar

Oleksandr Oleksiv OleksivO

View GitHub Profile
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
function* generatorFunction() {
yield 'First value';
yield 'Second value';
return 'Third value';
}
const iteratorObject = generatorFunction();
console.log(iteratorObject.next()) //{value: "First value", done: false}
console.log(iteratorObject.next()) //{value: "Second value", done: false}
console.log(iteratorObject.next()) // {value: "Third value", done: true}
console.log(iteratorObject.next()) // {value: "undefined, done: true}
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
try {
const successResponse = yield fetch('url');
const parsedResponse = yield successResponse.json();
console.log(successResponse);
} catch(error) {
console.log(error)
}
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
const INITIAL_STATE = {
token: null,
userId: null,
error: null,
loading: false,
};
// ...
const reducer = (state = INITIAL_STATE, action) => {
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
import axios from 'axios';
export const authStart = () => {
return {
type: AUTH_START
}
};
export const authSuccess = (token, userId) => {
return {
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
export const auth = (email, password, signin) => {
return {
type: 'AUTH_USER',
email: email,
password: password,
signin: signin
}
};
export const authStart = () => {
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
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);
@OleksivO
OleksivO / post.js
Last active May 5, 2020 09:18
the description for this gist
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());
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:00
the description for this gist
// same logic implemented with setTimeout()
export const checkAuthTimeout = (expirationTime) => {
return dispatch => {
setTimeout(() => {
dispatch(logout())
}, expirationTime * 1000)
}
};
@OleksivO
OleksivO / post.js
Created January 16, 2019 20:01
the description for this gist
//...
const rootReducer = combineReducers({
// ...
});
const sagaMiddleware = createSagaMiddleware();
// we apply saga as a simple middleware (Redux-Thunk for example)
const store = createStore(rootReducer, composeEnhancers(
applyMiddleware(sagaMiddleware)
// 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;