stats and settings
This commit is contained in:
parent
bd30190f96
commit
4d229d0b94
193
src/pages/ManageUsersPage.tsx
Normal file
193
src/pages/ManageUsersPage.tsx
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
import { useCallback, useEffect, useState } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent} from "@/components/ui/card";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import { Trash2 } from "lucide-react";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
import { HotelStaffRole } from "@/lib/types";
|
||||
import type { RegisterHotelStaffDto, StaffAccess } from "@/lib/types";
|
||||
import { getStaffAccess, postStaff, deleteStaffAccess } from "@/lib/auth-api";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
|
||||
export function ManageUsersPage() {
|
||||
const token = useAuthStore((s) => s.accessToken);
|
||||
const selectedPropertyId = useAuthStore((s) => s.selectedPropertyId);
|
||||
const [staff, setStaff] = useState<StaffAccess[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [formData, setFormData] = useState<RegisterHotelStaffDto>({
|
||||
name: "",
|
||||
email: "",
|
||||
phone: "",
|
||||
password: "",
|
||||
hotelRole: HotelStaffRole.FRONT_DESK,
|
||||
});
|
||||
|
||||
const loadStaff = useCallback(async () => {
|
||||
if (!token) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await getStaffAccess(token);
|
||||
setStaff(data);
|
||||
} catch (error) {
|
||||
console.error("Failed to load staff", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [token, selectedPropertyId]);
|
||||
|
||||
useEffect(() => {
|
||||
loadStaff();
|
||||
}, [loadStaff]);
|
||||
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData({ ...formData, [name]: value });
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!token) return;
|
||||
setSubmitting(true);
|
||||
try {
|
||||
await postStaff(token, formData);
|
||||
setOpen(false);
|
||||
setFormData({ name: "", email: "", phone: "", password: "", hotelRole: HotelStaffRole.FRONT_DESK });
|
||||
loadStaff();
|
||||
} catch (error) {
|
||||
console.error("Failed to create staff", error);
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!token || !confirm("Are you sure you want to delete this user?")) return;
|
||||
try {
|
||||
await deleteStaffAccess(token, id);
|
||||
loadStaff();
|
||||
} catch (error) {
|
||||
console.error("Failed to delete staff", error);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) return (
|
||||
<div className="flex min-h-[400px] items-center justify-center">
|
||||
<Spinner size={32} />
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold">Manage Users</h1>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button>+ Add User</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Add New Staff Member</DialogTitle>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="name">Name</Label>
|
||||
<Input id="name" name="name" value={formData.name} onChange={handleInputChange} required />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="email">Email</Label>
|
||||
<Input id="email" name="email" type="email" value={formData.email} onChange={handleInputChange} required />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="phone">Phone (optional)</Label>
|
||||
<Input id="phone" name="phone" value={formData.phone} onChange={handleInputChange} />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<Input id="password" name="password" type="password" value={formData.password} onChange={handleInputChange} minLength={6} required />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="hotelRole">Role</Label>
|
||||
<Select name="hotelRole" value={formData.hotelRole} onValueChange={(v) => setFormData({ ...formData, hotelRole: v as HotelStaffRole })}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={HotelStaffRole.FRONT_DESK}>Front Desk</SelectItem>
|
||||
<SelectItem value={HotelStaffRole.FINANCE}>Finance</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<Button type="submit" className="w-full" loading={submitting}>Create User</Button>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
<Card className="rounded-2xl">
|
||||
<CardContent className="pt-6">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead>Email</TableHead>
|
||||
<TableHead>Phone</TableHead>
|
||||
<TableHead>Role</TableHead>
|
||||
<TableHead>Created</TableHead>
|
||||
<TableHead>Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{staff.map((user) => (
|
||||
<TableRow key={user.id}>
|
||||
<TableCell className="font-medium">{user.user.name}</TableCell>
|
||||
<TableCell>{user.user.email}</TableCell>
|
||||
<TableCell>{user.user.phone || "-"}</TableCell>
|
||||
<TableCell>{user.user.role}</TableCell>
|
||||
<TableCell>{new Date(user.createdAt).toLocaleDateString()}</TableCell>
|
||||
<TableCell>
|
||||
<Button variant="ghost" size="icon" onClick={() => handleDelete(user.id)}>
|
||||
<Trash2 className="size-4" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
{staff.length === 0 && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={6} className="h-24 text-center text-muted-foreground">
|
||||
No staff members found. Add one above.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -1,19 +1,54 @@
|
|||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { TAX_RATE } from "@/lib/constants";
|
||||
import { ChevronRight, Users } from "lucide-react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
export function SettingsPage() {
|
||||
|
||||
const menuItems = [
|
||||
{
|
||||
label: "Manage Users",
|
||||
path: "/settings/users",
|
||||
icon: <Users className="size-4" />,
|
||||
description: "View and manage hotel staff members"
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<h1 className="text-2xl font-bold">Settings</h1>
|
||||
<Card className="rounded-2xl">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">Property (mock)</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="text-sm text-muted-foreground space-y-2">
|
||||
<p>Tax rate used in MSW pricing: {(TAX_RATE * 100).toFixed(0)}%</p>
|
||||
<p>Connect real backend, auth, and PSP in a future phase.</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
<section className="bg-slate-50 min-h-screen p-6">
|
||||
<div className="max-w-3xl mx-auto">
|
||||
<div className="grid grid-cols-1 gap-4">
|
||||
|
||||
<div className="py-3" />
|
||||
|
||||
<div>
|
||||
<div className="bg-white rounded-2xl border border-blue-200 shadow-sm overflow-hidden">
|
||||
{menuItems.map((item) => (
|
||||
<Link
|
||||
to={item.path}
|
||||
key={item.label}
|
||||
className="flex items-center justify-between px-6 py-6 border-b border-blue-100 last:border-b-0 hover:bg-blue-50 transition-all duration-200"
|
||||
>
|
||||
<div className="flex items-center space-x-4">
|
||||
<div className="bg-gradient-to-r from-blue-500 to-blue-600 text-white rounded-xl p-3 shadow-md">
|
||||
{item.icon}
|
||||
</div>
|
||||
<div>
|
||||
<span className="text-sm font-semibold text-gray-800 block leading-tight">
|
||||
{item.label}
|
||||
</span>
|
||||
<span className="text-xs text-gray-500 block mt-1">
|
||||
{item.description}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-gradient-to-r from-blue-500 to-blue-600 text-white rounded-xl p-2 shadow-md flex-shrink-0">
|
||||
<ChevronRight className="size-5" />
|
||||
</div>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,9 +31,13 @@ export function VisitsPage() {
|
|||
|
||||
const refresh = useCallback(async () => {
|
||||
const v = await apiGet<{
|
||||
series: { date: string; views: number; sessions: number }[];
|
||||
series: Array<{ date: string; count: number }>;
|
||||
}>("/analytics/visits");
|
||||
setSeries(v.series.slice(-21));
|
||||
setSeries(v.series.map((item) => ({
|
||||
date: item.date,
|
||||
views: item.count || 0,
|
||||
sessions: 0
|
||||
})).slice(-21));
|
||||
const r = await apiGet<{ data: SiteVisit[] }>("/analytics/visits/recent");
|
||||
setRecent(r.data);
|
||||
}, []);
|
||||
|
|
@ -45,7 +49,7 @@ export function VisitsPage() {
|
|||
async function simulateHit() {
|
||||
await apiPost("/analytics/visits", {
|
||||
path: "/booking",
|
||||
device: "desktop",
|
||||
// device: "desktop",
|
||||
});
|
||||
await refresh();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user