Commit 9576e68a by jay

vendor sign in, otp, business details

1 parent edc08403
...@@ -2,14 +2,22 @@ import { getSession, signOut } from "next-auth/react"; ...@@ -2,14 +2,22 @@ import { getSession, signOut } from "next-auth/react";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { useSelector } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { cleanImage } from "../../services/imageHandling"; import { cleanImage } from "../../services/imageHandling";
import { Button, Container, Form, Nav, Navbar } from "react-bootstrap"; import { Button, Container, Form, Nav, Navbar } from "react-bootstrap";
import { loadUser } from "../../redux/actions/userActions";
const Header = () => { const Header = () => {
const { user, error } = useSelector(state => state.loadedUser);
const dispatch = useDispatch();
console.log("user", user);
const [isSticky, setIsSticky] = useState(false); const [isSticky, setIsSticky] = useState(false);
useEffect(() => { useEffect(() => {
dispatch(loadUser());
}, []);
useEffect(() => {
const handleScroll = () => { const handleScroll = () => {
// Check if the scroll position is greater than a certain threshold // Check if the scroll position is greater than a certain threshold
const scrollY = window.scrollY || window.pageYOffset; const scrollY = window.scrollY || window.pageYOffset;
...@@ -17,15 +25,15 @@ const Header = () => { ...@@ -17,15 +25,15 @@ const Header = () => {
}; };
// Attach the scroll event listener when the component mounts // Attach the scroll event listener when the component mounts
window.addEventListener('scroll', handleScroll); window.addEventListener("scroll", handleScroll);
// Clean up the event listener when the component unmounts // Clean up the event listener when the component unmounts
return () => { return () => {
window.removeEventListener('scroll', handleScroll); window.removeEventListener("scroll", handleScroll);
}; };
}, []); }, []);
return ( return (
<header className={`header_wrap ${isSticky ? 'stick' : ''}`}> <header className={`header_wrap ${isSticky ? "stick" : ""}`}>
<Navbar expand="lg" className="bg-body-tertiary"> <Navbar expand="lg" className="bg-body-tertiary">
<Container fluid> <Container fluid>
<Navbar.Brand href="#"> <Navbar.Brand href="#">
...@@ -54,12 +62,23 @@ const Header = () => { ...@@ -54,12 +62,23 @@ const Header = () => {
</Button> </Button>
</div> </div>
</Form> </Form>
{/* {console.log(user.id)} */}
{(user && user.id) ? (
<div>
{console.log("here")}
<div><p>Brand Logo</p></div>
<p>{user.phone}</p>
</div>
) : (
<div>
<Button className="me-3" variant="primary"> <Button className="me-3" variant="primary">
Sign Up Sign Up
</Button> </Button>
<Button className="" variant="primary"> <Button className="" variant="primary">
Log In Log In
</Button> </Button>
</div>
)}
</Navbar.Collapse> </Navbar.Collapse>
</Container> </Container>
</Navbar> </Navbar>
......
...@@ -13,6 +13,7 @@ import { registerUser } from "../../redux/actions/userActions"; ...@@ -13,6 +13,7 @@ import { registerUser } from "../../redux/actions/userActions";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import OTPInput from "../common-components/OTPInput"; import OTPInput from "../common-components/OTPInput";
import { finishVendorOtpVerification } from "../../redux/actions/vendorActions"; import { finishVendorOtpVerification } from "../../redux/actions/vendorActions";
import { signIn } from "next-auth/react";
const Signup = props => { const Signup = props => {
console.log(props.type); console.log(props.type);
...@@ -35,7 +36,7 @@ const Signup = props => { ...@@ -35,7 +36,7 @@ const Signup = props => {
}; };
const signupValidationSchema = Yup.object().shape({ const signupValidationSchema = Yup.object().shape({
fullname: Yup.string().required("Fullname is Required"), fullname: Yup.string().required("Full name is required"),
email: Yup.string().required("Email Id is Required").email("Please Enter An Valid Email Id"), email: Yup.string().required("Email Id is Required").email("Please Enter An Valid Email Id"),
password: Yup.string().required("Password is Required").min(6, "Password must be minimum 6 characters"), password: Yup.string().required("Password is Required").min(6, "Password must be minimum 6 characters"),
confirmPassword: Yup.string() confirmPassword: Yup.string()
...@@ -74,6 +75,8 @@ const Signup = props => { ...@@ -74,6 +75,8 @@ const Signup = props => {
} }
}; };
// console.log("otp", otp);
return ( return (
<Fragment> <Fragment>
<div className="contaier-fluid login-banner-image"> <div className="contaier-fluid login-banner-image">
...@@ -130,14 +133,23 @@ const Signup = props => { ...@@ -130,14 +133,23 @@ const Signup = props => {
const otpRes = await finishVendorOtpVerification({ email: values.email, oneTimePassword }); const otpRes = await finishVendorOtpVerification({ email: values.email, oneTimePassword });
console.log("otpRes", otpRes); console.log("otpRes", otpRes);
if (otpRes.data.ok) { if (otpRes.data.ok) {
// router.push("/thank-you") const result = await signIn("credentials", {
toast.success("User registered successflly"); email: values.email,
password: values.password,
redirect: false
});
console.log("result", result);
router.push("/vendor/business-details");
// toast.success("User registered successflly");
} else if (!otpRes.data.ok) {
setOtp(new Array(4).fill(""));
toast.error("Invalid OTP, please try again.");
} }
} }
}} }}
> >
<div className="input-group"> <div className="input-group">
<label>Fullname</label> <label>Full Name</label>
<input type="text" name="fullname" onChange={handleChange} onBlur={handleBlur} value={values.fullname} placeholder="Your name" /> <input type="text" name="fullname" onChange={handleChange} onBlur={handleBlur} value={values.fullname} placeholder="Your name" />
{errors.fullname && touched.fullname && <span className="form-error">{errors.fullname}</span>} {errors.fullname && touched.fullname && <span className="form-error">{errors.fullname}</span>}
</div> </div>
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
"qs": "^6.11.0", "qs": "^6.11.0",
"react": "18.2.0", "react": "18.2.0",
"react-bootstrap": "^2.5.0", "react-bootstrap": "^2.5.0",
"react-bootstrap-typeahead": "^6.0.0", "react-bootstrap-typeahead": "^6.3.2",
"react-datepicker": "^4.8.0", "react-datepicker": "^4.8.0",
"react-dom": "18.2.0", "react-dom": "18.2.0",
"react-icons": "^5.0.1", "react-icons": "^5.0.1",
......
...@@ -37,15 +37,13 @@ export default NextAuth({ ...@@ -37,15 +37,13 @@ export default NextAuth({
* We can expect it contains two properties: `email` and `password` * We can expect it contains two properties: `email` and `password`
*/ */
try { try {
const { const userResponse = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/auth/local`, {
data: { user, jwt }
} = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/auth/local`, {
identifier: email, identifier: email,
password: password password: password
}); });
// console.log("Axios login returned with data:"); // console.log("Axios login returned with data:");
// console.log(user); console.log("userResponse", userResponse.data);
// console.log(jwt); // console.log(jwt);
// Response from the above call can be // Response from the above call can be
...@@ -75,7 +73,7 @@ export default NextAuth({ ...@@ -75,7 +73,7 @@ export default NextAuth({
// } // }
// } // }
return { ...user, name: user.username, jwt }; return { ...userResponse.data.user, name: userResponse.data.user.email, jwt: userResponse.data.jwt, email: userResponse.data.user.email, user: userResponse.data.user };
} catch (error) { } catch (error) {
console.log("Error while fetching credentials:"); console.log("Error while fetching credentials:");
console.log(error.response.data); console.log(error.response.data);
...@@ -89,6 +87,8 @@ export default NextAuth({ ...@@ -89,6 +87,8 @@ export default NextAuth({
], ],
callbacks: { callbacks: {
session: async ({ session, token }) => { session: async ({ session, token }) => {
console.log("session 1", session);
console.log("session 2", token);
session.id = token.id; session.id = token.id;
session.jwt = token.jwt; session.jwt = token.jwt;
...@@ -98,6 +98,9 @@ export default NextAuth({ ...@@ -98,6 +98,9 @@ export default NextAuth({
return Promise.resolve(session); return Promise.resolve(session);
}, },
jwt: async ({ token, user }) => { jwt: async ({ token, user }) => {
console.log("user 1", user);
console.log("token 1", token);
const isSignIn = user ? true : false; const isSignIn = user ? true : false;
if (isSignIn) { if (isSignIn) {
token.id = user.id; token.id = user.id;
......
...@@ -35,11 +35,12 @@ export const registerUser = userData => async dispatch => { ...@@ -35,11 +35,12 @@ export const registerUser = userData => async dispatch => {
}; };
const userFormData = new FormData(); const userFormData = new FormData();
userFormData.append("username", userData.mobile); userFormData.append("username", `${userData.mobile}-${userData.email}`);
userFormData.append("email", userData.email); userFormData.append("email", userData.email);
userFormData.append("password", userData.password); userFormData.append("password", userData.password);
userFormData.append("role", userData.role); userFormData.append("role", userData.role);
userFormData.append("phone", userData.mobile);
console.log("userFormData", userFormData);
const response = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/auth/local/register`, userFormData, config); const response = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/auth/local/register`, userFormData, config);
console.log(`Register user done:`); console.log(`Register user done:`);
......
import axios from "axios"; import axios from "axios";
import { getSession } from "next-auth/react";
import qs from "qs";
import {
GET_LOGGED_IN_VENDOR_FAIL,
GET_LOGGED_IN_VENDOR_REQUEST,
GET_LOGGED_IN_VENDOR_SUCCESS,
UPDATE_VENDOR_DETAILS_FAIL,
UPDATE_VENDOR_DETAILS_REQUEST,
UPDATE_VENDOR_DETAILS_SUCCESS
} from "../constants/vendorConstants";
export const finishVendorOtpVerification = async verificationData => { export const finishVendorOtpVerification = async verificationData => {
const config = { const config = {
...@@ -9,3 +19,109 @@ export const finishVendorOtpVerification = async verificationData => { ...@@ -9,3 +19,109 @@ export const finishVendorOtpVerification = async verificationData => {
return await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendor/finish-otp-verification`, verificationData, config); return await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendor/finish-otp-verification`, verificationData, config);
}; };
export const pincodeSearchByFilter = async code => {
const config = {
headers: {
"Content-Type": "application/json"
}
};
const query = {
filters: {
name: {
$contains: code
}
},
populate: ["masterCity"]
};
const queryString = qs.stringify(query, {
encodeValuesOnly: true
});
return await axios.get(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/master-pincodes?${queryString}`, config);
};
export const updateVendorBusinessDetails =
({ businessDetails, vendorId }) =>
async dispatch => {
const session = await getSession();
if (!session) {
throw new Error("You are not authenticated. Please log in.");
}
try {
dispatch({
type: UPDATE_VENDOR_DETAILS_REQUEST
});
const config = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session.jwt}`
}
};
const response = await axios.put(
`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/${vendorId}`,
{
businessDetails
},
config
);
dispatch({
type: UPDATE_VENDOR_DETAILS_SUCCESS
});
} catch (error) {
dispatch({
type: UPDATE_VENDOR_DETAILS_FAIL
});
}
};
export const getLoggedInVendor = () => async dispatch => {
const session = await getSession();
console.log("session", session);
if (!session) {
throw new Error("You are not authenticated. Please log in.");
}
try {
dispatch({
type: GET_LOGGED_IN_VENDOR_REQUEST
});
const config = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session.jwt}`
}
};
const query = {
filters: {
user: {
$eq: session.id
}
}
};
const queryString = qs.stringify(query, {
encodeValuesOnly: true
});
const response = await axios.get(
`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/vendors/${queryString}`, config
);
console.log("response", response.data);
dispatch({
type: GET_LOGGED_IN_VENDOR_SUCCESS
});
} catch (error) {
dispatch({
type: GET_LOGGED_IN_VENDOR_FAIL
});
}
};
// export const FETCH_OTP_VERIFY_REQUEST = "FETCH_OTP_VERIFY_REQUEST" export const GET_LOGGED_IN_VENDOR_REQUEST = "GET_LOGGED_IN_VENDOR_REQUEST"
// export const FETCH_OTP_VERIFY_SUCCESS = "FETCH_OTP_VERIFY_SUCCESS" export const GET_LOGGED_IN_VENDOR_SUCCESS = "GET_LOGGED_IN_VENDOR_SUCCESS"
// export const FETCH_OTP_VERIFY_FAIL = "FETCH_OTP_VERIFY_FAIL" export const GET_LOGGED_IN_VENDOR_FAIL = "GET_LOGGED_IN_VENDOR_FAIL"
// export const CLEAR_ERRORS = "CLEAR_ERRORS";
\ No newline at end of file \ No newline at end of file
export const UPDATE_VENDOR_DETAILS_REQUEST = "UPDATE_VENDOR_DETAILS_REQUEST"
export const UPDATE_VENDOR_DETAILS_SUCCESS = "UPDATE_VENDOR_DETAILS_SUCCESS"
export const UPDATE_VENDOR_DETAILS_FAIL = "UPDATE_VENDOR_DETAILS_FAIL"
export const CLEAR_ERRORS = "CLEAR_ERRORS";
\ No newline at end of file \ No newline at end of file
import { UPDATE_VENDOR_DETAILS_FAIL, UPDATE_VENDOR_DETAILS_REQUEST, UPDATE_VENDOR_DETAILS_SUCCESS, CLEAR_ERRORS } from "../constants/vendorConstants";
export const updateVendorReducer = (state = {}, action) => {
switch (action.type) {
case UPDATE_VENDOR_DETAILS_REQUEST:
return { loading: true };
case UPDATE_VENDOR_DETAILS_SUCCESS:
return {
loading: false,
updatedVendorData: action.payload
};
case UPDATE_VENDOR_DETAILS_FAIL:
return {
loading: false,
error: action.payload.error.message
};
case CLEAR_ERRORS:
return {
...state,
error: null
};
default:
return state;
}
};
This diff could not be displayed because it is too large.
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!