feat: Enhance EditProjectPage with translation support and refactor ProjectForm to use forwardRef

This commit is contained in:
2025-09-29 19:32:08 +02:00
parent e68b185aeb
commit fc5f0fd39a
3 changed files with 49 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
"use client"; "use client";
import { useEffect, useState } from "react"; import { useEffect, useState, useRef } from "react";
import { useParams } from "next/navigation"; import { useParams } from "next/navigation";
import ProjectForm from "@/components/ProjectForm"; import ProjectForm from "@/components/ProjectForm";
import PageContainer from "@/components/ui/PageContainer"; import PageContainer from "@/components/ui/PageContainer";
@@ -8,6 +8,7 @@ import PageHeader from "@/components/ui/PageHeader";
import Button from "@/components/ui/Button"; import Button from "@/components/ui/Button";
import Link from "next/link"; import Link from "next/link";
import { LoadingState } from "@/components/ui/States"; import { LoadingState } from "@/components/ui/States";
import { useTranslation } from "@/lib/i18n";
export default function EditProjectPage() { export default function EditProjectPage() {
const params = useParams(); const params = useParams();
@@ -15,6 +16,8 @@ export default function EditProjectPage() {
const [project, setProject] = useState(null); const [project, setProject] = useState(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState(null); const [error, setError] = useState(null);
const { t } = useTranslation();
const formRef = useRef();
useEffect(() => { useEffect(() => {
const fetchProject = async () => { const fetchProject = async () => {
@@ -62,10 +65,30 @@ export default function EditProjectPage() {
return ( return (
<PageContainer> <PageContainer>
<PageHeader <PageHeader
title="Edit Project" title={t('projects.editProject')}
description={`Editing: ${project.project_name || "Untitled Project"}`} description={`${t('projects.editing')}: ${project.project_name || "Untitled Project"}`}
action={ action={
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<Button
variant="primary"
size="sm"
onClick={() => formRef.current?.saveProject()}
>
<svg
className="w-4 h-4 mr-2"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
{t('common.save')}
</Button>
<Link href={`/projects/${id}`}> <Link href={`/projects/${id}`}>
<Button variant="outline" size="sm"> <Button variant="outline" size="sm">
<svg <svg
@@ -81,7 +104,7 @@ export default function EditProjectPage() {
d="M6 18L18 6M6 6l12 12" d="M6 18L18 6M6 6l12 12"
/> />
</svg> </svg>
Cancel {t('common.cancel')}
</Button> </Button>
</Link> </Link>
<Link href="/projects"> <Link href="/projects">
@@ -99,14 +122,14 @@ export default function EditProjectPage() {
d="M15 19l-7-7 7-7" d="M15 19l-7-7 7-7"
/> />
</svg> </svg>
Back to Projects {t('projects.backToProjects')}
</Button> </Button>
</Link> </Link>
</div> </div>
} }
/> />
<div className="max-w-2xl"> <div className="max-w-2xl">
<ProjectForm initialData={project} /> <ProjectForm ref={formRef} initialData={project} />
</div> </div>
</PageContainer> </PageContainer>
); );

View File

@@ -1,6 +1,6 @@
"use client"; "use client";
import { useState, useEffect } from "react"; import { useState, useEffect, forwardRef, useImperativeHandle } from "react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { Card, CardHeader, CardContent } from "@/components/ui/Card"; import { Card, CardHeader, CardContent } from "@/components/ui/Card";
import Button from "@/components/ui/Button"; import Button from "@/components/ui/Button";
@@ -8,7 +8,7 @@ import { Input } from "@/components/ui/Input";
import { formatDateForInput } from "@/lib/utils"; import { formatDateForInput } from "@/lib/utils";
import { useTranslation } from "@/lib/i18n"; import { useTranslation } from "@/lib/i18n";
export default function ProjectForm({ initialData = null }) { const ProjectForm = forwardRef(function ProjectForm({ initialData = null }, ref) {
const { t } = useTranslation(); const { t } = useTranslation();
const [form, setForm] = useState({ const [form, setForm] = useState({
contract_id: "", contract_id: "",
@@ -81,8 +81,7 @@ export default function ProjectForm({ initialData = null }) {
setForm({ ...form, [e.target.name]: e.target.value }); setForm({ ...form, [e.target.name]: e.target.value });
} }
async function handleSubmit(e) { async function saveProject() {
e.preventDefault();
setLoading(true); setLoading(true);
try { try {
@@ -112,6 +111,16 @@ export default function ProjectForm({ initialData = null }) {
setLoading(false); setLoading(false);
} }
} }
async function handleSubmit(e) {
e.preventDefault();
await saveProject();
}
// Expose save function to parent component
useImperativeHandle(ref, () => ({
saveProject
}));
return ( return (
<Card> <Card>
<CardHeader> <CardHeader>
@@ -438,4 +447,6 @@ export default function ProjectForm({ initialData = null }) {
</CardContent> </CardContent>
</Card> </Card>
); );
} });
export default ProjectForm;

View File

@@ -135,6 +135,8 @@ const translations = {
newProject: "Nowy projekt", newProject: "Nowy projekt",
editProject: "Edytuj projekt", editProject: "Edytuj projekt",
deleteProject: "Usuń projekt", deleteProject: "Usuń projekt",
editing: "Edycja",
backToProjects: "Powrót do projektów",
projectName: "Nazwa projektu", projectName: "Nazwa projektu",
city: "Miasto", city: "Miasto",
address: "Adres", address: "Adres",
@@ -687,6 +689,8 @@ const translations = {
newProject: "New Project", newProject: "New Project",
editProject: "Edit Project", editProject: "Edit Project",
deleteProject: "Delete Project", deleteProject: "Delete Project",
editing: "Editing",
backToProjects: "Back to Projects",
projectName: "Project Name", projectName: "Project Name",
city: "City", city: "City",
address: "Address", address: "Address",