mirror of
http://88.130.71.182:3000/BlitTech/badoHair_fe.git
synced 2026-06-13 08:58:31 +00:00
changes after second dev test
fixed full name issue, refresh issue on reservation and user/admin login issue
This commit is contained in:
@@ -12,7 +12,7 @@ import { toast } from "sonner";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function AdminLogin() {
|
||||
const { login } = useAuth();
|
||||
const { login, logout } = useAuth();
|
||||
const router = useRouter();
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
@@ -24,6 +24,7 @@ export default function AdminLogin() {
|
||||
try {
|
||||
const profile = await login(email, password);
|
||||
if (profile.role !== "admin") {
|
||||
logout();
|
||||
toast.error("Accès refusé : rôle administrateur requis");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Check, X, Trash2, Mail, Phone } from "lucide-react";
|
||||
import { Check, X, Trash2, Mail, Phone, Loader2 } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
@@ -23,6 +23,7 @@ export default function AdminReservations() {
|
||||
const { t, locale } = useLanguage();
|
||||
const [filter, setFilter] = useState<"all" | Reservation["status"]>("all");
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null);
|
||||
const [updatingId, setUpdatingId] = useState<string | null>(null);
|
||||
|
||||
const statusConfig: Record<Reservation["status"], { label: string; variant: "default" | "secondary" | "destructive" }> = {
|
||||
pending: { label: t("admin.status.pending"), variant: "secondary" },
|
||||
@@ -35,20 +36,26 @@ export default function AdminReservations() {
|
||||
.sort((a, b) => `${b.date}${b.time}`.localeCompare(`${a.date}${a.time}`));
|
||||
|
||||
const handleConfirm = async (id: string) => {
|
||||
setUpdatingId(id);
|
||||
try {
|
||||
await updateReservationStatus(id, "confirmed");
|
||||
toast.success(t("admin.bookings.confirmed_toast"));
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : t("admin.error"));
|
||||
} finally {
|
||||
setUpdatingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCancel = async (id: string) => {
|
||||
setUpdatingId(id);
|
||||
try {
|
||||
await updateReservationStatus(id, "cancelled");
|
||||
toast.success(t("admin.bookings.cancelled_toast"));
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : t("admin.error"));
|
||||
} finally {
|
||||
setUpdatingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -135,19 +142,27 @@ export default function AdminReservations() {
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-right space-x-1">
|
||||
{r.status !== "confirmed" && (
|
||||
<Button variant="ghost" size="icon" onClick={() => handleConfirm(r.id)} title={t("admin.status.confirmed")}>
|
||||
<Check className="h-4 w-4 text-primary" />
|
||||
{updatingId === r.id ? (
|
||||
<Button variant="ghost" size="icon" disabled>
|
||||
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
|
||||
</Button>
|
||||
) : (
|
||||
<>
|
||||
{r.status !== "confirmed" && (
|
||||
<Button variant="ghost" size="icon" onClick={() => handleConfirm(r.id)} title={t("admin.status.confirmed")}>
|
||||
<Check className="h-4 w-4 text-primary" />
|
||||
</Button>
|
||||
)}
|
||||
{r.status !== "cancelled" && (
|
||||
<Button variant="ghost" size="icon" onClick={() => handleCancel(r.id)} title={t("admin.status.cancelled")}>
|
||||
<X className="h-4 w-4 text-muted-foreground" />
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="ghost" size="icon" onClick={() => setDeleteId(r.id)} title={t("admin.delete")}>
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
{r.status !== "cancelled" && (
|
||||
<Button variant="ghost" size="icon" onClick={() => handleCancel(r.id)} title={t("admin.status.cancelled")}>
|
||||
<X className="h-4 w-4 text-muted-foreground" />
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="ghost" size="icon" onClick={() => setDeleteId(r.id)} title={t("admin.delete")}>
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
|
||||
@@ -15,7 +15,7 @@ type Mode = "login" | "register" | "forgot";
|
||||
|
||||
export default function Auth() {
|
||||
const { t } = useLanguage();
|
||||
const { login, register, user } = useAuth();
|
||||
const { login, register, logout, user } = useAuth();
|
||||
const router = useRouter();
|
||||
const [mode, setMode] = useState<Mode>("login");
|
||||
const [email, setEmail] = useState("");
|
||||
@@ -24,9 +24,7 @@ export default function Auth() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
router.replace(user.role === "admin" ? "/admin" : "/");
|
||||
}
|
||||
if (user) router.replace("/");
|
||||
}, [user, router]);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
@@ -35,13 +33,18 @@ export default function Auth() {
|
||||
try {
|
||||
if (mode === "login") {
|
||||
const profile = await login(email, password);
|
||||
if (profile.role === "admin") {
|
||||
logout();
|
||||
toast.error(t("auth.admin_not_allowed"));
|
||||
return;
|
||||
}
|
||||
toast.success(t("auth.login_success"));
|
||||
router.push(profile.role === "admin" ? "/admin" : "/");
|
||||
router.push("/");
|
||||
} else if (mode === "register") {
|
||||
const profile = await register(email, password, name);
|
||||
if (profile) {
|
||||
toast.success(t("auth.register_success"));
|
||||
router.push(profile.role === "admin" ? "/admin" : "/");
|
||||
router.push("/");
|
||||
} else {
|
||||
toast.success(t("auth.confirm_email"));
|
||||
router.push("/connexion");
|
||||
|
||||
@@ -18,7 +18,7 @@ import { toast } from "sonner";
|
||||
import { User, CalendarDays, ShoppingBag, X } from "lucide-react";
|
||||
|
||||
export default function MonCompte() {
|
||||
const { user, isLoading } = useAuth();
|
||||
const { user, isLoading, refreshUser } = useAuth();
|
||||
const { t, locale } = useLanguage();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -39,6 +39,12 @@ export default function MonCompte() {
|
||||
if (!isLoading && user?.role === "admin") router.replace("/admin");
|
||||
}, [user, isLoading, router]);
|
||||
|
||||
// Refresh profile from server on mount to get the latest full_name
|
||||
useEffect(() => {
|
||||
if (user) refreshUser().catch(() => {});
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
startTransition(() => {
|
||||
@@ -65,6 +71,7 @@ export default function MonCompte() {
|
||||
setSavingProfile(true);
|
||||
try {
|
||||
await updateProfile(name, phone || null);
|
||||
await refreshUser();
|
||||
toast.success(t("account.profile_saved"));
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : t("auth.error"));
|
||||
|
||||
Reference in New Issue
Block a user