Commit 3fcc9f92 by Syed Abdul Rahman

Added env, Implemented try catch and code cleanup completed

parent f7dac3cd
VITE_API_URL=http://192.168.1.73:3000
\ No newline at end of file
{ {
"users": [ "users": [
{ {
"id": "5ebc", "id": "0b2d",
"email": "syedabdul.rahman@krds.com", "email": "syedabdul.rahman@krds.com"
"selectionCount": 4
}, },
{ {
"id": "45ea", "id": "1d58",
"email": "abdul@gmail.com", "email": "abdul@gmail.com"
"selectionCount": 3
}, },
{ {
"id": "aafd", "id": "42f0",
"email": "lucy@gmail.com", "email": "lucy@gmail.com"
"selectionCount": 4 },
{
"id": "e770",
"email": "new@gmail.com"
} }
], ],
"selectedSeats": [ "selectedSeats": [
{ {
"id": "45ea", "id": "0b2d",
"userid": "45ea", "userid": "0b2d",
"selected": [ "selected": [
{ {
"row": 8, "row": 8,
"seat": 3 "seat": 4
}, },
{ {
"row": 8, "row": 8,
"seat": 2 "seat": 5
}, },
{ {
"row": 8, "row": 8,
"seat": 1 "seat": 6
} }
] ]
}, },
{ {
"id": "5ebc", "id": "1d58",
"userid": "5ebc", "userid": "1d58",
"selected": [ "selected": [
{ {
"row": 6, "row": 8,
"seat": 5 "seat": 1
},
{
"row": 6,
"seat": 6
}, },
{ {
"row": 6, "row": 8,
"seat": 7 "seat": 2
}, },
{ {
"row": 6, "row": 8,
"seat": 8 "seat": 3
} }
] ]
}, },
{ {
"id": "aafd", "id": "42f0",
"userid": "aafd", "userid": "42f0",
"selected": [ "selected": [
{ {
"row": 7, "row": 7,
"seat": 5 "seat": 1
}, },
{ {
"row": 7, "row": 7,
"seat": 6 "seat": 2
}, },
{ {
"row": 7, "row": 7,
"seat": 7 "seat": 3
}, },
{ {
"row": 7, "row": 7,
"seat": 8 "seat": 4
} }
] ]
} }
......
...@@ -7,28 +7,31 @@ ...@@ -7,28 +7,31 @@
} }
.title { .title {
font-family: "Poppins-Bold"; font-family: 'Poppins-Bold';
color: white; color: white;
font-size: 2rem; font-size: 2rem;
} }
.screenWrapper { .screenWrapper {
width: 80%; width: 50%;
min-width: 300px;
} }
@media screen and (min-width: 768px) { @media screen and (min-width: 768px) {
.title { .title {
font-family: "Poppins-Bold"; font-family: 'Poppins-Bold';
color: white; color: white;
font-size: 2rem; font-size: 2rem;
} }
.screenWrapper { .screenWrapper {
width: 30%; width: 30% !important;
max-width: 30%;
min-width: 400px;
} }
.title { .title {
font-family: "Poppins-Bold"; font-family: 'Poppins-Bold';
color: white; color: white;
font-size: 2rem; font-size: 2rem;
} }
......
export const appConstants = { export const appConstants = {
API_URL_SEATS: 'http://192.168.1.73:3000/selectedSeats',
API_URL_USERS: 'http://192.168.1.73:3000/users',
USER: 'user', USER: 'user',
MODAL: 'modal' MODAL: 'modal'
}; };
......
import { appConstants } from '../lib/AppConstants';
export const login = async (email) => { export const login = async (email) => {
const response = await fetch(`${appConstants.API_URL_USERS}`); try {
const data = await response.json(); const response = await fetch(`${import.meta.env.VITE_API_URL}/users`);
const user = data.find((e) => e.email == email); const data = await response.json();
return user; const user = data.find((e) => e.email === email);
return user;
} catch (error) {
console.error('Login error:', error);
throw error;
}
}; };
export const createUser = async (email) => { export const createUser = async (email) => {
const response = await fetch(`${appConstants.API_URL_USERS}`, { try {
method: 'POST', const response = await fetch(`${import.meta.env.VITE_API_URL}/users`, {
body: JSON.stringify({ method: 'POST',
email: email headers: {
}) 'Content-Type': 'application/json'
}); },
const data = await response.json(); body: JSON.stringify({ email })
return data; });
const data = await response.json();
return data;
} catch (error) {
console.error('Create user error:', error);
throw error;
}
}; };
export const getSelectedSeatsApi = async () => { export const getSelectedSeatsApi = async () => {
const response = await fetch(`${appConstants.API_URL_SEATS}`); try {
const data = await response.json(); const response = await fetch(
return data; `${import.meta.env.VITE_API_URL}/selectedSeats`
);
const data = await response.json();
return data;
} catch (error) {
console.error('Get selected seats error:', error);
throw error;
}
}; };
export const editSeatsApi = async (id, body) => { export const editSeatsApi = async (id, body) => {
try { try {
await fetch(`${appConstants.API_URL_SEATS}/${id}`, { await fetch(`${import.meta.env.VITE_API_URL}/selectedSeats/${id}`, {
method: 'PATCH', method: 'PATCH',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
...@@ -34,32 +50,22 @@ export const editSeatsApi = async (id, body) => { ...@@ -34,32 +50,22 @@ export const editSeatsApi = async (id, body) => {
body: JSON.stringify({ selected: body }) body: JSON.stringify({ selected: body })
}); });
} catch (error) { } catch (error) {
console.log(error, 'error'); console.error('Edit seats error:', error);
throw error;
} }
}; };
export const createSeatApi = async (body) => { export const createSeatApi = async (body) => {
await fetch(`${appConstants.API_URL_SEATS}`, { try {
method: 'POST', await fetch(`${import.meta.env.VITE_API_URL}/selectedSeats`, {
headers: { method: 'POST',
'Content-Type': 'application/json' headers: {
}, 'Content-Type': 'application/json'
body: JSON.stringify(body) },
}); body: JSON.stringify(body)
}; });
} catch (error) {
export const saveSeatSelectionCountApi = async (id, count) => { console.error('Create seat error:', error);
await fetch(`${appConstants.API_URL_USERS}/${id}`, { throw error;
method: 'PATCH', }
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ selectionCount: count })
});
};
export const getSeatSelectionCountApi = async (id) => {
const response = await fetch(`${appConstants.API_URL_USERS}/${id}`);
const data = await response.json();
return data ? data : null;
}; };
export const setItem = (key, value) => { export const setItem = (key, value) => {
localStorage.setItem(key, JSON.stringify(value)); sessionStorage.setItem(key, JSON.stringify(value));
}; };
export const getItem = (key) => { export const getItem = (key) => {
const item = localStorage.getItem(key); const item = sessionStorage.getItem(key);
console.log(item, 'item');
return item ? JSON.parse(item) : null; return item ? JSON.parse(item) : null;
}; };
export const reset = () => { export const reset = () => {
localStorage.clear(); sessionStorage.clear();
}; };
...@@ -5,9 +5,7 @@ import { appConstants } from '../../lib/AppConstants'; ...@@ -5,9 +5,7 @@ import { appConstants } from '../../lib/AppConstants';
import { import {
getSelectedSeatsApi, getSelectedSeatsApi,
editSeatsApi, editSeatsApi,
createSeatApi, createSeatApi
saveSeatSelectionCountApi,
getSeatSelectionCountApi
} from '../../lib/api'; } from '../../lib/api';
import BookingWrapper from '../../components/Layout/BookingWrapper/Index'; import BookingWrapper from '../../components/Layout/BookingWrapper/Index';
import Header from '../../components/Layout/Header/Index'; import Header from '../../components/Layout/Header/Index';
...@@ -44,27 +42,36 @@ const BookingPage = () => { ...@@ -44,27 +42,36 @@ const BookingPage = () => {
const [modalButtonState, setModalButtonState] = useState(true); const [modalButtonState, setModalButtonState] = useState(true);
const [showSuccessModal, setShowSuccessModal] = useState(false); const [showSuccessModal, setShowSuccessModal] = useState(false);
useEffect(() => { useEffect(() => {
const fetchSeatSelectionCount = async () => {
const data = await getSeatSelectionCountApi(userId);
setNoOfSeats(data?.selectionCount);
};
fetchSelectedSeats(); fetchSelectedSeats();
fetchSeatSelectionCount();
setShowSeatsModal(getItem(appConstants.MODAL)); setShowSeatsModal(getItem(appConstants.MODAL));
}, []); }, []);
const fetchSelectedSeats = async () => { const fetchSelectedSeats = async () => {
const data = await getSelectedSeatsApi(); try {
const currentData = data?.find((e) => e.userid == userId); const data = await getSelectedSeatsApi();
const structuredData = data.flatMap((user) => user.selected); const currentData = data?.find((e) => e.userid == userId);
const temp = structuredData?.filter( const structuredData = data.flatMap((user) => user.selected);
(e) =>
!currentData?.selected?.some( const temp = structuredData?.filter(
(e2) => e2.row === e.row && e2.seat === e.seat (e) =>
) !currentData?.selected?.some(
); (e2) => e2.row === e.row && e2.seat === e.seat
setSelectedSeats(temp); )
setCurrentSeats(currentData?.selected); );
setSelectedSeats(temp);
setCurrentSeats(currentData?.selected);
setNoOfSeats(
currentData?.selected?.length
? currentData?.selected?.length
: noOfSeats
);
} catch (error) {
console.error('Error in fetchSelectedSeats:', error);
setSelectedSeats([]);
setCurrentSeats([]);
setNoOfSeats(noOfSeats);
}
}; };
const logout = () => { const logout = () => {
...@@ -84,9 +91,6 @@ const BookingPage = () => { ...@@ -84,9 +91,6 @@ const BookingPage = () => {
const modalClose = () => { const modalClose = () => {
setItem('modal', false); setItem('modal', false);
if (userId && noOfSeats) {
saveSeatSelectionCountApi(userId, noOfSeats);
}
fetchSelectedSeats(); fetchSelectedSeats();
setShowSeatsModal(false); setShowSeatsModal(false);
}; };
...@@ -136,21 +140,25 @@ const BookingPage = () => { ...@@ -136,21 +140,25 @@ const BookingPage = () => {
}; };
const seatsFinal = async () => { const seatsFinal = async () => {
let data = await getSelectedSeatsApi(); try {
const existingUserSeatMap = data?.find((ele) => ele.id == userId); const data = await getSelectedSeatsApi();
if (existingUserSeatMap) { const existingUserSeatMap = data?.find((ele) => ele.id == userId);
await editSeatsApi(userId, currentSeats); if (existingUserSeatMap) {
} else { await editSeatsApi(userId, currentSeats);
let info = { } else {
id: userId, const info = {
userid: userId, id: userId,
selected: currentSeats userid: userId,
}; selected: currentSeats
await createSeatApi(info); };
await createSeatApi(info);
}
setShowSeatsConfirmationModal(false);
fetchSelectedSeats();
setShowSuccessModal(true);
} catch (error) {
console.error('Error in seatsFinal:', error);
} }
setShowSeatsConfirmationModal(false);
fetchSelectedSeats();
setShowSuccessModal(true);
}; };
const handleSuccessModalConfirm = () => { const handleSuccessModalConfirm = () => {
...@@ -165,6 +173,7 @@ const BookingPage = () => { ...@@ -165,6 +173,7 @@ const BookingPage = () => {
className={styles['logout']} className={styles['logout']}
width={30} width={30}
onClick={() => logout()} onClick={() => logout()}
title="Logout"
/> />
<div className={styles['screen-wrapper']}> <div className={styles['screen-wrapper']}>
<Header>Choose Seats</Header> <Header>Choose Seats</Header>
......
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import Login from '../../components/TopLevel/Login/Index'; import Login from '../../components/TopLevel/Login/Index';
import { login, createUser, getSeatSelectionCountApi } from '../../lib/api'; import { login, createUser, getSelectedSeatsApi } from '../../lib/api';
import { setItem } from '../../lib/localStorage'; import { setItem } from '../../lib/localStorage';
const LoginPage = () => { const LoginPage = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const onSubmit = async (email) => { const onSubmit = async (email) => {
const userData = await login(email); try {
const fetchSeatSelectionCount = async (id) => { const userData = await login(email);
const data = await getSeatSelectionCountApi(id);
if (data.selectionCount) { const fetchSeatSelectionCount = async (id) => {
setItem('modal', false); try {
const data = await getSelectedSeatsApi();
const currentData = data?.find((e) => e.userid == id);
if (currentData?.selected?.length > 0) {
setItem('modal', false);
} else {
setItem('modal', true);
}
navigate('/');
} catch (error) {
console.error('Error fetching seat selection count:', error);
}
};
if (userData) {
setItem('user', userData.id);
fetchSeatSelectionCount(userData?.id);
} else { } else {
setItem('modal', true); const data = await createUser(email);
setItem('user', data.id);
fetchSeatSelectionCount(data?.id);
} }
navigate('/'); } catch (error) {
}; console.error('Error during login or user creation:', error);
if (userData) {
setItem('user', userData.id);
fetchSeatSelectionCount(userData?.id);
} else {
const data = await createUser(email);
setItem('user', data.id);
fetchSeatSelectionCount(data?.id);
} }
}; };
......
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