All files / src/components/admin ProProjectsSection.tsx

95.23% Statements 20/21
90% Branches 9/10
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                          139x 139x       139x   139x 6x 6x 6x 6x 5x 5x   1x 1x   6x       139x             13x                                           245x                                                                                         1x                                   8x                               1x                           1x                                    
import { useState } from "react";
import { notify } from "../../lib/notify";
import { Link } from "@tanstack/react-router";
import { Plus, X } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { getErrorMessage, type Project } from "../../lib/api";
 
interface ProProjectsSectionProps {
	projects: Project[];
	onCreateProject: (project: {
		title: string;
		description: string;
	}) => Promise<void>;
}
 
export function ProProjectsSection({
	projects,
	onCreateProject,
}: ProProjectsSectionProps) {
	const [showNewProject, setShowNewProject] = useState(false);
	const [newProject, setNewProject] = useState({
		title: "",
		description: "",
	});
	const [isCreatingProject, setIsCreatingProject] = useState(false);
 
	const handleCreateProject = async () => {
		Iif (!newProject.title.trim()) return;
		setIsCreatingProject(true);
		try {
			await onCreateProject(newProject);
			setShowNewProject(false);
			setNewProject({ title: "", description: "" });
		} catch (err) {
			console.error("Failed to create project:", err);
			notify.error(getErrorMessage(err));
		} finally {
			setIsCreatingProject(false);
		}
	};
 
	return (
		<>
			<Card>
				<CardHeader className="flex flex-row items-center justify-between">
					<CardTitle className="text-lg">
						Projects ({projects.length})
					</CardTitle>
					<Button size="sm" onClick={() => setShowNewProject(true)}>
						<Plus className="h-4 w-4 mr-1" />
						Add Project
					</Button>
				</CardHeader>
				<CardContent>
					{projects.length === 0 ? (
						<p className="text-foreground-muted text-center py-8">
							No projects yet. Click "Add Project" to create one.
						</p>
					) : (
						<div className="overflow-x-auto">
							<table className="w-full">
								<thead>
									<tr className="border-b border-border-default text-left text-sm text-foreground-muted">
										<th className="pb-3 font-medium">Title</th>
		<th className="pb-3 font-medium">Status</th>
										<th className="pb-3 font-medium">Created</th>
									</tr>
								</thead>
								<tbody>
									{projects.map((project) => (
										<tr
											key={project.id}
											className="border-b border-border-default last:border-0 hover:bg-background-muted"
										>
											<td className="py-3">
												<Link
													to={`/admin/projects/${project.id}`}
													className="text-primary-600 hover:underline"
												>
													{project.title}
												</Link>
											</td>
											<td className="py-3">
												<span
													className={`inline-block px-2 py-1 text-xs font-medium rounded-full ${
														project.status === "published"
															? "bg-success-light text-success"
															: project.status === "draft"
																? "bg-warning-light text-warning"
																: "bg-background-muted text-foreground-muted"
													}`}
												>
													{project.status}
												</span>
											</td>
											<td className="py-3 text-foreground-muted">
												{new Date(project.dateCreated).toLocaleDateString()}
											</td>
										</tr>
									))}
								</tbody>
							</table>
						</div>
					)}
				</CardContent>
			</Card>
 
			{/* New Project Modal */}
			{showNewProject && (
				<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
					<div className="bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4">
						<div className="flex items-center justify-between p-4 border-b border-border-default">
							<h3 className="text-lg font-semibold">Create New Project</h3>
							<button
								type="button"
								onClick={() => setShowNewProject(false)}
								className="p-1 hover:bg-background-muted rounded"
							>
								<X className="h-5 w-5" />
							</button>
						</div>
						<div className="p-4 space-y-4">
							<div>
								<label
									htmlFor="project-title"
									className="block text-sm font-medium text-foreground-default mb-1"
								>
									Title *
								</label>
								<Input
									id="project-title"
									value={newProject.title}
									onChange={(e) =>
										setNewProject({ ...newProject, title: e.target.value })
									}
									placeholder="Project title"
								/>
							</div>
							<div>
								<label
									htmlFor="project-description"
									className="block text-sm font-medium text-foreground-default mb-1"
								>
									Description
								</label>
								<textarea
									id="project-description"
									value={newProject.description}
									onChange={(e) =>
										setNewProject({
											...newProject,
											description: e.target.value,
										})
									}
									rows={3}
									className="w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
									placeholder="Brief description..."
								/>
							</div>
						</div>
						<div className="flex justify-end gap-2 p-4 border-t border-border-default">
							<Button
								variant="outline"
								onClick={() => setShowNewProject(false)}
							>
								Cancel
							</Button>
							<Button
								onClick={handleCreateProject}
								isLoading={isCreatingProject}
								disabled={!newProject.title.trim()}
							>
								Create Project
							</Button>
						</div>
					</div>
				</div>
			)}
		</>
	);
}