Commit 37f4be73 by Madhankumar

appcontext update

parent c70643c1
import { createContext, useContext, useState } from "react"; import { createContext, useContext, useState } from "react";
import * as api from "../lib/api"; import * as api from "../lib/api";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { useNavigate } from "react-router-dom";
export const AppContext = createContext(); export const AppContext = createContext();
export const useAppContext = () => useContext(AppContext); export const useAppContext = () => useContext(AppContext);
...@@ -8,8 +9,8 @@ export function ContextProvider({ children }) { ...@@ -8,8 +9,8 @@ export function ContextProvider({ children }) {
const [userSeats, setUserSeats] = useState([]); const [userSeats, setUserSeats] = useState([]);
const [reservedSeats, setReservedSeats] = useState([]); const [reservedSeats, setReservedSeats] = useState([]);
const [seatLayout, setSeatLayout] = useState([]); const [seatLayout, setSeatLayout] = useState([]);
const [isLoading, setisLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const navigate = useNavigate();
const loginOrRegister = async ({ mobile }) => { const loginOrRegister = async ({ mobile }) => {
try { try {
const user = await api.getUserByMobile({ mobile }); const user = await api.getUserByMobile({ mobile });
...@@ -19,7 +20,8 @@ export function ContextProvider({ children }) { ...@@ -19,7 +20,8 @@ export function ContextProvider({ children }) {
sessionStorage.setItem("userId", response?.id); sessionStorage.setItem("userId", response?.id);
await getAllReservedSeats(); await getAllReservedSeats();
// return true for newly registered user // return true for newly registered user
return true;
navigate("/seat-limit");
} else { } else {
//login //login
sessionStorage.setItem("userId", user?.[0]?.id); sessionStorage.setItem("userId", user?.[0]?.id);
...@@ -30,7 +32,7 @@ export function ContextProvider({ children }) { ...@@ -30,7 +32,7 @@ export function ContextProvider({ children }) {
userSelectedSeats?.[0]?.seats?.length userSelectedSeats?.[0]?.seats?.length
); );
// return false for already registered user // return false for already registered user
return false; navigate("/booking");
} }
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
...@@ -53,7 +55,7 @@ export function ContextProvider({ children }) { ...@@ -53,7 +55,7 @@ export function ContextProvider({ children }) {
} }
}; };
const getAllReservedSeats = async () => { const getAllReservedSeats = async () => {
setisLoading(false); setIsLoading(false);
try { try {
//to clear the data for glitch //to clear the data for glitch
setReservedSeats([]); setReservedSeats([]);
...@@ -72,7 +74,7 @@ export function ContextProvider({ children }) { ...@@ -72,7 +74,7 @@ export function ContextProvider({ children }) {
setUserSeats(e.seats) setUserSeats(e.seats)
); );
setisLoading(true); setIsLoading(true);
return await response; return await response;
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
...@@ -80,12 +82,12 @@ export function ContextProvider({ children }) { ...@@ -80,12 +82,12 @@ export function ContextProvider({ children }) {
}; };
const getSeatLayout = async () => { const getSeatLayout = async () => {
setisLoading(false); setIsLoading(false);
try { try {
setSeatLayout([]); setSeatLayout([]);
const response = await api.getSeatLayout(); const response = await api.getSeatLayout();
setSeatLayout(response); setSeatLayout(response);
setisLoading(true); setIsLoading(true);
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
} }
......
import { useEffect } from "react"; import { useEffect } from "react";
import { useAppContext } from "../context"; import { useAppContext } from "../context";
import Login from "../components/top-level/login"; import Login from "../components/top-level/login";
import { useNavigate } from "react-router-dom";
export function LoginPage() {
const navigate = useNavigate();
export function LoginPage() {
const { loginOrRegister } = useAppContext(); const { loginOrRegister } = useAppContext();
useEffect(() => { useEffect(() => {
sessionStorage.clear(); sessionStorage.clear();
}, []); }, []);
const handleLogin = async ({ mobile }) => { const handleLogin = async ({ mobile }) => {
const isRegistered = await loginOrRegister({ mobile }); await loginOrRegister({ mobile });
if (isRegistered) {
navigate("/seat-limit");
} else {
navigate("/booking");
}
}; };
return <Login onSubmit={handleLogin} />; return <Login onSubmit={handleLogin} />;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment