import { useState, useEffect } from "react"; import axios from "axios"; interface ApiResponse { success: boolean; data: T; message?: string; } export interface Department { id: string; name: string; positions: any[]; isExpanded?: boolean; } export interface Unit { id: string; name: string; departments: Department[]; isExpanded?: boolean; } export const useUnits = (organizationId: string) => { const [units, setUnits] = useState([]); const [selectedUnitId, setSelectedUnitId] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Fetch units for an organization const fetchUnits = async () => { if (!organizationId) { setUnits([]); setSelectedUnitId(""); return; } setLoading(true); }; // Create a new unit const createUnit = async (unitData: Partial): Promise => { if (!organizationId) { throw new Error("No organization selected"); } setLoading(true); try { // In a real app, this would be an API call // const response = await axios.post>( // `/api/organizations/${organizationId}/units`, // unitData // ); // const newUnit = response.data.data; // For now, simulate API response const newUnit: Unit = { id: `unit-${Date.now()}`, name: unitData.name || "New Unit", departments: [], isExpanded: false, }; setUnits((prev) => [...prev, newUnit]); setLoading(false); return newUnit; } catch (err) { setError(err instanceof Error ? err : new Error("Failed to create unit")); setLoading(false); throw err; } }; // Update an existing unit const updateUnit = async ( unitId: string, updates: Partial ): Promise => { setLoading(true); try { // In a real app, this would be an API call // const response = await axios.put>( // `/api/units/${unitId}`, // updates // ); // const updatedUnit = response.data.data; // For now, simulate API response const updatedUnits = units.map((unit) => unit.id === unitId ? { ...unit, ...updates } : unit ); setUnits(updatedUnits); setLoading(false); const updatedUnit = updatedUnits.find((u) => u.id === unitId); if (!updatedUnit) { throw new Error("Unit not found after update"); } return updatedUnit; } catch (err) { setError(err instanceof Error ? err : new Error("Failed to update unit")); setLoading(false); throw err; } }; // Delete a unit const deleteUnit = async (unitId: string): Promise => { setLoading(true); try { // In a real app, this would be an API call // await axios.delete>(`/api/units/${unitId}`); // For now, simulate API response setUnits((prev) => prev.filter((unit) => unit.id !== unitId)); // If the deleted unit was selected, select another unit if (selectedUnitId === unitId) { const remaining = units.filter((unit) => unit.id !== unitId); if (remaining.length > 0) { setSelectedUnitId(remaining[0].id); } else { setSelectedUnitId(""); } } setLoading(false); } catch (err) { setError(err instanceof Error ? err : new Error("Failed to delete unit")); setLoading(false); throw err; } }; // Add department to a unit const addDepartment = async ( unitId: string, departmentName: string ): Promise => { setLoading(true); try { // In a real app, this would be an API call // const response = await axios.post>( // `/api/units/${unitId}/departments`, // { name: departmentName } // ); // const newDepartment = response.data.data; // For now, simulate API response const newDepartment: Department = { id: `dept-${Date.now()}`, name: departmentName, positions: [], isExpanded: false, }; setUnits((prev) => prev.map((unit) => unit.id === unitId ? { ...unit, departments: [...unit.departments, newDepartment] } : unit ) ); setLoading(false); return newDepartment; } catch (err) { setError( err instanceof Error ? err : new Error("Failed to add department") ); setLoading(false); throw err; } }; // Toggle expand/collapse for a unit const toggleUnitExpand = (unitId: string) => { setUnits((prev) => prev.map((unit) => unit.id === unitId ? { ...unit, isExpanded: !unit.isExpanded } : unit ) ); }; // Toggle expand/collapse for a department const toggleDepartmentExpand = (unitId: string, deptId: string) => { setUnits((prev) => prev.map((unit) => { if (unit.id !== unitId) return unit; return { ...unit, departments: unit.departments.map((dept) => dept.id === deptId ? { ...dept, isExpanded: !dept.isExpanded } : dept ), }; }) ); }; // Select a unit const selectUnit = (unitId: string) => { setSelectedUnitId(unitId); }; // Load units when organizationId changes useEffect(() => { fetchUnits(); }, [organizationId]); return { units, selectedUnitId, selectedUnit: units.find((unit) => unit.id === selectedUnitId), loading, error, fetchUnits, createUnit, updateUnit, deleteUnit, addDepartment, toggleUnitExpand, toggleDepartmentExpand, selectUnit, }; };