staff section done, user to be
This commit is contained in:
@@ -11,18 +11,28 @@ interface StaffFormProps {
|
|||||||
isLoading?: boolean;
|
isLoading?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function StaffForm({ initialData, onSubmit, onCancel, isLoading }: StaffFormProps) {
|
export function StaffForm({
|
||||||
const [name, setName] = useState(initialData?.name || "");
|
initialData,
|
||||||
const [email, setEmail] = useState(initialData?.email || "");
|
onSubmit,
|
||||||
const [role, setRole] = useState(initialData?.role || "Staff");
|
onCancel,
|
||||||
const [phone, setPhone] = useState(initialData?.phone || "");
|
isLoading,
|
||||||
|
}: StaffFormProps) {
|
||||||
|
const [name, setName] = useState("");
|
||||||
|
const [email, setEmail] = useState("");
|
||||||
|
const [role, setRole] = useState("Staff");
|
||||||
|
const [phone, setPhone] = useState("");
|
||||||
|
|
||||||
|
const [hasTypedRole, setHasTypedRole] = useState(false);
|
||||||
|
|
||||||
|
// Set initial values once on mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setName(initialData?.name || "");
|
if (initialData) {
|
||||||
setEmail(initialData?.email || "");
|
if (initialData.name) setName(initialData.name);
|
||||||
setRole(initialData?.role || "Staff");
|
if (initialData.email) setEmail(initialData.email);
|
||||||
setPhone(initialData?.phone || "");
|
if (initialData.role) setRole(initialData.role);
|
||||||
}, [initialData]);
|
if (initialData.phone) setPhone(initialData.phone);
|
||||||
|
}
|
||||||
|
}, []); // run once only
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -30,13 +40,21 @@ export function StaffForm({ initialData, onSubmit, onCancel, isLoading }: StaffF
|
|||||||
alert("Name is required");
|
alert("Name is required");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onSubmit({ name, email: email || undefined, role, phone: phone || undefined });
|
|
||||||
|
onSubmit({
|
||||||
|
name: name.trim(),
|
||||||
|
email: email.trim() || undefined,
|
||||||
|
role: role.trim(),
|
||||||
|
phone: phone.trim() || undefined,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={handleSubmit} className="space-y-4">
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700">Name *</label>
|
<label className="block text-sm font-medium text-gray-700">
|
||||||
|
Name *
|
||||||
|
</label>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
className="mt-1 block w-full border rounded p-2"
|
className="mt-1 block w-full border rounded p-2"
|
||||||
@@ -46,6 +64,7 @@ export function StaffForm({ initialData, onSubmit, onCancel, isLoading }: StaffF
|
|||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700">Email</label>
|
<label className="block text-sm font-medium text-gray-700">Email</label>
|
||||||
<input
|
<input
|
||||||
@@ -56,17 +75,29 @@ export function StaffForm({ initialData, onSubmit, onCancel, isLoading }: StaffF
|
|||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700">Role *</label>
|
<label className="block text-sm font-medium text-gray-700">
|
||||||
|
Role *
|
||||||
|
</label>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
className="mt-1 block w-full border rounded p-2"
|
className="mt-1 block w-full border rounded p-2"
|
||||||
value={role}
|
value={role}
|
||||||
onChange={(e) => setRole(e.target.value)}
|
onChange={(e) => {
|
||||||
|
setHasTypedRole(true);
|
||||||
|
setRole(e.target.value);
|
||||||
|
}}
|
||||||
|
onFocus={() => {
|
||||||
|
if (!hasTypedRole && role === "Staff") {
|
||||||
|
setRole("");
|
||||||
|
}
|
||||||
|
}}
|
||||||
required
|
required
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-700">Phone</label>
|
<label className="block text-sm font-medium text-gray-700">Phone</label>
|
||||||
<input
|
<input
|
||||||
@@ -77,6 +108,7 @@ export function StaffForm({ initialData, onSubmit, onCancel, isLoading }: StaffF
|
|||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex justify-end space-x-2">
|
<div className="flex justify-end space-x-2">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { useQuery, useMutation } from "@tanstack/react-query";
|
import { useQuery, useMutation } from "@tanstack/react-query";
|
||||||
import { TopAppBar } from "@/components/layout/top-app-bar";
|
import { TopAppBar } from "@/components/layout/top-app-bar";
|
||||||
import { Sidebar } from "@/components/layout/sidebar";
|
import { Sidebar } from "@/components/layout/sidebar";
|
||||||
@@ -8,13 +8,19 @@ import { Card, CardContent } from "@/components/ui/card";
|
|||||||
import { StaffUncheckedCreateInputObjectSchema } from "@repo/db/shared/schemas";
|
import { StaffUncheckedCreateInputObjectSchema } from "@repo/db/shared/schemas";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { apiRequest, queryClient } from "@/lib/queryClient";
|
import { apiRequest, queryClient } from "@/lib/queryClient";
|
||||||
|
import { StaffForm } from "@/components/staffs/staff-form";
|
||||||
|
|
||||||
|
// Correctly infer Staff type from zod schema
|
||||||
type Staff = z.infer<typeof StaffUncheckedCreateInputObjectSchema>;
|
type Staff = z.infer<typeof StaffUncheckedCreateInputObjectSchema>;
|
||||||
|
|
||||||
export default function SettingsPage() {
|
export default function SettingsPage() {
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
||||||
|
|
||||||
|
// Modal and editing staff state
|
||||||
|
const [modalOpen, setModalOpen] = useState(false);
|
||||||
|
const [editingStaff, setEditingStaff] = useState<Staff | null>(null);
|
||||||
|
|
||||||
const toggleMobileMenu = () => setIsMobileMenuOpen((prev) => !prev);
|
const toggleMobileMenu = () => setIsMobileMenuOpen((prev) => !prev);
|
||||||
|
|
||||||
// Fetch staff data
|
// Fetch staff data
|
||||||
@@ -35,8 +41,12 @@ export default function SettingsPage() {
|
|||||||
staleTime: 1000 * 60 * 5, // 5 minutes cache
|
staleTime: 1000 * 60 * 5, // 5 minutes cache
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add Staff Mutation
|
// Add Staff mutation
|
||||||
const addStaffMutation = useMutation({
|
const addStaffMutate = useMutation<
|
||||||
|
Staff, // Return type
|
||||||
|
Error, // Error type
|
||||||
|
Omit<Staff, "id" | "createdAt"> // Variables
|
||||||
|
>({
|
||||||
mutationFn: async (newStaff: Omit<Staff, "id" | "createdAt">) => {
|
mutationFn: async (newStaff: Omit<Staff, "id" | "createdAt">) => {
|
||||||
const res = await apiRequest("POST", "/api/staffs/", newStaff);
|
const res = await apiRequest("POST", "/api/staffs/", newStaff);
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@@ -62,8 +72,12 @@ export default function SettingsPage() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update Staff Mutation
|
// Update Staff mutation
|
||||||
const updateStaffMutation = useMutation({
|
const updateStaffMutate = useMutation<
|
||||||
|
Staff,
|
||||||
|
Error,
|
||||||
|
{ id: number; updatedFields: Partial<Staff> }
|
||||||
|
>({
|
||||||
mutationFn: async ({
|
mutationFn: async ({
|
||||||
id,
|
id,
|
||||||
updatedFields,
|
updatedFields,
|
||||||
@@ -95,8 +109,8 @@ export default function SettingsPage() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Delete Staff Mutation
|
// Delete Staff mutation
|
||||||
const deleteStaffMutation = useMutation({
|
const deleteStaffMutation = useMutation<number, Error, number>({
|
||||||
mutationFn: async (id: number) => {
|
mutationFn: async (id: number) => {
|
||||||
const res = await apiRequest("DELETE", `/api/staffs/${id}`);
|
const res = await apiRequest("DELETE", `/api/staffs/${id}`);
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
@@ -122,69 +136,117 @@ export default function SettingsPage() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handlers for prompts and mutations
|
// Extract mutation states for modal control and loading
|
||||||
|
|
||||||
|
const isAdding = addStaffMutate.status === "pending";
|
||||||
|
const isAddSuccess = addStaffMutate.status === "success";
|
||||||
|
|
||||||
|
const isUpdating = updateStaffMutate.status === "pending";
|
||||||
|
const isUpdateSuccess = updateStaffMutate.status === "success";
|
||||||
|
|
||||||
|
// Open Add modal
|
||||||
const openAddStaffModal = () => {
|
const openAddStaffModal = () => {
|
||||||
const name = prompt("Enter staff name:");
|
setEditingStaff(null);
|
||||||
if (!name) return;
|
setModalOpen(true);
|
||||||
const email = prompt("Enter staff email (optional):") || undefined;
|
|
||||||
const role = prompt("Enter staff role:") || "Staff";
|
|
||||||
const phone = prompt("Enter staff phone (optional):") || undefined;
|
|
||||||
addStaffMutation.mutate({ name, email, role, phone });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Open Edit modal
|
||||||
const openEditStaffModal = (staff: Staff) => {
|
const openEditStaffModal = (staff: Staff) => {
|
||||||
if (typeof staff.id !== "number") {
|
setEditingStaff(staff);
|
||||||
toast({
|
setModalOpen(true);
|
||||||
title: "Error",
|
|
||||||
description: "Staff ID is missing",
|
|
||||||
variant: "destructive",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const name = prompt("Edit staff name:", staff.name);
|
|
||||||
if (!name) return;
|
|
||||||
const email = prompt("Edit staff email:", staff.email || "") || undefined;
|
|
||||||
const role = prompt("Edit staff role:", staff.role || "Staff") || "Staff";
|
|
||||||
const phone = prompt("Edit staff phone:", staff.phone || "") || undefined;
|
|
||||||
updateStaffMutation.mutate({
|
|
||||||
id: staff.id,
|
|
||||||
updatedFields: { name, email, role, phone },
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Handle form submit for Add or Edit
|
||||||
|
const handleFormSubmit = (formData: Omit<Staff, "id" | "createdAt">) => {
|
||||||
|
if (editingStaff) {
|
||||||
|
// Editing existing staff
|
||||||
|
if (editingStaff.id === undefined) {
|
||||||
|
toast({
|
||||||
|
title: "Error",
|
||||||
|
description: "Staff ID is missing",
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
updateStaffMutate.mutate({
|
||||||
|
id: editingStaff.id,
|
||||||
|
updatedFields: formData,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
addStaffMutate.mutate(formData);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleModalCancel = () => {
|
||||||
|
setModalOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Close modal on successful add/update
|
||||||
|
useEffect(() => {
|
||||||
|
if (isAddSuccess || isUpdateSuccess) {
|
||||||
|
setModalOpen(false);
|
||||||
|
}
|
||||||
|
}, [isAddSuccess, isUpdateSuccess]);
|
||||||
|
|
||||||
|
// Delete staff
|
||||||
const handleDeleteStaff = (id: number) => {
|
const handleDeleteStaff = (id: number) => {
|
||||||
if (confirm("Are you sure you want to delete this staff member?")) {
|
if (confirm("Are you sure you want to delete this staff member?")) {
|
||||||
deleteStaffMutation.mutate(id);
|
deleteStaffMutation.mutate(id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// View staff handler (just an alert for now)
|
||||||
const handleViewStaff = (staff: Staff) =>
|
const handleViewStaff = (staff: Staff) =>
|
||||||
alert(`Viewing staff member:\n${staff.name} (${staff.email || "No email"})`);
|
alert(
|
||||||
|
`Viewing staff member:\n${staff.name} (${staff.email || "No email"})`
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen overflow-hidden bg-gray-100">
|
<div className="flex h-screen overflow-hidden bg-gray-100">
|
||||||
<Sidebar isMobileOpen={isMobileMenuOpen} setIsMobileOpen={setIsMobileMenuOpen} />
|
<Sidebar
|
||||||
|
isMobileOpen={isMobileMenuOpen}
|
||||||
|
setIsMobileOpen={setIsMobileMenuOpen}
|
||||||
|
/>
|
||||||
<div className="flex-1 flex flex-col overflow-hidden">
|
<div className="flex-1 flex flex-col overflow-hidden">
|
||||||
<TopAppBar toggleMobileMenu={toggleMobileMenu} />
|
<TopAppBar toggleMobileMenu={toggleMobileMenu} />
|
||||||
<main className="flex-1 overflow-y-auto p-4">
|
<main className="flex-1 overflow-y-auto p-2">
|
||||||
<Card>
|
<Card>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<StaffTable
|
<div className="mt-8">
|
||||||
staff={staff}
|
<StaffTable
|
||||||
isLoading={isLoading}
|
staff={staff}
|
||||||
isError={isError}
|
isLoading={isLoading}
|
||||||
onAdd={openAddStaffModal}
|
isError={isError}
|
||||||
onEdit={openEditStaffModal}
|
onAdd={openAddStaffModal}
|
||||||
onDelete={handleDeleteStaff}
|
onEdit={openEditStaffModal}
|
||||||
onView={handleViewStaff}
|
onDelete={handleDeleteStaff}
|
||||||
/>
|
onView={handleViewStaff}
|
||||||
{isError && (
|
/>
|
||||||
<p className="mt-4 text-red-600">
|
{isError && (
|
||||||
{(error as Error)?.message || "Failed to load staff data."}
|
<p className="mt-4 text-red-600">
|
||||||
</p>
|
{(error as Error)?.message || "Failed to load staff data."}
|
||||||
)}
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
{/* Modal Overlay */}
|
||||||
|
{modalOpen && (
|
||||||
|
<div className="fixed inset-0 bg-black bg-opacity-50 flex justify-center items-center z-50">
|
||||||
|
<div className="bg-white rounded-lg p-6 w-full max-w-md shadow-lg">
|
||||||
|
<h2 className="text-lg font-bold mb-4">
|
||||||
|
{editingStaff ? "Edit Staff" : "Add Staff"}
|
||||||
|
</h2>
|
||||||
|
<StaffForm
|
||||||
|
initialData={editingStaff || undefined}
|
||||||
|
onSubmit={handleFormSubmit}
|
||||||
|
onCancel={handleModalCancel}
|
||||||
|
isLoading={isAdding || isUpdating}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user