Created
January 24, 2016 14:34
-
-
Save jbaxleyiii/65580b815eb52809a4a7 to your computer and use it in GitHub Desktop.
Why sagas rock
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 "regenerator/runtime" | |
import { take, put, cps } from "redux-saga" | |
import { GraphQL } from "../../graphql" | |
import { auth } from "../../methods" | |
import { addSaga } from "../utilities" | |
import actions from "./actions" | |
// Check for availibilty of account | |
addSaga(function* checkAccount(getState) { | |
// setup this saga to always be listening for actions | |
while (true) { | |
// wait for the email field to be blurred | |
const { data } = yield take("ONBOARD.SET_DATA") | |
const { email } = data | |
// if the event was triggered by email check to see if it available | |
if (email) { | |
// make call to Rock to check if account is open | |
let isAvailable = yield cps(auth.available, email) | |
// end the run of this saga iteration by setting account | |
yield put(actions.setAccount(!isAvailable)) | |
} | |
} | |
}) | |
function* login(getState) { | |
const currentState = getState() | |
const { data, state } = currentState.onBoard | |
if (data.email && data.password) { | |
let { email, password } = data | |
// set the UI to show the loading screen | |
yield put(actions.loading()) | |
try { | |
// make the call to try and login | |
let isAuthorized = yield cps(auth.login, email, password) | |
// this should always be true shouldn't it? | |
if (isAuthorized) { | |
// return Meteor login to parent saga | |
const result = yield cps(Meteor.loginWithPassword, email, password) | |
return { result } | |
} | |
} catch (error) { | |
return { error } | |
} | |
} | |
} | |
function* signup(getState) { | |
const currentState = getState() | |
const { data, state } = currentState.onBoard | |
// shorthand for 80 ch limit | |
let d = data | |
if (d.email && d.password && d.firstName && d.lastName && d.terms) { | |
let { email, password } = data | |
// set the UI to show the loading screen | |
yield put(actions.loading()) | |
try { | |
// make the call to try and signup | |
let isAuthorized = yield cps(auth.signup, data) | |
// this should always be true shouldn't it? | |
if (isAuthorized) { | |
// return Meteor login to parent saga | |
const result = yield cps(Meteor.loginWithPassword, email, password) | |
return { result } | |
} | |
} catch (error) { | |
return { error } | |
} | |
} | |
} | |
// handle onboard wordflow | |
addSaga(function* onBoard(getState) { | |
// setup this saga to always be listening for actions | |
while (true) { | |
const { state } = yield take("ONBOARD.SET_STATE") | |
if (state === "submit") { | |
let currentState = getState(), | |
returnValue; | |
if (currentState.onBoard.account) { | |
returnValue = yield* login(getState) | |
} else { | |
returnValue = yield* signup(getState) | |
} | |
let { result, error } = returnValue | |
if (error) { | |
// add error to store | |
yield put(actions.error({ "password": error.error })) | |
// set not logged in status | |
yield put(actions.authorize(false)) | |
// fail the form | |
yield put(actions.fail()) | |
// reset the UI | |
yield put(actions.setState("default")) | |
} else { | |
// set the logged in status | |
yield put(actions.authorize(true)) | |
// succeed the form | |
yield put(actions.success()) | |
// reset the UI | |
yield put(actions.setState("default")) | |
} | |
} | |
} | |
}) |
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 onBoard from "../" | |
import { auth } from "../../../methods" | |
import login from "./login" | |
import signup from "./signup" | |
const onboard = store => next => action => { | |
const { dispatch, getState } = store | |
// restrict middleware to onboard actions | |
if (action.type.indexOf("ONBOARD") != 0) { | |
return next(action) | |
} | |
switch (action.type) { | |
case "ONBOARD.SET_STATE": | |
const state = getState() | |
if (action.state === "submit") { | |
if (state.onBoard.account) { | |
return login(store, next, action) | |
} | |
return signup(store, next, action) | |
} | |
return next(action) | |
case "ONBOARD.SET_DATA": | |
// don't hold everything up | |
next(action) | |
if (action.data.email) { | |
// set state based on is email is already in system | |
auth.available(action.data.email, (err, isAvailable) => { | |
dispatch(onBoard.setAccount(!isAvailable)) | |
}) | |
} | |
break | |
default: | |
return next(action) | |
} | |
} | |
export default onboard |
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
/*global Meteor */ | |
import onBoard from "../" | |
import { auth } from "../../../methods" | |
const login = (store, next, action) => { | |
const { dispatch, getState } = store | |
const state = getState() | |
const { data } = state.onBoard | |
// submitting form | |
if (state.onBoard.state === "default" && action.state === "submit" && data.email && data.password) { | |
// trigger loading screen | |
dispatch(onBoard.loading()) | |
auth.login(data.email, data.password, (err, authorized) => { | |
const failure = () => { | |
dispatch(onBoard.error({ | |
unauthorized: { | |
message: "Incorrect email and password combination" | |
} | |
})) | |
dispatch(onBoard.authorize(false)) | |
dispatch(onBoard.fail()) | |
} | |
if (authorized) { | |
Meteor.loginWithPassword(data.email, data.password, (err, response) => { | |
console.log(err, response) | |
dispatch(onBoard.success()) | |
dispatch(onBoard.authorize(true)) | |
action.state = "default" | |
return next(action) | |
}) | |
} else { | |
failure(err) | |
return next(action) | |
} | |
}) | |
return function cancel() { | |
// clearTimeout(timeoutId) | |
} | |
} | |
return next(action) | |
} | |
export default login |
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
/*global Meteor */ | |
import onBoard from "../" | |
import { auth } from "../../../methods" | |
const signup = (store, next, action) => { | |
const { dispatch, getState } = store | |
const state = getState() | |
const { data } = state.onBoard | |
// submitting form | |
if ( | |
state.onBoard.state === "default" && | |
action.state === "submit" && | |
data.email && | |
data.password && | |
data.firstName && | |
data.lastName && | |
data.terms | |
) { | |
// trigger loading screen | |
dispatch(onBoard.loading()) | |
const failure = () => { | |
dispatch(onBoard.error({ | |
unauthorized: { | |
message: "Incorrect email and password combination" | |
} | |
})) | |
dispatch(onBoard.authorize(false)) | |
dispatch(onBoard.fail()) | |
} | |
auth.signup(data, (err) => { | |
if (err) { failure(err); return } | |
Meteor.loginWithPassword(data.email, data.password, () => { | |
dispatch(onBoard.success()) | |
dispatch(onBoard.authorize(true)) | |
action.state = "default" | |
return next(action) | |
}) | |
}) | |
return function cancel() { | |
} | |
} | |
return next(action) | |
} | |
export default signup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment