chore: file formatting
This commit is contained in:
@@ -1,48 +1,23 @@
|
||||
import {useState, useEffect, useCallback} from "react";
|
||||
import {AxiosError} from "axios";
|
||||
import {BytescaleUploader} from "@/components/BytescaleUploader/BytescaleUploader";
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { AxiosError } from 'axios';
|
||||
import { BytescaleUploader } from '@/components/BytescaleUploader/BytescaleUploader';
|
||||
|
||||
import {
|
||||
getDepartmentsApi,
|
||||
createDepartmentApi,
|
||||
updateDepartmentApi,
|
||||
} from "@/api/department";
|
||||
import { getDepartmentsApi, createDepartmentApi, updateDepartmentApi } from '@/api/department';
|
||||
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
|
||||
import {Card, CardContent, CardHeader, CardTitle} from "@/components/ui/card";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog';
|
||||
|
||||
import {Input} from "@/components/ui/input";
|
||||
import {Textarea} from "@/components/ui/textarea";
|
||||
import {Switch} from "@/components/ui/switch";
|
||||
import {Label} from "@/components/ui/label";
|
||||
import {Badge} from "@/components/ui/badge";
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
|
||||
import {
|
||||
Loader2,
|
||||
RefreshCw,
|
||||
Plus,
|
||||
Pencil,
|
||||
Eye,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
} from "lucide-react";
|
||||
import { Loader2, RefreshCw, Plus, Pencil, Eye, ChevronLeft, ChevronRight } from 'lucide-react';
|
||||
|
||||
interface Department {
|
||||
departmentId: string;
|
||||
@@ -60,7 +35,7 @@ interface Department {
|
||||
export default function DepartmentPage() {
|
||||
const [departments, setDepartments] = useState<Department[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
const [error, setError] = useState('');
|
||||
|
||||
const [openModal, setOpenModal] = useState(false);
|
||||
const [editing, setEditing] = useState<Department | null>(null);
|
||||
@@ -68,36 +43,36 @@ export default function DepartmentPage() {
|
||||
const [viewOpen, setViewOpen] = useState(false);
|
||||
const [viewData, setViewData] = useState<Department | null>(null);
|
||||
|
||||
const [searchText, setSearchText] = useState("");
|
||||
const [searchText, setSearchText] = useState('');
|
||||
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const itemsPerPage = 10;
|
||||
|
||||
const [form, setForm] = useState<Department>({
|
||||
departmentId: "",
|
||||
name: "",
|
||||
image: "",
|
||||
para1: "",
|
||||
para2: "",
|
||||
para3: "",
|
||||
facilities: "",
|
||||
services: "",
|
||||
departmentId: '',
|
||||
name: '',
|
||||
image: '',
|
||||
para1: '',
|
||||
para2: '',
|
||||
para3: '',
|
||||
facilities: '',
|
||||
services: '',
|
||||
isActive: true,
|
||||
sortOrder: 0,
|
||||
});
|
||||
|
||||
const fetchDepartments = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
setError('');
|
||||
|
||||
try {
|
||||
const res = await getDepartmentsApi();
|
||||
setDepartments(res?.data || []);
|
||||
} catch (err) {
|
||||
if (err instanceof AxiosError) {
|
||||
setError(err.response?.data?.message || "Failed to load departments");
|
||||
setError(err.response?.data?.message || 'Failed to load departments');
|
||||
} else {
|
||||
setError("Something went wrong");
|
||||
setError('Something went wrong');
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -111,7 +86,7 @@ export default function DepartmentPage() {
|
||||
const filteredDepartments = departments.filter(
|
||||
(dep) =>
|
||||
dep.name.toLowerCase().includes(searchText.toLowerCase()) ||
|
||||
dep.departmentId.toLowerCase().includes(searchText.toLowerCase()),
|
||||
dep.departmentId.toLowerCase().includes(searchText.toLowerCase())
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -121,41 +96,37 @@ export default function DepartmentPage() {
|
||||
const totalPages = Math.ceil(filteredDepartments.length / itemsPerPage);
|
||||
const indexOfLastItem = currentPage * itemsPerPage;
|
||||
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
|
||||
const currentItems = filteredDepartments.slice(
|
||||
indexOfFirstItem,
|
||||
indexOfLastItem,
|
||||
);
|
||||
const currentItems = filteredDepartments.slice(indexOfFirstItem, indexOfLastItem);
|
||||
|
||||
function handleChange(e: any) {
|
||||
const value =
|
||||
e.target.type === "number" ? Number(e.target.value) : e.target.value;
|
||||
setForm({...form, [e.target.name]: value});
|
||||
const value = e.target.type === 'number' ? Number(e.target.value) : e.target.value;
|
||||
setForm({ ...form, [e.target.name]: value });
|
||||
}
|
||||
|
||||
const handleToggleStatus = async (dep: Department) => {
|
||||
try {
|
||||
const {departmentId, ...updateData} = dep;
|
||||
const { departmentId, ...updateData } = dep;
|
||||
await updateDepartmentApi(departmentId, {
|
||||
...updateData,
|
||||
isActive: !dep.isActive,
|
||||
} as any);
|
||||
fetchDepartments();
|
||||
} catch (error) {
|
||||
console.error("Failed to toggle status", error);
|
||||
console.error('Failed to toggle status', error);
|
||||
}
|
||||
};
|
||||
|
||||
function openAdd() {
|
||||
setEditing(null);
|
||||
setForm({
|
||||
departmentId: "",
|
||||
name: "",
|
||||
image: "",
|
||||
para1: "",
|
||||
para2: "",
|
||||
para3: "",
|
||||
facilities: "",
|
||||
services: "",
|
||||
departmentId: '',
|
||||
name: '',
|
||||
image: '',
|
||||
para1: '',
|
||||
para2: '',
|
||||
para3: '',
|
||||
facilities: '',
|
||||
services: '',
|
||||
isActive: true,
|
||||
sortOrder: 0,
|
||||
});
|
||||
@@ -180,7 +151,7 @@ export default function DepartmentPage() {
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
if (editing) {
|
||||
const {departmentId, ...updateData} = form;
|
||||
const { departmentId, ...updateData } = form;
|
||||
await updateDepartmentApi(editing.departmentId, form as any);
|
||||
} else {
|
||||
await createDepartmentApi(form);
|
||||
@@ -206,12 +177,7 @@ export default function DepartmentPage() {
|
||||
className="w-[250px] text-base"
|
||||
/>
|
||||
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={fetchDepartments}
|
||||
disabled={loading}
|
||||
className="text-base"
|
||||
>
|
||||
<Button variant="outline" onClick={fetchDepartments} disabled={loading} className="text-base">
|
||||
<RefreshCw className="mr-2 h-5 w-5" />
|
||||
Refresh
|
||||
</Button>
|
||||
@@ -223,11 +189,7 @@ export default function DepartmentPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="p-4 text-red-600 bg-red-50 border rounded-md text-base">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
{error && <div className="p-4 text-red-600 bg-red-50 border rounded-md text-base">{error}</div>}
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
@@ -239,18 +201,10 @@ export default function DepartmentPage() {
|
||||
<Table className="w-full min-w-[700px] table-fixed border-separate border-spacing-0">
|
||||
<TableHeader className="sticky top-0 z-20 bg-background shadow-sm">
|
||||
<TableRow>
|
||||
<TableHead className="w-[100px] bg-background text-sm font-bold">
|
||||
Priority
|
||||
</TableHead>
|
||||
<TableHead className="w-[300px] bg-background text-sm font-bold">
|
||||
Name
|
||||
</TableHead>
|
||||
<TableHead className="w-[80px] bg-background text-sm font-bold">
|
||||
Status (Active)
|
||||
</TableHead>
|
||||
<TableHead className="w-[80px] bg-background text-right text-sm font-bold">
|
||||
Actions
|
||||
</TableHead>
|
||||
<TableHead className="w-[100px] bg-background text-sm font-bold">Priority</TableHead>
|
||||
<TableHead className="w-[300px] bg-background text-sm font-bold">Name</TableHead>
|
||||
<TableHead className="w-[80px] bg-background text-sm font-bold">Status (Active)</TableHead>
|
||||
<TableHead className="w-[80px] bg-background text-right text-sm font-bold">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
@@ -263,65 +217,37 @@ export default function DepartmentPage() {
|
||||
</TableRow>
|
||||
) : currentItems.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell
|
||||
colSpan={4}
|
||||
className="text-center text-muted-foreground py-10 text-base"
|
||||
>
|
||||
<TableCell colSpan={4} className="text-center text-muted-foreground py-10 text-base">
|
||||
No departments found
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
currentItems.map((dep) => (
|
||||
<TableRow
|
||||
key={dep.departmentId}
|
||||
className="hover:bg-muted/50"
|
||||
>
|
||||
<TableCell className="font-mono text-sm">
|
||||
{dep.sortOrder}
|
||||
</TableCell>
|
||||
<TableRow key={dep.departmentId} className="hover:bg-muted/50">
|
||||
<TableCell className="font-mono text-sm">{dep.sortOrder}</TableCell>
|
||||
|
||||
<TableCell>
|
||||
<div
|
||||
className="font-semibold text-base truncate"
|
||||
title={dep.name}
|
||||
>
|
||||
<div className="font-semibold text-base truncate" title={dep.name}>
|
||||
{dep.name}
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{dep.departmentId}
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground">{dep.departmentId}</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
checked={dep.isActive}
|
||||
onCheckedChange={() => handleToggleStatus(dep)}
|
||||
/>
|
||||
<Badge
|
||||
variant={dep.isActive ? "default" : "secondary"}
|
||||
>
|
||||
{dep.isActive ? "Active" : "Hidden"}
|
||||
<Switch checked={dep.isActive} onCheckedChange={() => handleToggleStatus(dep)} />
|
||||
<Badge variant={dep.isActive ? 'default' : 'secondary'}>
|
||||
{dep.isActive ? 'Active' : 'Hidden'}
|
||||
</Badge>
|
||||
</div>
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="text-right">
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="h-9 w-9"
|
||||
onClick={() => openView(dep)}
|
||||
>
|
||||
<Button size="icon" variant="ghost" className="h-9 w-9" onClick={() => openView(dep)}>
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="h-9 w-9"
|
||||
onClick={() => openEdit(dep)}
|
||||
>
|
||||
<Button size="icon" variant="ghost" className="h-9 w-9" onClick={() => openEdit(dep)}>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
@@ -336,16 +262,9 @@ export default function DepartmentPage() {
|
||||
{!loading && filteredDepartments.length > 0 && (
|
||||
<div className="flex items-center justify-between px-2 py-6 border-t">
|
||||
<div className="text-base text-muted-foreground">
|
||||
Showing{" "}
|
||||
<span className="font-semibold">{indexOfFirstItem + 1}</span> to{" "}
|
||||
<span className="font-semibold">
|
||||
{Math.min(indexOfLastItem, filteredDepartments.length)}
|
||||
</span>{" "}
|
||||
of{" "}
|
||||
<span className="font-semibold">
|
||||
{filteredDepartments.length}
|
||||
</span>{" "}
|
||||
departments
|
||||
Showing <span className="font-semibold">{indexOfFirstItem + 1}</span> to{' '}
|
||||
<span className="font-semibold">{Math.min(indexOfLastItem, filteredDepartments.length)}</span> of{' '}
|
||||
<span className="font-semibold">{filteredDepartments.length}</span> departments
|
||||
</div>
|
||||
<div className="flex items-center gap-6">
|
||||
<div className="text-base font-semibold">
|
||||
@@ -356,9 +275,7 @@ export default function DepartmentPage() {
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="h-10 w-10"
|
||||
onClick={() =>
|
||||
setCurrentPage((prev) => Math.max(prev - 1, 1))
|
||||
}
|
||||
onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 1))}
|
||||
disabled={currentPage === 1}
|
||||
>
|
||||
<ChevronLeft className="h-5 w-5" />
|
||||
@@ -367,9 +284,7 @@ export default function DepartmentPage() {
|
||||
variant="outline"
|
||||
size="icon"
|
||||
className="h-10 w-10"
|
||||
onClick={() =>
|
||||
setCurrentPage((prev) => Math.min(prev + 1, totalPages))
|
||||
}
|
||||
onClick={() => setCurrentPage((prev) => Math.min(prev + 1, totalPages))}
|
||||
disabled={currentPage === totalPages || totalPages === 0}
|
||||
>
|
||||
<ChevronRight className="h-5 w-5" />
|
||||
@@ -384,9 +299,7 @@ export default function DepartmentPage() {
|
||||
<Dialog open={openModal} onOpenChange={setOpenModal}>
|
||||
<DialogContent className="w-full !max-w-5xl max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{editing ? "Edit Department" : "Add Department"}
|
||||
</DialogTitle>
|
||||
<DialogTitle>{editing ? 'Edit Department' : 'Add Department'}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
@@ -395,7 +308,7 @@ export default function DepartmentPage() {
|
||||
<BytescaleUploader
|
||||
value={form.image}
|
||||
folderPath="/departments"
|
||||
onChange={(url) => setForm({...form, image: url})}
|
||||
onChange={(url) => setForm({ ...form, image: url })}
|
||||
/>
|
||||
</div>
|
||||
<Input
|
||||
@@ -406,43 +319,13 @@ export default function DepartmentPage() {
|
||||
placeholder="Department ID"
|
||||
/>
|
||||
|
||||
<Input
|
||||
name="name"
|
||||
value={form.name}
|
||||
onChange={handleChange}
|
||||
placeholder="Department Name"
|
||||
/>
|
||||
<Input name="name" value={form.name} onChange={handleChange} placeholder="Department Name" />
|
||||
|
||||
<Textarea
|
||||
name="para1"
|
||||
value={form.para1}
|
||||
onChange={handleChange}
|
||||
placeholder="Para1"
|
||||
/>
|
||||
<Textarea
|
||||
name="para2"
|
||||
value={form.para2}
|
||||
onChange={handleChange}
|
||||
placeholder="Para2"
|
||||
/>
|
||||
<Textarea
|
||||
name="para3"
|
||||
value={form.para3}
|
||||
onChange={handleChange}
|
||||
placeholder="Para3"
|
||||
/>
|
||||
<Textarea
|
||||
name="facilities"
|
||||
value={form.facilities}
|
||||
onChange={handleChange}
|
||||
placeholder="Facilities"
|
||||
/>
|
||||
<Textarea
|
||||
name="services"
|
||||
value={form.services}
|
||||
onChange={handleChange}
|
||||
placeholder="Services"
|
||||
/>
|
||||
<Textarea name="para1" value={form.para1} onChange={handleChange} placeholder="Para1" />
|
||||
<Textarea name="para2" value={form.para2} onChange={handleChange} placeholder="Para2" />
|
||||
<Textarea name="para3" value={form.para3} onChange={handleChange} placeholder="Para3" />
|
||||
<Textarea name="facilities" value={form.facilities} onChange={handleChange} placeholder="Facilities" />
|
||||
<Textarea name="services" value={form.services} onChange={handleChange} placeholder="Services" />
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 border-t pt-4">
|
||||
<div className="flex items-center justify-between p-3 border rounded-md">
|
||||
@@ -452,13 +335,11 @@ export default function DepartmentPage() {
|
||||
<Switch
|
||||
id="isActive"
|
||||
checked={form.isActive}
|
||||
onCheckedChange={(val) => setForm({...form, isActive: val})}
|
||||
onCheckedChange={(val) => setForm({ ...form, isActive: val })}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="sortOrder">
|
||||
Sort Priority (Lower numbers show first)
|
||||
</Label>
|
||||
<Label htmlFor="sortOrder">Sort Priority (Lower numbers show first)</Label>
|
||||
<Input
|
||||
id="sortOrder"
|
||||
name="sortOrder"
|
||||
@@ -475,9 +356,7 @@ export default function DepartmentPage() {
|
||||
<Button variant="outline" onClick={() => setOpenModal(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSubmit}>
|
||||
{editing ? "Save Changes" : "Create"}
|
||||
</Button>
|
||||
<Button onClick={handleSubmit}>{editing ? 'Save Changes' : 'Create'}</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
@@ -490,8 +369,8 @@ export default function DepartmentPage() {
|
||||
{viewData && (
|
||||
<div className="space-y-4 text-sm">
|
||||
<div className="flex gap-4 items-center border-b pb-4">
|
||||
<Badge variant={viewData.isActive ? "default" : "secondary"}>
|
||||
{viewData.isActive ? "ACTIVE" : "HIDDEN"}
|
||||
<Badge variant={viewData.isActive ? 'default' : 'secondary'}>
|
||||
{viewData.isActive ? 'ACTIVE' : 'HIDDEN'}
|
||||
</Badge>
|
||||
<p>
|
||||
<b>Sort Order:</b> {viewData.sortOrder}
|
||||
|
||||
Reference in New Issue
Block a user