"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { apiFetch, ApiError } from "@/lib/api"; import { AuthUser, Branch } from "@/lib/types"; import { useToast } from "@/components/ui/Toast"; import { SkeletonList } from "@/components/ui/Skeleton"; import EmptyState from "@/components/ui/EmptyState"; export default function BranchesPage() { const router = useRouter(); const { show } = useToast(); const [me, setMe] = useState(null); const [branches, setBranches] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [saving, setSaving] = useState(false); const [branchName, setBranchName] = useState(""); const [locationCity, setLocationCity] = useState(""); useEffect(() => { (async () => { try { const result = await apiFetch<{ user: AuthUser }>("/api/auth/me"); if (result.user.role === "DELIVERY") { router.replace("/terminal"); return; } if (result.user.role !== "SUPER_ADMIN") { router.replace("/admin/dashboard"); return; } setMe(result.user); } catch { router.replace("/login"); return; } await loadBranches(); setLoading(false); })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); async function loadBranches() { const result = await apiFetch<{ branches: Branch[] }>("/api/branches"); setBranches(result.branches); } async function handleCreate(e: React.FormEvent) { e.preventDefault(); setError(null); if (!branchName.trim() || !locationCity.trim()) { setError("Enter both a branch name and a city."); return; } setSaving(true); try { await apiFetch("/api/branches", { method: "POST", body: JSON.stringify({ branchName: branchName.trim(), locationCity: locationCity.trim() }), }); show(branchName.trim() + " created", "success"); setBranchName(""); setLocationCity(""); await loadBranches(); } catch (err) { const message = err instanceof ApiError ? err.message : "Could not create branch"; setError(message); show(message, "error"); } setSaving(false); } if (!me) { return (
); } return (

Vessel

Branches

← Dashboard
{error && (
{error}
)} {loading ? ( ) : (

All branches

Create a branch here first, then head to{" "} Staff {" "} to assign it a Branch Manager. That manager can then add their own delivery staff — you won't need to touch this page again for day-to-day hiring.

{branches.length === 0 ? ( document.getElementById("branch-name")?.focus() }} /> ) : (
    {branches.map((b) => (
  • {b.branchName}

    {b.locationCity}

    {b.managerId ? "Has manager" : "No manager yet"}
  • ))}
)}
)}

Add a new branch

setBranchName(e.target.value)} placeholder="e.g. CBD Hub" className="tap-target w-full rounded-xl border border-depth-200 px-3" />
setLocationCity(e.target.value)} placeholder="e.g. Nairobi" className="tap-target w-full rounded-xl border border-depth-200 px-3" />
); }