251127:1700 Frontend Start Build

This commit is contained in:
admin
2025-11-27 17:08:49 +07:00
parent 6abb746e08
commit 4f3aa87a93
1795 changed files with 893474 additions and 10 deletions

View File

@@ -0,0 +1,240 @@
// File: app/(dashboard)/projects/new/page.tsx
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { Loader2, ChevronLeft, Save } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import apiClient from "@/lib/api/client";
// 1. กำหนด Schema สำหรับตรวจสอบข้อมูล (Validation)
// อ้างอิงจาก Data Dictionary ตาราง projects
const projectSchema = z.object({
project_code: z
.string()
.min(1, "กรุณาระบุรหัสโครงการ")
.max(50, "รหัสโครงการต้องไม่เกิน 50 ตัวอักษร")
.regex(/^[A-Z0-9-]+$/, "รหัสโครงการควรประกอบด้วยตัวอักษรภาษาอังกฤษตัวใหญ่ ตัวเลข หรือขีด (-) เท่านั้น"),
project_name: z
.string()
.min(1, "กรุณาระบุชื่อโครงการ")
.max(255, "ชื่อโครงการต้องไม่เกิน 255 ตัวอักษร"),
description: z.string().optional(), // ฟิลด์เสริม (อาจจะยังไม่มีใน DB แต่เผื่อไว้สำหรับ UI)
status: z.enum(["Active", "Inactive", "On Hold"]).default("Active"),
start_date: z.string().optional(),
end_date: z.string().optional(),
});
type ProjectValues = z.infer<typeof projectSchema>;
export default function CreateProjectPage() {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
// 2. ตั้งค่า React Hook Form
const {
register,
handleSubmit,
setValue, // ใช้สำหรับ manual set value (เช่น Select)
watch, // ใช้ดูค่า (สำหรับ Debug หรือ Conditional Logic)
formState: { errors },
} = useForm<ProjectValues>({
resolver: zodResolver(projectSchema),
defaultValues: {
project_code: "",
project_name: "",
status: "Active",
},
});
// 3. ฟังก์ชัน Submit
async function onSubmit(data: ProjectValues) {
setIsLoading(true);
try {
// เรียก API สร้างโครงการ (Mockup URL)
// ใน Phase หลัง Backend จะเตรียม Endpoint POST /projects ไว้ให้
console.log("Submitting project data:", data);
// จำลองการส่งข้อมูล (Artificial Delay)
await new Promise((resolve) => setTimeout(resolve, 1000));
// await apiClient.post("/projects", data);
alert("สร้างโครงการสำเร็จ"); // TODO: เปลี่ยนเป็น Toast
router.push("/projects");
router.refresh();
} catch (error) {
console.error("Failed to create project:", error);
alert("เกิดข้อผิดพลาดในการสร้างโครงการ");
} finally {
setIsLoading(false);
}
}
return (
<div className="max-w-2xl mx-auto space-y-6">
{/* Header with Back Button */}
<div className="flex items-center gap-4">
<Button
variant="outline"
size="icon"
onClick={() => router.back()}
className="h-9 w-9"
>
<ChevronLeft className="h-5 w-5" />
<span className="sr-only">Back</span>
</Button>
<div>
<h2 className="text-2xl font-bold tracking-tight">Create New Project</h2>
<p className="text-muted-foreground">
</p>
</div>
</div>
<form onSubmit={handleSubmit(onSubmit)}>
<Card>
<CardHeader>
<CardTitle></CardTitle>
<CardDescription>
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{/* Project Code */}
<div className="space-y-2">
<Label htmlFor="project_code" className="after:content-['*'] after:ml-0.5 after:text-red-500">
(Project Code)
</Label>
<Input
id="project_code"
placeholder="e.g. LCBP3-C1"
className={errors.project_code ? "border-destructive" : ""}
{...register("project_code")}
// แปลงเป็นตัวพิมพ์ใหญ่ให้อัตโนมัติเพื่อความเป็นระเบียบ
onChange={(e) => {
e.target.value = e.target.value.toUpperCase();
register("project_code").onChange(e);
}}
/>
{errors.project_code ? (
<p className="text-xs text-destructive">{errors.project_code.message}</p>
) : (
<p className="text-xs text-muted-foreground">
(-)
</p>
)}
</div>
{/* Project Name */}
<div className="space-y-2">
<Label htmlFor="project_name" className="after:content-['*'] after:ml-0.5 after:text-red-500">
(Project Name)
</Label>
<Input
id="project_name"
placeholder="ระบุชื่อโครงการฉบับเต็ม..."
className={errors.project_name ? "border-destructive" : ""}
{...register("project_name")}
/>
{errors.project_name && (
<p className="text-xs text-destructive">{errors.project_name.message}</p>
)}
</div>
{/* Description */}
<div className="space-y-2">
<Label htmlFor="description"></Label>
<Textarea
id="description"
placeholder="คำอธิบายเกี่ยวกับขอบเขตงานของโครงการ..."
className="min-h-[100px]"
{...register("description")}
/>
</div>
{/* Dates Row */}
<div className="grid gap-4 md:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="start_date"></Label>
<Input
id="start_date"
type="date"
{...register("start_date")}
/>
</div>
<div className="space-y-2">
<Label htmlFor="end_date"></Label>
<Input
id="end_date"
type="date"
{...register("end_date")}
/>
</div>
</div>
{/* Status Select */}
<div className="space-y-2">
<Label htmlFor="status"></Label>
{/* เนื่องจาก Select ของ Shadcn เป็น Custom UI
เราต้องใช้ onValueChange เพื่อเชื่อมกับ React Hook Form
*/}
<Select
onValueChange={(value: any) => setValue("status", value)}
defaultValue="Active"
>
<SelectTrigger>
<SelectValue placeholder="เลือกสถานะ" />
</SelectTrigger>
<SelectContent>
<SelectItem value="Active">Active ()</SelectItem>
<SelectItem value="On Hold">On Hold ()</SelectItem>
<SelectItem value="Inactive">Inactive ()</SelectItem>
</SelectContent>
</Select>
</div>
</CardContent>
<CardFooter className="flex justify-end gap-2 border-t p-4 bg-muted/50">
<Button
type="button"
variant="ghost"
onClick={() => router.back()}
disabled={isLoading}
>
</Button>
<Button type="submit" disabled={isLoading}>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
<Save className="mr-2 h-4 w-4" />
</Button>
</CardFooter>
</Card>
</form>
</div>
);
}

View File

@@ -0,0 +1,251 @@
// File: app/(dashboard)/projects/page.tsx
"use client";
import { useState } from "react";
import { Plus, Search, MoreHorizontal, Folder, Calendar, BarChart3 } from "lucide-react";
import { useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import { Progress } from "@/components/ui/progress";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Card,
CardContent,
CardHeader,
CardTitle,
CardDescription,
CardFooter,
} from "@/components/ui/card";
// Type สำหรับข้อมูล Project (Mockup ตาม Data Dictionary)
type Project = {
id: number;
project_code: string;
project_name: string;
status: "Active" | "Completed" | "On Hold";
progress: number;
start_date: string;
end_date: string;
contractor_name: string;
};
// Mock Data
const mockProjects: Project[] = [
{
id: 1,
project_code: "LCBP3",
project_name: "โครงการพัฒนาท่าเรือแหลมฉบัง ระยะที่ 3 (ส่วนที่ 1-4)",
status: "Active",
progress: 45,
start_date: "2021-01-01",
end_date: "2025-12-31",
contractor_name: "Multiple Contractors",
},
{
id: 2,
project_code: "LCBP3-C1",
project_name: "งานก่อสร้างงานทางทะเล (ส่วนที่ 1)",
status: "Active",
progress: 70,
start_date: "2021-06-01",
end_date: "2024-06-01",
contractor_name: "CNNC",
},
{
id: 3,
project_code: "LCBP3-C2",
project_name: "งานก่อสร้างอาคาร ท่าเทียบเรือ (ส่วนที่ 2)",
status: "Active",
progress: 15,
start_date: "2023-01-01",
end_date: "2026-01-01",
contractor_name: "ITD-NWR Joint Venture",
},
];
export default function ProjectsPage() {
const router = useRouter();
const [searchTerm, setSearchTerm] = useState("");
const filteredProjects = mockProjects.filter((project) =>
project.project_name.toLowerCase().includes(searchTerm.toLowerCase()) ||
project.project_code.toLowerCase().includes(searchTerm.toLowerCase())
);
const getStatusVariant = (status: string) => {
switch (status) {
case "Active": return "success"; // ใช้ variant ที่เรา custom ไว้ใน badge.tsx
case "Completed": return "default";
case "On Hold": return "warning";
default: return "secondary";
}
};
const handleCreateProject = () => {
router.push("/projects/new"); // อัปเดตเป็นลิงก์จริง
};
const handleViewDetails = (id: number) => {
router.push(`/projects/${id}`);
};
return (
<div className="space-y-6">
{/* Header */}
<div className="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
<div>
<h2 className="text-2xl font-bold tracking-tight">Projects</h2>
<p className="text-muted-foreground">
</p>
</div>
<Button onClick={handleCreateProject} className="w-full md:w-auto">
<Plus className="mr-2 h-4 w-4" /> New Project
</Button>
</div>
{/* Filters */}
<div className="flex items-center space-x-2">
<div className="relative flex-1 md:max-w-sm">
<Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search projects..."
className="pl-8"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
</div>
</div>
{/* Desktop View: Table */}
<div className="hidden rounded-md border bg-card md:block">
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[100px]">Code</TableHead>
<TableHead>Project Name</TableHead>
<TableHead>Contractor</TableHead>
<TableHead>Status</TableHead>
<TableHead className="w-[200px]">Progress</TableHead>
<TableHead className="text-right">Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{filteredProjects.map((project) => (
<TableRow key={project.id} className="cursor-pointer hover:bg-muted/50" onClick={() => handleViewDetails(project.id)}>
<TableCell className="font-medium">{project.project_code}</TableCell>
<TableCell>
<div className="flex flex-col">
<span>{project.project_name}</span>
<span className="text-xs text-muted-foreground">
{project.start_date} - {project.end_date}
</span>
</div>
</TableCell>
<TableCell>{project.contractor_name}</TableCell>
<TableCell>
<Badge variant={getStatusVariant(project.status)}>
{project.status}
</Badge>
</TableCell>
<TableCell>
<div className="flex items-center gap-2">
<Progress value={project.progress} className="h-2" />
<span className="text-xs text-muted-foreground w-[30px] text-right">
{project.progress}%
</span>
</div>
</TableCell>
<TableCell className="text-right">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 w-8 p-0">
<span className="sr-only">Open menu</span>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuItem onClick={(e) => { e.stopPropagation(); handleViewDetails(project.id); }}>
View Details
</DropdownMenuItem>
<DropdownMenuItem onClick={(e) => { e.stopPropagation(); alert(`Manage Contracts for ${project.project_code}`); }}>
Manage Contracts
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={(e) => { e.stopPropagation(); alert(`Edit ${project.project_code}`); }}>
Edit Project
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
{/* Mobile View: Cards */}
<div className="grid gap-4 md:hidden">
{filteredProjects.map((project) => (
<Card key={project.id} onClick={() => handleViewDetails(project.id)} className="cursor-pointer active:bg-muted/50">
<CardHeader className="pb-2">
<div className="flex justify-between items-start">
<div>
<CardTitle className="text-base font-bold">{project.project_code}</CardTitle>
<CardDescription className="mt-1 line-clamp-2">
{project.project_name}
</CardDescription>
</div>
<Badge variant={getStatusVariant(project.status)} className="shrink-0">
{project.status}
</Badge>
</div>
</CardHeader>
<CardContent className="pb-2 space-y-4">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Folder className="h-4 w-4" />
<span>{project.contractor_name}</span>
</div>
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Calendar className="h-4 w-4" />
<span>{project.start_date} - {project.end_date}</span>
</div>
<div className="space-y-1">
<div className="flex justify-between text-xs">
<span className="text-muted-foreground flex items-center gap-1">
<BarChart3 className="h-3 w-3" /> Progress
</span>
<span className="font-medium">{project.progress}%</span>
</div>
<Progress value={project.progress} className="h-2" />
</div>
</CardContent>
<CardFooter className="pt-2">
<Button variant="ghost" size="sm" className="w-full ml-auto">
View Details
</Button>
</CardFooter>
</Card>
))}
</div>
</div>
);
}