This commit is contained in:
nospeedlimitindonesia
2024-10-09 04:35:15 +00:00
parent 1da45c528d
commit 624a9e8ec8
8 changed files with 267 additions and 184 deletions

View File

@@ -183,6 +183,8 @@ function CafePage({
isEditMode={isEditMode}
onFilterChange={(e) => setFilterId(e)}
filterId={filterId}
beingEditedType={beingEditedType}
setBeingEditedType={setBeingEditedType}
/>
<div style={{ marginTop: "-13px" }}></div>
{filterId === 0 ? (

View File

@@ -1,19 +1,25 @@
import React from "react";
import React, { useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import "./LoginPage.css";
import RouletteWheel from "../components/RouletteWheel";
import { loginUser } from "../helpers/userHelpers"; // Import from userHelper.js
import { loginUser, signUpUser } from "../helpers/userHelpers";
const LoginPage = () => {
const navigate = useNavigate();
const location = useLocation(); // Use useLocation hook instead of useSearchParams
const searchParams = new URLSearchParams(location.search); // Pass location.search directly
const location = useLocation();
const searchParams = new URLSearchParams(location.search);
const next = searchParams.get("next");
const table = searchParams.get("table");
const handleLogin = async (email, username, password) => {
const handleLogin = async (
email,
username,
password,
setLoading,
setError
) => {
try {
setLoading(true);
const response = await loginUser(username, password);
if (response.success) {
@@ -23,38 +29,60 @@ const LoginPage = () => {
window.location.href = response.cafeId;
} else {
let destination = "/";
// Validate parameters and construct the destination URL
if (table && !next) {
console.error(
'Parameter "table" requires "next" to be present in the URL.',
);
// Navigate to a default route or handle this case as needed
console.error('Parameter "table" requires "next" to be present.');
navigate("/");
return;
}
if (next) {
destination = `/${next}`;
if (table) {
destination += `?table=${table}`;
}
if (table) destination += `?table=${table}`;
}
// navigate(destination, { replace: true });
window.location.href = destination;
}
} else {
setError(true); // Trigger error state in the button
console.error("Login failed");
}
} catch (error) {
setError(true);
console.error("Error occurred while logging in:", error.message);
} finally {
setLoading(false); // Ensure loading state is cleared
}
};
const handleSignUp = async (
email,
username,
password,
setLoading,
setError
) => {
try {
setLoading(true);
const response = await signUpUser(email, username, password);
if (response.success) {
localStorage.setItem("auth", response.token);
let destination = "/";
window.location.href = destination;
} else {
setError(true); // Trigger error state in the button
console.error("Login failed");
}
} catch (error) {
setError(true);
console.error("Error occurred while logging in:", error.message);
} finally {
setLoading(false); // Ensure loading state is cleared
}
};
return (
<div className="login-container">
<RouletteWheel onSign={handleLogin} />
<RouletteWheel onSignIn={handleLogin} onSignUp={handleSignUp} />
</div>
);
};