Commit b6d296a7 by Madhankumar

api call optimized

parent 20ba4c3b
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
"reservedSeats": [ "reservedSeats": [
{ {
"seats": [ "seats": [
"E8", "D8",
"H4" "F7"
], ],
"id": 1 "id": 1
}, },
......
import "./App.css"; import "./App.css";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom"; import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { _Login } from "../src/pages/login"; import { _Login } from "../src/pages/login";
import { _Seatlayout } from "../src/pages/seat-layout"; import { _Seatlayout } from "../src/pages/seat-layout";
import { _Confirmation } from "../src/pages/confirmation"; import { _Confirmation } from "../src/pages/confirmation";
import { AuthGuard } from "./auth/index"; import { AuthGuard } from "./auth/index";
import { _seatLimit } from "../src/pages/seat-limit"; import { _seatLimit } from "../src/pages/seat-limit";
function App() { function App() {
......
...@@ -12,10 +12,16 @@ export function ContextProvider({ children }) { ...@@ -12,10 +12,16 @@ export function ContextProvider({ children }) {
if (!user.length) { if (!user.length) {
//register //register
const response = await api.Register({ mobile }); const response = await api.Register({ mobile });
return await response; return {
response: response,
isRegistered: true,
};
} else { } else {
//login //login
return await user[0]; return {
response: user[0],
isRegistered: false,
};
} }
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
...@@ -24,6 +30,7 @@ export function ContextProvider({ children }) { ...@@ -24,6 +30,7 @@ export function ContextProvider({ children }) {
const AddOrUpdateSeats = async ({ id, seats }) => { const AddOrUpdateSeats = async ({ id, seats }) => {
try { try {
const isSeats = userseats.filter((e) => e.id == id); const isSeats = userseats.filter((e) => e.id == id);
//check the id
if (!isSeats.length) { if (!isSeats.length) {
//add seats //add seats
const response = await api.AddSeats({ seats }); const response = await api.AddSeats({ seats });
...@@ -51,9 +58,8 @@ export function ContextProvider({ children }) { ...@@ -51,9 +58,8 @@ export function ContextProvider({ children }) {
const GetSeatById = async (id) => { const GetSeatById = async (id) => {
try { try {
const response = await api.GetAllSeats(); const response = await api.GetSeatById(id);
const result = response.filter((e) => e.id == id); return response;
return result;
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
} }
......
// import { useNavigate } from "react-router-dom";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Button from "../../base/button"; import Button from "../../base/button";
import styles from "./seat-layout.module.css"; import styles from "./seat-layout.module.css";
...@@ -51,9 +50,11 @@ function SeatLayout({ ...@@ -51,9 +50,11 @@ function SeatLayout({
{seats?.map((seat, i) => ( {seats?.map((seat, i) => (
<div key={i} className={styles.seat}> <div key={i} className={styles.seat}>
{seat?.map((e, i) => ( {seat?.map((e, i) => (
<div className={`${e === 0 ? styles["remove-seats"] : ""}`}> <div
key={i}
className={`${e === 0 ? styles["remove-seats"] : ""}`}
>
<Seat <Seat
key={e}
seatNo={e} seatNo={e}
onChange={() => handleSeats(e)} onChange={() => handleSeats(e)}
status={ status={
......
...@@ -9,7 +9,6 @@ function SeatLimit({ onLimit, ...props }) { ...@@ -9,7 +9,6 @@ function SeatLimit({ onLimit, ...props }) {
e.preventDefault(); e.preventDefault();
if (limit > 0) { if (limit > 0) {
onLimit({ limit }); onLimit({ limit });
sessionStorage.setItem("seatLimit", limit);
} else { } else {
toast.info("Please enter atleast 1 seat"); toast.info("Please enter atleast 1 seat");
} }
...@@ -26,12 +25,6 @@ function SeatLimit({ onLimit, ...props }) { ...@@ -26,12 +25,6 @@ function SeatLimit({ onLimit, ...props }) {
min="1" min="1"
max="10" max="10"
/> />
{/* <input
className={styles.input}
type="number"
value={limit}
onChange={(e) => setLimit(e.target.value)}
/> */}
</div> </div>
<Button {...props}>Book Seats</Button> <Button {...props}>Book Seats</Button>
</form> </form>
......
import Confirmation from "../components/top-level/confirmation"; import Confirmation from "../components/top-level/confirmation";
import { useLocation, useNavigate } from "react-router-dom"; import { useLocation, useNavigate } from "react-router-dom";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useAppContext } from "../_context";
export function _Confirmation() { export function _Confirmation() {
const [count, setCount] = useState(0); const [count, setCount] = useState(0);
const [reservedseat, setReservedSeat] = useState([]); const [reservedseat, setReservedSeat] = useState([]);
let { GetSeatById } = useAppContext();
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
useEffect(() => { useEffect(() => {
getSeatById(); getSeatById();
}, []); }, []);
const getSeatById = async () => { const getSeatById = async () => {
setCount(location?.state?.seats.length); if (location?.state?.seats.length) {
setReservedSeat(location?.state?.seats); setCount(location.state.seats.length);
setReservedSeat(location.state.seats);
} else {
const userid = sessionStorage.getItem("userId");
const userseats = await GetSeatById(userid);
setCount(userseats.seats.length);
setReservedSeat(userseats.seats);
}
}; };
const handleModify = (e) => { const handleModify = (e) => {
e.preventDefault(); e.preventDefault();
//for Auth Guard
sessionStorage.setItem("currentPath", "/booking"); sessionStorage.setItem("currentPath", "/booking");
navigate("/booking"); navigate("/booking");
}; };
......
...@@ -5,28 +5,23 @@ import { useNavigate } from "react-router-dom"; ...@@ -5,28 +5,23 @@ import { useNavigate } from "react-router-dom";
export function _Login() { export function _Login() {
const navigate = useNavigate(); const navigate = useNavigate();
const { LoginOrRegister, GetSeatById } = useAppContext(); const { LoginOrRegister } = useAppContext();
useEffect(() => { useEffect(() => {
sessionStorage.clear(); sessionStorage.clear();
}, []); }, []);
const handleLogin = async ({ mobile }) => { const handleLogin = async ({ mobile }) => {
const users = await LoginOrRegister({ mobile }); const users = await LoginOrRegister({ mobile });
sessionStorage.setItem("userId", users.response.id);
if (users) { if (users.isRegistered) {
sessionStorage.setItem("userId", users.id); //for Auth Guard
//check seat length for already booked seats for the user sessionStorage.setItem("currentPath", "/seat-limit");
const userseat = await GetSeatById(users.id); navigate("/seat-limit");
} else {
if (userseat.length) { //for Auth Guard
//if store the seat length in session sessionStorage.setItem("currentPath", "/booking");
sessionStorage.setItem("seatLimit", userseat[0].seats.length); navigate("/booking");
//redirect to booking component otherwise it will not redirect if not login
sessionStorage.setItem("currentPath", "/booking");
navigate("/booking");
} else {
sessionStorage.setItem("currentPath", "/seat-limit");
navigate("/seat-limit");
}
} }
}; };
return <Login onSubmit={handleLogin} />; return <Login onSubmit={handleLogin} />;
......
...@@ -5,20 +5,21 @@ import { useNavigate } from "react-router-dom"; ...@@ -5,20 +5,21 @@ import { useNavigate } from "react-router-dom";
import { bookingSeats } from "./seed"; import { bookingSeats } from "./seed";
export function _Seatlayout() { export function _Seatlayout() {
const { userseats, AddOrUpdateSeats, GetAllSeats } = useAppContext(); let { userseats, AddOrUpdateSeats, GetAllSeats } = useAppContext();
const [reservedSeat, setReservedSeat] = useState([]); const [reservedSeat, setReservedSeat] = useState([]);
const [selectedSeat, setSelectedSeat] = useState([]); const [selectedSeat, setSelectedSeat] = useState([]);
const [seatLimit, setSeatLimit] = useState(0);
const navigate = useNavigate(); const navigate = useNavigate();
const userId = sessionStorage.getItem("userId"); const userId = sessionStorage.getItem("userId");
const seatLimit = sessionStorage.getItem("seatLimit");
useEffect(() => { useEffect(() => {
handleReservedSeats(); handleReservedSeats();
}, [userseats]); }, [userseats]);
useEffect(() => { useLayoutEffect(() => {
GetAllSeats(); GetAllSeats();
}, []); }, []);
const handleReservedSeats = () => {
const handleReservedSeats = async () => {
let seatArray = []; let seatArray = [];
const reservedSeats = userseats?.filter((e) => e.id != userId); const reservedSeats = userseats?.filter((e) => e.id != userId);
reservedSeats.forEach((element) => { reservedSeats.forEach((element) => {
...@@ -27,9 +28,14 @@ export function _Seatlayout() { ...@@ -27,9 +28,14 @@ export function _Seatlayout() {
setReservedSeat(seatArray); setReservedSeat(seatArray);
}); });
}); });
const selectedSeats = userseats?.find((e) => e.id == userId); const selectedSeats = await userseats?.find((e) => e.id == userId);
const results = selectedSeats?.seats; const results = selectedSeats?.seats;
setSelectedSeat(results); setSelectedSeat(results);
const sessionSeatLimit = sessionStorage.getItem("seatLimit");
setTimeout(() => {
sessionStorage.setItem("seatLimit", sessionSeatLimit ?? results?.length);
setSeatLimit(sessionStorage.getItem("seatLimit"));
}, 500);
}; };
const handleConfirmed = async ({ id, seats }) => { const handleConfirmed = async ({ id, seats }) => {
......
...@@ -3,8 +3,9 @@ import { useNavigate } from "react-router-dom"; ...@@ -3,8 +3,9 @@ import { useNavigate } from "react-router-dom";
export function _seatLimit() { export function _seatLimit() {
const navigate = useNavigate(); const navigate = useNavigate();
const handleLimit = (e) => {
const handleLimit = () => { sessionStorage.setItem("seatLimit", e.limit);
//for Auth Guard
sessionStorage.setItem("currentPath", "/booking"); sessionStorage.setItem("currentPath", "/booking");
navigate("/booking"); navigate("/booking");
}; };
......
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