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 { 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