Skip to content

Instantly share code, notes, and snippets.

@hedgerh
Last active April 29, 2019 22:30
Show Gist options
  • Save hedgerh/40a7a4ea3ce873bfb98fa0a7149f0a8c to your computer and use it in GitHub Desktop.
Save hedgerh/40a7a4ea3ce873bfb98fa0a7149f0a8c to your computer and use it in GitHub Desktop.
import { call, put, takeLatest } from 'redux-saga/effects'
import client from '../httpClient'
const createAction = (type) => (payload) => ({
type,
payload,
})
const http = {
login: (credentials) => client.post('/login', credentials),
}
const LOGIN = 'LOGIN'
const LOGIN_SUCCESS = 'LOGIN_SUCCESS'
const LOGIN_ERROR = 'LOGIN_ERROR'
const actions = {
login: createAction(LOGIN),
loginSuccess: createAction(LOGIN_SUCCESS),
loginError: createAction(LOGIN_ERROR),
}
function* loginSaga(action) {
try {
const user = yield call(http.login, action.payload)
yield put(actions.loginSuccess(user))
} catch (e) {
yield put(actions.loginError('Uh oh'))
}
}
export function* authSagas() {
yield takeLatest(LOGIN, loginSaga)
}
const initialState = {
isLoading: false,
}
export default function reducer(state, action) {
switch (action.type) {
case LOGIN:
return { ...state, isLoading: true }
case LOGIN_SUCCESS:
return { ...state, user: action.payload }
case LOGIN_ERROR:
return { ...state, isLoading: false }
}
return state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment