Skip to content

Commit

Permalink
Sign Up Saga
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Aug 22, 2019
1 parent 2fd05cd commit e41cfc7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
30 changes: 12 additions & 18 deletions src/components/sign-up/sign-up.component.jsx
@@ -1,9 +1,10 @@
import React from "react"; import React from "react";
import { connect } from "react-redux";


import FormInput from "../form-input/form-input.components"; import FormInput from "../form-input/form-input.components";
import CustomButton from "../custom-button/custom-button.component"; import CustomButton from "../custom-button/custom-button.component";


import { auth, createUserProfileDocument } from "../../firebase/firebase.utils"; import { signUpStart } from "../../redux/user/user.actions";


import { SignUpContainer, SignUpTitle } from "./sign-up.styles"; import { SignUpContainer, SignUpTitle } from "./sign-up.styles";
class SignUp extends React.Component { class SignUp extends React.Component {
Expand All @@ -21,29 +22,15 @@ class SignUp extends React.Component {
handleSubmit = async event => { handleSubmit = async event => {
event.preventDefault(); event.preventDefault();


const { signUpStart } = this.props;
const { displayName, email, password, confirmPassword } = this.state; const { displayName, email, password, confirmPassword } = this.state;


if (password !== confirmPassword) { if (password !== confirmPassword) {
alert("Passwords do not match"); alert("Passwords do not match");
return; return;
} }


try { signUpStart({ email, password, displayName });
const { user } = await auth.createUserWithEmailAndPassword(
email,
password
);
await createUserProfileDocument(user, { displayName });

this.setState({
displayName: "",
email: "",
password: "",
confirmPassword: ""
});
} catch (error) {
console.log("Error in sign up", error.message);
}
}; };


handelChange = event => { handelChange = event => {
Expand Down Expand Up @@ -99,4 +86,11 @@ class SignUp extends React.Component {
} }
} }


export default SignUp; const mapDispatchToProps = dispatch => ({
signUpStart: userCredentials => dispatch(signUpStart(userCredentials))
});

export default connect(
null,
mapDispatchToProps
)(SignUp);
15 changes: 15 additions & 0 deletions src/redux/user/user.actions.js
Expand Up @@ -40,3 +40,18 @@ export const signOutFailure = error => ({
type: UserActionTypes.SIGN_OUT_FAILURE, type: UserActionTypes.SIGN_OUT_FAILURE,
payload: error payload: error
}); });

export const signUpStart = userCredentials => ({
type: UserActionTypes.SIGN_UP_START,
payload: userCredentials
});

export const signUpSuccess = ({ user, additionalData }) => ({
type: UserActionTypes.SIGN_UP_SUCCESS,
payload: { user, additionalData }
});

export const signUpFailure = error => ({
type: UserActionTypes.SIGN_UP_FAILURE,
payload: error
});
1 change: 1 addition & 0 deletions src/redux/user/user.reducer.jsx
Expand Up @@ -23,6 +23,7 @@ const userReducer = (state = INITIAL_STATE, action) => {


case UserActionTypes.SIGN_IN_FAILURE: case UserActionTypes.SIGN_IN_FAILURE:
case UserActionTypes.SIGN_OUT_FAILURE: case UserActionTypes.SIGN_OUT_FAILURE:
case UserActionTypes.SIGN_UP_FAILURE:
return { return {
...state, ...state,
error: action.payload error: action.payload
Expand Down
37 changes: 33 additions & 4 deletions src/redux/user/user.sagas.js
Expand Up @@ -6,7 +6,9 @@ import {
signInSuccess, signInSuccess,
signInFailure, signInFailure,
signOutSuccess, signOutSuccess,
signOutFailure signOutFailure,
signUpSuccess,
signUpFailure
} from "./user.actions"; } from "./user.actions";


import { import {
Expand All @@ -16,9 +18,13 @@ import {
getCurrentUser getCurrentUser
} from "../../firebase/firebase.utils"; } from "../../firebase/firebase.utils";


export function* getSnapshotFromUserAuth(userAuth) { export function* getSnapshotFromUserAuth(userAuth, additionalData) {
try { try {
const userRef = yield call(createUserProfileDocument, userAuth); const userRef = yield call(
createUserProfileDocument,
userAuth,
additionalData
);
const userSnapshot = yield userRef.get(); const userSnapshot = yield userRef.get();
yield put(signInSuccess({ id: userSnapshot.id, ...userSnapshot.data() })); yield put(signInSuccess({ id: userSnapshot.id, ...userSnapshot.data() }));
} catch (error) { } catch (error) {
Expand Down Expand Up @@ -63,6 +69,19 @@ export function* signOut() {
} }
} }


export function* signUp({ payload: { email, password, displayName } }) {
try {
const { user } = yield auth.createUserWithEmailAndPassword(email, password);
yield put(signUpSuccess({ user, additionalData: { displayName } }));
} catch (error) {
yield put(signUpFailure(error));
}
}

export function* signInAfterSignUp({ payload: { user, additionalData } }) {
yield getSnapshotFromUserAuth(user, additionalData);
}

export function* onGoogleSignInStart() { export function* onGoogleSignInStart() {
yield takeLatest(UserActionTypes.GOOGLE_SIGN_IN_START, googleSignInStart); yield takeLatest(UserActionTypes.GOOGLE_SIGN_IN_START, googleSignInStart);
} }
Expand All @@ -79,11 +98,21 @@ export function* onSignOutStart() {
yield takeLatest(UserActionTypes.SIGN_OUT_START, signOut); yield takeLatest(UserActionTypes.SIGN_OUT_START, signOut);
} }


export function* onSignUpStart() {
yield takeLatest(UserActionTypes.SIGN_UP_START, signUp);
}

export function* onSignUpSuccess() {
yield takeLatest(UserActionTypes.SIGN_UP_SUCCESS, signInAfterSignUp);
}

export function* userSagas() { export function* userSagas() {
yield all([ yield all([
call(onGoogleSignInStart), call(onGoogleSignInStart),
call(onEmailSignInStart), call(onEmailSignInStart),
call(onCheckUserSession), call(onCheckUserSession),
call(onSignOutStart) call(onSignOutStart),
call(onSignUpStart),
call(onSignUpSuccess)
]); ]);
} }
5 changes: 4 additions & 1 deletion src/redux/user/user.types.js
Expand Up @@ -7,7 +7,10 @@ const UserActionTypes = {
CHECK_USER_SESSION: "CHECK_USER_SESSION", CHECK_USER_SESSION: "CHECK_USER_SESSION",
SIGN_OUT_START: "SIGN_OUT_START", SIGN_OUT_START: "SIGN_OUT_START",
SIGN_OUT_SUCCESS: "SIGN_OUT_SUCCESS", SIGN_OUT_SUCCESS: "SIGN_OUT_SUCCESS",
SIGN_OUT_FAILURE: "SIGN_OUT_FAILURE" SIGN_OUT_FAILURE: "SIGN_OUT_FAILURE",
SIGN_UP_START: "SIGN_UP_START",
SIGN_UP_SUCCESS: "SIGN_UP_SUCCESS",
SIGN_UP_FAILURE: "SIGN_UP_FAILURE"
}; };


export default UserActionTypes; export default UserActionTypes;

0 comments on commit e41cfc7

Please sign in to comment.