Commit 37f4be73 by Madhankumar

appcontext update

parent c70643c1
import { createContext, useContext, useState } from "react";
import * as api from "../lib/api";
import { toast } from "react-toastify";
import { useNavigate } from "react-router-dom";
export const AppContext = createContext();
export const useAppContext = () => useContext(AppContext);
......@@ -8,8 +9,8 @@ export function ContextProvider({ children }) {
const [userSeats, setUserSeats] = useState([]);
const [reservedSeats, setReservedSeats] = useState([]);
const [seatLayout, setSeatLayout] = useState([]);
const [isLoading, setisLoading] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const navigate = useNavigate();
const loginOrRegister = async ({ mobile }) => {
try {
const user = await api.getUserByMobile({ mobile });
......@@ -19,7 +20,8 @@ export function ContextProvider({ children }) {
sessionStorage.setItem("userId", response?.id);
await getAllReservedSeats();
// return true for newly registered user
return true;
navigate("/seat-limit");
} else {
//login
sessionStorage.setItem("userId", user?.[0]?.id);
......@@ -30,7 +32,7 @@ export function ContextProvider({ children }) {
userSelectedSeats?.[0]?.seats?.length
);
// return false for already registered user
return false;
navigate("/booking");
}
} catch (err) {
toast.error(err.message);
......@@ -53,7 +55,7 @@ export function ContextProvider({ children }) {
}
};
const getAllReservedSeats = async () => {
setisLoading(false);
setIsLoading(false);
try {
//to clear the data for glitch
setReservedSeats([]);
......@@ -72,7 +74,7 @@ export function ContextProvider({ children }) {
setUserSeats(e.seats)
);
setisLoading(true);
setIsLoading(true);
return await response;
} catch (err) {
toast.error(err.message);
......@@ -80,12 +82,12 @@ export function ContextProvider({ children }) {
};
const getSeatLayout = async () => {
setisLoading(false);
setIsLoading(false);
try {
setSeatLayout([]);
const response = await api.getSeatLayout();
setSeatLayout(response);
setisLoading(true);
setIsLoading(true);
} catch (err) {
toast.error(err.message);
}
......
import { useEffect } from "react";
import { useAppContext } from "../context";
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();
useEffect(() => {
sessionStorage.clear();
}, []);
const handleLogin = async ({ mobile }) => {
const isRegistered = await loginOrRegister({ mobile });
if (isRegistered) {
navigate("/seat-limit");
} else {
navigate("/booking");
}
await loginOrRegister({ mobile });
};
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