Compare commits

...

2 Commits

6 changed files with 377 additions and 2 deletions

View File

@ -0,0 +1,14 @@
[
{ "id": "CMP001", "name": "CLRI", "status": "Active" },
{ "id": "CMP002", "name": "TechNova Solutions", "status": "Active" },
{ "id": "CMP003", "name": "Infotech Ltd", "status": "Disabled" },
{ "id": "CMP004", "name": "Global Systems", "status": "Active" },
{ "id": "CMP005", "name": "Alpha Corp", "status": "Disabled" },
{ "id": "CMP006", "name": "Beta Industries", "status": "Active" },
{ "id": "CMP007", "name": "Gamma Tech", "status": "Active" },
{ "id": "CMP008", "name": "Delta Services", "status": "Active" },
{ "id": "CMP009", "name": "Epsilon Enterprises", "status": "Disabled" },
{ "id": "CMP010", "name": "Zeta Labs", "status": "Active" },
{ "id": "CMP011", "name": "Eta Group", "status": "Active" },
{ "id": "CMP012", "name": "Theta Digital", "status": "Active" }
]

View File

@ -0,0 +1,15 @@
import { useQuery } from '@tanstack/react-query';
import { fetchMockData } from '../../../api/client';
export interface Company {
id: string;
name: string;
status: 'Active' | 'Disabled';
}
export const useCompanies = () => {
return useQuery<Company[]>({
queryKey: ['companies'],
queryFn: () => fetchMockData<Company[]>('master-data/companies.json'),
});
};

View File

@ -0,0 +1,78 @@
import { useState, useEffect } from 'react';
import { X } from 'lucide-react';
import type { Company } from '../api/useCompanyData';
interface CompanyFormModalProps {
company: Company | null; // null means Add mode, object means Edit mode
onClose: () => void;
onSave: (name: string, status: 'Active' | 'Disabled') => void;
}
export default function CompanyFormModal({ company, onClose, onSave }: CompanyFormModalProps) {
const [name, setName] = useState('');
const [status, setStatus] = useState<'Active' | 'Disabled'>('Active');
useEffect(() => {
if (company) {
// eslint-disable-next-line react-hooks/set-state-in-effect
setName(company.name);
setStatus(company.status);
} else {
setName('');
setStatus('Active');
}
}, [company]);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onSave(name, status);
};
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
<div className="bg-app-card rounded-lg shadow-xl w-full max-w-md p-6 relative">
<button onClick={onClose} className="absolute top-4 right-4 text-text-muted hover:text-text-primary">
<X size={24} />
</button>
<h3 className="text-xl font-bold text-text-primary mb-6">
{company ? 'Edit Company' : 'Add New Company'}
</h3>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">Company Name</label>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
required
className="w-full px-3 py-2 border border-app-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary text-text-secondary bg-app-card"
placeholder="Enter company name..."
/>
</div>
<div>
<label className="block text-sm font-medium text-text-secondary mb-1">Status</label>
<select
value={status}
onChange={(e) => setStatus(e.target.value as 'Active' | 'Disabled')}
className="w-full px-3 py-2 border border-app-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary text-text-secondary bg-app-card"
>
<option value="Active">Active</option>
<option value="Disabled">Disabled</option>
</select>
</div>
<div className="flex justify-end space-x-3 mt-6">
<button type="button" onClick={onClose} className="px-4 py-2 bg-app-muted text-text-primary rounded-lg hover:bg-app-border transition-colors text-sm font-medium">
Cancel
</button>
<button type="submit" className="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary-hover transition-colors text-sm font-medium">
{company ? 'Update' : 'Add'} Company
</button>
</div>
</form>
</div>
</div>
);
}

View File

@ -0,0 +1,178 @@
import { useState, useMemo } from 'react';
import { ArrowUpDown, ArrowUp, ArrowDown, Search, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight, Pencil, Trash2 } from 'lucide-react';
import type { Company } from '../api/useCompanyData';
interface CompanyTableProps {
records: Company[];
onEdit: (company: Company) => void;
onDelete: (id: string) => void;
}
type SortKey = 'id' | 'name' | 'status';
type SortDirection = 'ascending' | 'descending';
const getStatusClass = (status: string) => {
switch (status) {
case 'Active': return 'bg-present-100 text-present-700';
case 'Disabled': return 'bg-absent-100 text-absent-700';
default: return 'bg-app-muted text-text-secondary';
}
};
export default function CompanyTable({ records, onEdit, onDelete }: CompanyTableProps) {
const [searchQuery, setSearchQuery] = useState('');
const [currentPage, setCurrentPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
const [sortConfig, setSortConfig] = useState<{ key: SortKey; direction: SortDirection }>({
key: 'name',
direction: 'ascending'
});
const filteredData = useMemo(() => {
if (!searchQuery) return records;
return records.filter(record =>
record.id.toLowerCase().includes(searchQuery.toLowerCase()) ||
record.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
record.status.toLowerCase().includes(searchQuery.toLowerCase())
);
}, [records, searchQuery]);
const sortedData = useMemo(() => {
const sortableData = [...filteredData];
sortableData.sort((a, b) => {
if (a[sortConfig.key] < b[sortConfig.key]) return sortConfig.direction === 'ascending' ? -1 : 1;
if (a[sortConfig.key] > b[sortConfig.key]) return sortConfig.direction === 'ascending' ? 1 : -1;
return 0;
});
return sortableData;
}, [filteredData, sortConfig]);
const totalPages = Math.ceil(sortedData.length / pageSize);
const isPaginationEnabled = sortedData.length > pageSize;
const paginatedData = useMemo(() => {
const startIndex = (currentPage - 1) * pageSize;
return sortedData.slice(startIndex, startIndex + pageSize);
}, [sortedData, currentPage, pageSize]);
const pageNumbers = useMemo(() => {
const pages = [];
const maxVisiblePages = 5;
let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2));
let endPage = startPage + maxVisiblePages - 1;
if (endPage > totalPages) {
endPage = totalPages;
startPage = Math.max(1, endPage - maxVisiblePages + 1);
}
for (let i = startPage; i <= endPage; i++) pages.push(i);
return pages;
}, [currentPage, totalPages]);
const requestSort = (key: SortKey) => {
let direction: SortDirection = 'ascending';
if (sortConfig.key === key && sortConfig.direction === 'ascending') direction = 'descending';
setSortConfig({ key, direction });
setCurrentPage(1);
};
const handlePageSizeChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
setPageSize(Number(e.target.value));
setCurrentPage(1);
};
const getSortIcon = (key: SortKey) => {
if (sortConfig.key !== key) return <ArrowUpDown size={14} className="ml-1 text-text-light" />;
if (sortConfig.direction === 'ascending') return <ArrowUp size={14} className="ml-1 text-primary" />;
return <ArrowDown size={14} className="ml-1 text-primary" />;
};
return (
<div className="bg-app-card p-6 rounded-lg shadow-sm">
<div className="flex flex-col md:flex-row md:items-center justify-between mb-6 gap-4">
<h2 className="text-lg font-semibold text-text-primary">Companies List</h2>
<div className="relative w-full md:w-64">
<Search size={16} className="absolute left-3 top-1/2 -translate-y-1/2 text-text-light" />
<input
type="text"
placeholder="Search id, name, status..."
value={searchQuery}
onChange={(e) => { setSearchQuery(e.target.value); setCurrentPage(1); }}
className="pl-9 pr-3 py-2 border border-app-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary text-text-secondary bg-app-card w-full"
/>
</div>
</div>
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-app-border">
<thead>
<tr>
{(['id', 'name', 'status'] as SortKey[]).map((key) => (
<th
key={key}
onClick={() => requestSort(key)}
className="px-6 py-3 text-left text-xs font-semibold text-text-muted uppercase tracking-wider cursor-pointer hover:bg-app-muted transition-colors"
>
<div className="flex items-center">
{key === 'id' ? 'Company ID' : key.charAt(0).toUpperCase() + key.slice(1)} {getSortIcon(key)}
</div>
</th>
))}
<th className="px-6 py-3 text-left text-xs font-semibold text-text-muted uppercase tracking-wider">Action</th>
</tr>
</thead>
<tbody className="divide-y divide-app-border">
{paginatedData.length > 0 ? (
paginatedData.map((record) => (
<tr key={record.id} className="hover:bg-app-muted transition-colors">
<td className="px-6 py-4 whitespace-nowrap text-sm text-text-primary">{record.id}</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-text-secondary">{record.name}</td>
<td className="px-6 py-4 whitespace-nowrap text-sm">
<span className={`px-2 py-1 rounded-full text-xs font-medium ${getStatusClass(record.status)}`}>{record.status}</span>
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm flex space-x-3">
<button onClick={() => onEdit(record)} className="text-primary hover:text-primary-hover flex items-center">
<Pencil size={16} className="mr-1" /> Edit
</button>
<button onClick={() => onDelete(record.id)} className="text-absent-500 hover:text-absent-700 flex items-center">
<Trash2 size={16} className="mr-1" /> Delete
</button>
</td>
</tr>
))
) : (
<tr>
<td colSpan={4} className="px-6 py-8 text-center text-sm text-text-muted">No companies found.</td>
</tr>
)}
</tbody>
</table>
</div>
<div className="flex flex-col md:flex-row md:items-center justify-between mt-4 pt-4 border-t border-app-border gap-4">
<div className="flex items-center space-x-2">
<span className="text-sm text-text-muted">Show</span>
<select value={pageSize} onChange={handlePageSizeChange} className="px-2 py-1 border border-app-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary text-text-secondary bg-app-card">
<option value={10}>10</option>
<option value={15}>15</option>
<option value={20}>20</option>
</select>
<span className="text-sm text-text-muted">entries</span>
</div>
{isPaginationEnabled && totalPages > 1 && (
<div className="flex items-center space-x-1">
<span className="text-sm text-text-muted mr-2 hidden sm:inline">Page {currentPage} of {totalPages}</span>
<button onClick={() => setCurrentPage(1)} disabled={currentPage === 1} className="p-2 border border-app-border rounded-lg text-text-secondary hover:bg-app-muted disabled:opacity-50 disabled:cursor-not-allowed transition-colors"><ChevronsLeft size={16} /></button>
<button onClick={() => setCurrentPage(prev => Math.max(prev - 1, 1))} disabled={currentPage === 1} className="p-2 border border-app-border rounded-lg text-text-secondary hover:bg-app-muted disabled:opacity-50 disabled:cursor-not-allowed transition-colors"><ChevronLeft size={16} /></button>
{pageNumbers.map(num => (
<button key={num} onClick={() => setCurrentPage(num)} className={`w-8 h-8 flex items-center justify-center border rounded-lg text-sm transition-colors ${currentPage === num ? 'bg-primary text-white border-primary' : 'border-app-border text-text-secondary hover:bg-app-muted'}`}>{num}</button>
))}
<button onClick={() => setCurrentPage(prev => Math.min(prev + 1, totalPages))} disabled={currentPage === totalPages} className="p-2 border border-app-border rounded-lg text-text-secondary hover:bg-app-muted disabled:opacity-50 disabled:cursor-not-allowed transition-colors"><ChevronRight size={16} /></button>
<button onClick={() => setCurrentPage(totalPages)} disabled={currentPage === totalPages} className="p-2 border border-app-border rounded-lg text-text-secondary hover:bg-app-muted disabled:opacity-50 disabled:cursor-not-allowed transition-colors"><ChevronsRight size={16} /></button>
</div>
)}
</div>
</div>
);
}

View File

@ -0,0 +1,90 @@
import { useState, useMemo } from 'react';
import { Building, Plus } from 'lucide-react';
import { toast } from 'sonner';
import Loader from '../../../components/ui/Loader';
import ErrorState from '../../../components/ui/ErrorState';
import CompanyTable from '../components/CompanyTable';
import CompanyFormModal from '../components/CompanyFormModal';
import { useCompanies, type Company } from '../api/useCompanyData';
export default function ManageCompanies() {
const { data: apiRecords, isLoading, isError } = useCompanies();
const [localRecords, setLocalRecords] = useState<Company[]>([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const [editingCompany, setEditingCompany] = useState<Company | null>(null);
// Sync local state when API data loads
useMemo(() => {
// eslint-disable-next-line react-hooks/set-state-in-render
if (apiRecords) setLocalRecords(apiRecords);
}, [apiRecords]);
const handleAddClick = () => {
setEditingCompany(null);
setIsModalOpen(true);
};
const handleEditClick = (company: Company) => {
setEditingCompany(company);
setIsModalOpen(true);
};
const handleDeleteClick = (id: string) => {
// In a real app, you'd call an API to delete. Here we update local state.
setLocalRecords(prev => prev.filter(c => c.id !== id));
toast.success("Company deleted successfully.");
};
const handleSave = (name: string, status: 'Active' | 'Disabled') => {
if (editingCompany) {
// Edit existing
setLocalRecords(prev => prev.map(c => c.id === editingCompany.id ? { ...c, name, status } : c));
toast.success("Company updated successfully.");
} else {
// Add new
const newId = `CMP${String(localRecords.length + 1).padStart(3, '0')}`;
setLocalRecords(prev => [...prev, { id: newId, name, status }]);
toast.success("Company added successfully.");
}
setIsModalOpen(false);
};
if (isLoading) return <Loader message="Loading companies..." />;
if (isError) return <ErrorState message="Failed to load companies." />;
return (
<div className="space-y-6">
{/* Header & Add Button */}
<div className="bg-app-card p-6 rounded-lg shadow-sm flex flex-col md:flex-row md:items-center justify-between gap-4">
<div className="flex items-center space-x-3">
<div className="p-3 bg-primary/10 rounded-full">
<Building className="text-primary" size={28} />
</div>
<div>
<h2 className="text-xl font-bold text-text-primary">Manage Companies</h2>
<p className="text-sm text-text-muted">Add, edit, or disable company entities.</p>
</div>
</div>
<button
onClick={handleAddClick}
className="flex items-center justify-center px-4 py-2 bg-action text-white rounded-lg hover:bg-action-hover transition-colors font-medium"
>
<Plus size={18} className="mr-2" />
Add Company
</button>
</div>
{/* Table */}
<CompanyTable records={localRecords} onEdit={handleEditClick} onDelete={handleDeleteClick} />
{/* Modal */}
{isModalOpen && (
<CompanyFormModal
company={editingCompany}
onClose={() => setIsModalOpen(false)}
onSave={handleSave}
/>
)}
</div>
);
}

View File

@ -1,7 +1,7 @@
import { Routes, Route, Navigate } from 'react-router-dom';
import MasterDataDashboard from '../pages/MasterDataDashboard';
import ManageCompanies from '../pages/ManageCompanies'; // New Import
// Placeholder for future pages
const Placeholder = ({ title }: { title: string }) => (
<div className="bg-app-card p-6 rounded-lg shadow-sm">
<h2 className="text-xl font-bold text-text-primary">{title}</h2>
@ -14,7 +14,7 @@ export default function MasterDataRouter() {
<Routes>
<Route path="/" element={<Navigate to="/master-data/dashboard" replace />} />
<Route path="/dashboard" element={<MasterDataDashboard />} />
<Route path="/company" element={<Placeholder title="Manage Company" />} />
<Route path="/company" element={<ManageCompanies />} /> {/* Updated Route */}
<Route path="/branches" element={<Placeholder title="Manage Branches" />} />
<Route path="/departments" element={<Placeholder title="Manage Departments" />} />
<Route path="/designations" element={<Placeholder title="Manage Designations" />} />