Skip to content

Commit

Permalink
refactor: signup - signin to asynk thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
manismk committed May 24, 2022
1 parent 40a1a28 commit 4ac38b4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 68 deletions.
17 changes: 14 additions & 3 deletions src/pages/login/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useState } from "react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { InputPassword, InputTextBox } from "../../components";
import { InputPassword, InputTextBox, Loader } from "../../components";
import "./auth.css";
import { handleLoginValidation } from "../../utils/";
import { handleSignIn } from "../../service";
import { handleSignIn } from "../../store/features/authSlice";
import { useDispatch, useSelector } from "react-redux";

export const Login = () => {
const [userData, setUserData] = useState({
Expand All @@ -12,6 +13,8 @@ export const Login = () => {
mailError: "",
passwordError: "",
});
const { loginLoading } = useSelector((state) => state.auth);
const dispatch = useDispatch();
const navigate = useNavigate();
const location = useLocation();

Expand All @@ -29,7 +32,14 @@ export const Login = () => {
}));
}
if (mailError.length === 0 && passwordError.length === 0) {
handleSignIn(userData.userMail, userData.password, location, navigate);
dispatch(
handleSignIn({
email: userData.userMail,
password: userData.password,
location,
navigate,
})
);
}
};

Expand Down Expand Up @@ -112,6 +122,7 @@ export const Login = () => {
</p>
</div>
</main>
{loginLoading && <Loader />}
</>
);
};
24 changes: 15 additions & 9 deletions src/pages/signup/Signup.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { InputPassword, InputTextBox } from "../../components";
import { handleSignUp } from "../../service";
import { InputPassword, InputTextBox, Loader } from "../../components";
import { handleSignUp } from "../../store/features/authSlice";
import { handleSignUpValidation } from "../../utils";

export const SignUp = () => {
Expand All @@ -20,6 +21,8 @@ export const SignUp = () => {

const navigate = useNavigate();
const location = useLocation();
const dispatch = useDispatch();
const { signUpLoading } = useSelector((state) => state.auth);

const signUpHandler = () => {
const {
Expand Down Expand Up @@ -59,13 +62,15 @@ export const SignUp = () => {
lastNameError.length === 0 &&
confirmPasswordError.length === 0
) {
handleSignUp(
userData.userMail,
userData.password,
userData.firstName,
userData.lastName,
location,
navigate
dispatch(
handleSignUp({
email: userData.userMail,
password: userData.password,
firstName: userData.firstName,
lastName: userData.lastName,
location,
navigate,
})
);
}
};
Expand Down Expand Up @@ -175,6 +180,7 @@ export const SignUp = () => {
</p>
</div>
</main>
{signUpLoading && <Loader />}
</>
);
};
16 changes: 0 additions & 16 deletions src/service/handleSignIn.js

This file was deleted.

37 changes: 0 additions & 37 deletions src/service/handleSignUp.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ export { handleComment } from "./handleComment";
export { handleReply } from "./handleReply";
export { handleCreatePost } from "./handleCreatePost";
export { handleEditProfile } from "./handleEditProfile";
export { handleSignIn } from "./handleSignIn";
export { handleSignOut } from "./handleSignOut";
export { handleSignUp } from "./handleSignUp";
80 changes: 79 additions & 1 deletion src/store/features/authSlice.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
import { createSlice } from "@reduxjs/toolkit";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { toast } from "react-toastify";
import { auth, db } from "../../firebase";

const initialState = {
user: null,
loginLoading: false,
signUpLoading: false,
};

export const handleSignIn = createAsyncThunk(
"auth/signIn",
async ({ email, password, location, navigate }, thunkAPI) => {
const from = location.state?.from?.pathname || "/";
return auth
.signInWithEmailAndPassword(email, password)
.then(() => {
navigate(from, { replace: true });
toast.success("Logged In Successfully");
})
.catch((e) => {
console.log("Error in signIn", e);
toast.error(e.message);
});
}
);
export const handleSignUp = createAsyncThunk(
"auth/signUp",
async (
{ email, password, firstName, lastName, location, navigate },
thunkAPI
) => {
const from = location.state?.from?.pathname || "/";

return auth
.createUserWithEmailAndPassword(email, password)
.then((response) => {
db.collection(`users/`)
.doc(response.user.uid)
.set({
firstName,
lastName,
mailId: email,
bio: "I am a new socialoo user",
profilePictureUrl:
"https://firebasestorage.googleapis.com/v0/b/socialoo-e8f1c.appspot.com/o/profile-default.jpg?alt=media&token=2eaaa7f6-3f12-401b-b784-41deb1583aed",
portfolioLink: "",
uid: response.user.uid,
saved: [],
followers: [],
following: [],
})
.then(() => {
navigate(from, { replace: true });
toast.success("Signed Up Successfully");
});
})
.catch((e) => {
console.log("Error in signUp", e);
toast.error(e.message);
});
}
);

const authSlice = createSlice({
name: "auth",
initialState,
Expand All @@ -12,6 +70,26 @@ const authSlice = createSlice({
state.user = payload;
},
},
extraReducers: {
[handleSignIn.pending]: (state) => {
state.loginLoading = true;
},
[handleSignIn.fulfilled]: (state) => {
state.loginLoading = false;
},
[handleSignIn.rejected]: (state) => {
state.loginLoading = false;
},
[handleSignUp.pending]: (state) => {
state.signUpLoading = true;
},
[handleSignUp.fulfilled]: (state) => {
state.signUpLoading = false;
},
[handleSignUp.rejected]: (state) => {
state.signUpLoading = false;
},
},
});

export const { setUser } = authSlice.actions;
Expand Down

0 comments on commit 4ac38b4

Please sign in to comment.