From 8461a23adca9fce7324d6d616dc0796347eca640 Mon Sep 17 00:00:00 2001 From: dhinesh-m24 Date: Tue, 3 Feb 2026 13:08:13 +0530 Subject: [PATCH] feat: Implement a form to add staff --- .../features/workforce/directory/AddStaff.tsx | 260 ++++++++++++++++-- 1 file changed, 231 insertions(+), 29 deletions(-) diff --git a/apps/web/src/features/workforce/directory/AddStaff.tsx b/apps/web/src/features/workforce/directory/AddStaff.tsx index e22a1b70..a01e166c 100644 --- a/apps/web/src/features/workforce/directory/AddStaff.tsx +++ b/apps/web/src/features/workforce/directory/AddStaff.tsx @@ -1,38 +1,82 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { Button } from "@/common/components/ui/button"; -import { ArrowLeft } from "lucide-react"; -import StaffForm from "./components/StaffForm"; +import { Input } from "@/common/components/ui/input"; +import { Label } from "@/common/components/ui/label"; +import { ArrowLeft, Loader2, Save, Mail, Phone, User, Award, ShieldAlert } from "lucide-react"; import DashboardLayout from "@/features/layouts/DashboardLayout"; -import { useCreateStaff } from "@/dataconnect-generated/react"; -import { dataConnect } from "@/features/auth/firebase"; -import type { Staff } from "../type"; +import { useCreateStaff, useGetUserById } from "@/dataconnect-generated/react"; +import { dataConnect, auth } from "@/features/auth/firebase"; +import { useForm, Controller } from "react-hook-form"; +import { Checkbox } from "@/common/components/ui/checkbox"; + +const COMMON_SKILLS = [ + "Barista", + "Server", + "Cook", + "Dishwasher", + "Bartender", + "Manager" +]; + +interface AddStaffFormData { + firstName: string; + lastName: string; + email: string; + phone: string; + skills: string[]; +} export default function AddStaff() { const navigate = useNavigate(); const [isSubmitting, setIsSubmitting] = useState(false); + + // Get current user and their role from DataConnect + const currentUser = auth.currentUser; + const { data: userData, isLoading: isUserLoading } = useGetUserById( + dataConnect, + { id: currentUser?.uid || "" }, + { enabled: !!currentUser } + ); + const { mutateAsync: createStaff } = useCreateStaff(dataConnect); - const handleSubmit = async (staffData: Omit) => { + const { register, handleSubmit, control, formState: { errors } } = useForm({ + defaultValues: { + firstName: "", + lastName: "", + email: "", + phone: "", + skills: [] + } + }); + + // Check for admin role + const isAdmin = userData?.user?.userRole?.toLowerCase() === 'admin'; + + useEffect(() => { + if (!isUserLoading && !isAdmin && currentUser) { + // Small delay to allow user to see why they are being redirected if needed, + // but usually immediate is better for security + const timer = setTimeout(() => { + navigate("/staff"); + }, 2000); + return () => clearTimeout(timer); + } + }, [isAdmin, isUserLoading, navigate, currentUser]); + + const onSubmit = async (data: AddStaffFormData) => { + if (!isAdmin) return; setIsSubmitting(true); try { await createStaff({ - userId: `user_${Math.random().toString(36).substr(2, 9)}`, // Placeholder UID - fullName: staffData.employee_name, - role: staffData.position, - level: staffData.profile_type, - email: staffData.email, - phone: staffData.contact_number, - photoUrl: staffData.photo, - initial: staffData.initial, - bio: staffData.notes, - skills: staffData.skills, - averageRating: staffData.averageRating, - reliabilityScore: staffData.reliability_score, - onTimeRate: staffData.shift_coverage_percentage, - totalShifts: staffData.total_shifts, - city: staffData.city, - addres: staffData.address, + userId: `user_${Math.random().toString(36).substring(2, 11)}`, + fullName: `${data.firstName} ${data.lastName}`, + email: data.email, + phone: data.phone, + skills: data.skills, + backgroundCheckStatus: "PENDING", + initial: `${data.firstName.charAt(0)}${data.lastName.charAt(0)}`.toUpperCase(), }); navigate("/staff"); } catch (error) { @@ -42,10 +86,43 @@ export default function AddStaff() { } }; + if (isUserLoading) { + return ( +
+ +

VERIFYING PERMISSIONS...

+
+ ); + } + + if (!isAdmin) { + return ( +
+
+ +
+
+

Access Denied

+

+ Only administrators can manually onboard new staff members. + You are being redirected to the staff directory. +

+
+ +
+ ); + } + return ( } > - +
+
+
+ {/* Identity Section */} +
+

+ + Staff Identity +

+
+
+ + + {errors.firstName &&

{errors.firstName.message}

} +
+
+ + + {errors.lastName &&

{errors.lastName.message}

} +
+
+
+ + {/* Contact Section */} +
+

+ + Contact Information +

+
+
+ + } + placeholder="john.doe@example.com" + className="rounded-xl" + /> + {errors.email &&

{errors.email.message}

} +
+
+ + } + placeholder="+1 (555) 000-0000" + className="rounded-xl" + /> + {errors.phone &&

{errors.phone.message}

} +
+
+
+ + {/* Skills Section */} +
+

+ + Selection of Skills +

+
+ {COMMON_SKILLS.map((skill) => ( + ( +
+ { + const checked = e.target.checked; + const updatedSkills = checked + ? [...(field.value || []), skill] + : field.value?.filter((s: string) => s !== skill); + field.onChange(updatedSkills); + }} + /> + +
+ )} + /> + ))} +
+
+ + {/* Action Buttons */} +
+ + +
+
+
+
); }