Compare commits
2 Commits
84f63c37ce
...
7335d17900
| Author | SHA1 | Date | |
|---|---|---|---|
| 7335d17900 | |||
| b358f5d7b7 |
@@ -1,6 +1,6 @@
|
||||
import db from "@/lib/db";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withReadAuth, withUserAuth } from "@/lib/middleware/auth";
|
||||
import { withReadAuth, withTeamLeadAuth, withUserAuth } from "@/lib/middleware/auth";
|
||||
|
||||
async function getContractHandler(req, { params }) {
|
||||
const { id } = await params;
|
||||
@@ -21,6 +21,79 @@ async function getContractHandler(req, { params }) {
|
||||
return NextResponse.json(contract);
|
||||
}
|
||||
|
||||
async function updateContractHandler(req, { params }) {
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const body = await req.json();
|
||||
const {
|
||||
contract_number,
|
||||
contract_name,
|
||||
customer_contract_number,
|
||||
customer,
|
||||
investor,
|
||||
date_signed,
|
||||
finish_date,
|
||||
} = body;
|
||||
|
||||
// Check if contract exists
|
||||
const existingContract = db
|
||||
.prepare("SELECT * FROM contracts WHERE contract_id = ?")
|
||||
.get(id);
|
||||
|
||||
if (!existingContract) {
|
||||
return NextResponse.json(
|
||||
{ error: "Contract not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Update the contract
|
||||
const result = db
|
||||
.prepare(
|
||||
`UPDATE contracts
|
||||
SET contract_number = ?,
|
||||
contract_name = ?,
|
||||
customer_contract_number = ?,
|
||||
customer = ?,
|
||||
investor = ?,
|
||||
date_signed = ?,
|
||||
finish_date = ?
|
||||
WHERE contract_id = ?`
|
||||
)
|
||||
.run(
|
||||
contract_number,
|
||||
contract_name || null,
|
||||
customer_contract_number || null,
|
||||
customer || null,
|
||||
investor || null,
|
||||
date_signed || null,
|
||||
finish_date || null,
|
||||
id
|
||||
);
|
||||
|
||||
if (result.changes === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to update contract" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// Fetch and return the updated contract
|
||||
const updatedContract = db
|
||||
.prepare("SELECT * FROM contracts WHERE contract_id = ?")
|
||||
.get(id);
|
||||
|
||||
return NextResponse.json(updatedContract);
|
||||
} catch (error) {
|
||||
console.error("Error updating contract:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteContractHandler(req, { params }) {
|
||||
const { id } = params;
|
||||
|
||||
@@ -61,4 +134,5 @@ async function deleteContractHandler(req, { params }) {
|
||||
|
||||
// Protected routes - require authentication
|
||||
export const GET = withReadAuth(getContractHandler);
|
||||
export const DELETE = withUserAuth(deleteContractHandler);
|
||||
export const PUT = withUserAuth(updateContractHandler);
|
||||
export const DELETE = withTeamLeadAuth(deleteContractHandler);
|
||||
|
||||
139
src/app/contracts/[id]/edit/page.js
Normal file
139
src/app/contracts/[id]/edit/page.js
Normal file
@@ -0,0 +1,139 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useParams } from "next/navigation";
|
||||
import ContractForm from "@/components/ContractForm";
|
||||
import PageContainer from "@/components/ui/PageContainer";
|
||||
import PageHeader from "@/components/ui/PageHeader";
|
||||
import Button from "@/components/ui/Button";
|
||||
import Link from "next/link";
|
||||
import { LoadingState } from "@/components/ui/States";
|
||||
import { useTranslation } from "@/lib/i18n";
|
||||
|
||||
export default function EditContractPage() {
|
||||
const params = useParams();
|
||||
const id = params.id;
|
||||
const [contract, setContract] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
const { t } = useTranslation();
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchContract() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/contracts/${id}`);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to fetch contract");
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
setContract(data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching contract:", error);
|
||||
setError("Nie udało się pobrać danych umowy.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (id) {
|
||||
fetchContract();
|
||||
}
|
||||
}, [id]);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<PageContainer>
|
||||
<PageHeader
|
||||
title={t('contracts.editContract')}
|
||||
description={t('contracts.editContractDescription')}
|
||||
/>
|
||||
<LoadingState message={t('navigation.loading')} />
|
||||
</PageContainer>
|
||||
);
|
||||
}
|
||||
|
||||
if (error || !contract) {
|
||||
return (
|
||||
<PageContainer>
|
||||
<PageHeader
|
||||
title={t('contracts.editContract')}
|
||||
description={t('contracts.editContractDescription')}
|
||||
/>
|
||||
<div className="bg-red-50 border border-red-200 rounded-lg p-4 flex items-start mb-6">
|
||||
<svg
|
||||
className="w-5 h-5 text-red-600 mr-3 mt-0.5 flex-shrink-0"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||
/>
|
||||
</svg>
|
||||
<div className="flex-1">
|
||||
<p className="text-sm font-medium text-red-800">
|
||||
{error || "Nie znaleziono umowy."}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<Link href="/contracts">
|
||||
<Button variant="outline">
|
||||
<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="M15 19l-7-7 7-7"
|
||||
/>
|
||||
</svg>
|
||||
{t('contracts.backToContracts')}
|
||||
</Button>
|
||||
</Link>
|
||||
</PageContainer>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<PageContainer>
|
||||
<PageHeader
|
||||
title={t('contracts.editContract')}
|
||||
description={`${t('contracts.editing')} ${contract.contract_number}`}
|
||||
action={
|
||||
<Link href={`/contracts/${id}`}>
|
||||
<Button variant="outline" size="sm">
|
||||
<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="M15 19l-7-7 7-7"
|
||||
/>
|
||||
</svg>
|
||||
{t('contracts.backToContract')}
|
||||
</Button>
|
||||
</Link>
|
||||
}
|
||||
/>
|
||||
<div className="max-w-2xl">
|
||||
<ContractForm initialData={contract} />
|
||||
</div>
|
||||
</PageContainer>
|
||||
);
|
||||
}
|
||||
@@ -113,6 +113,24 @@ export default function ContractDetailsPage() {
|
||||
{t('contracts.backToContracts')}
|
||||
</Button>
|
||||
</Link>
|
||||
<Link href={`/contracts/${contractId}/edit`}>
|
||||
<Button variant="outline" size="sm">
|
||||
<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="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
|
||||
/>
|
||||
</svg>
|
||||
{t('contracts.editContract')}
|
||||
</Button>
|
||||
</Link>
|
||||
<Link href={`/projects/new?contract_id=${contractId}`}>
|
||||
<Button variant="primary" size="sm">
|
||||
<svg
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/Card";
|
||||
import Button from "@/components/ui/Button";
|
||||
import Badge from "@/components/ui/Badge";
|
||||
@@ -15,6 +16,7 @@ import { useTranslation } from "@/lib/i18n";
|
||||
|
||||
export default function ContractsMainPage() {
|
||||
const { t } = useTranslation();
|
||||
const { data: session } = useSession();
|
||||
const [contracts, setContracts] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [searchTerm, setSearchTerm] = useState("");
|
||||
@@ -22,17 +24,27 @@ export default function ContractsMainPage() {
|
||||
const [sortBy, setSortBy] = useState("date_signed");
|
||||
const [sortOrder, setSortOrder] = useState("desc");
|
||||
const [statusFilter, setStatusFilter] = useState("all");
|
||||
const [showDeleteModal, setShowDeleteModal] = useState(false);
|
||||
const [contractToDelete, setContractToDelete] = useState(null);
|
||||
const [deleting, setDeleting] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
const [success, setSuccess] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchContracts() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch("/api/contracts");
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to fetch contracts");
|
||||
}
|
||||
const data = await res.json();
|
||||
setContracts(data);
|
||||
setFilteredContracts(data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching contracts:", error);
|
||||
setError("Nie udało się pobrać listy umów. Spróbuj ponownie później.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -93,27 +105,6 @@ export default function ContractsMainPage() {
|
||||
setFilteredContracts(filtered);
|
||||
}, [searchTerm, contracts, sortBy, sortOrder, statusFilter]);
|
||||
|
||||
async function handleDelete(id) {
|
||||
const confirmed = confirm("Czy na pewno chcesz usunąć tę umowę?");
|
||||
if (!confirmed) return;
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/contracts/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
setContracts(contracts.filter((c) => c.contract_id !== id));
|
||||
} else {
|
||||
alert("Błąd podczas usuwania umowy.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting contract:", error);
|
||||
alert("Błąd podczas usuwania umowy.");
|
||||
}
|
||||
}
|
||||
|
||||
// Get contract statistics
|
||||
const getContractStats = () => {
|
||||
const currentDate = new Date();
|
||||
const total = contracts.length;
|
||||
@@ -148,25 +139,50 @@ export default function ContractsMainPage() {
|
||||
}
|
||||
};
|
||||
|
||||
async function handleDelete(id) {
|
||||
const confirmed = confirm("Czy na pewno chcesz usunąć tę umowę?");
|
||||
if (!confirmed) return;
|
||||
const initiateDelete = (contract) => {
|
||||
setContractToDelete(contract);
|
||||
setShowDeleteModal(true);
|
||||
};
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!contractToDelete) return;
|
||||
|
||||
setDeleting(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/contracts/${id}`, {
|
||||
const res = await fetch(`/api/contracts/${contractToDelete.contract_id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
if (res.ok) {
|
||||
setContracts(contracts.filter((c) => c.contract_id !== id));
|
||||
setContracts(contracts.filter((c) => c.contract_id !== contractToDelete.contract_id));
|
||||
setSuccess(`Umowa "${contractToDelete.contract_number}" została usunięta.`);
|
||||
setShowDeleteModal(false);
|
||||
setContractToDelete(null);
|
||||
|
||||
// Auto-hide success message after 5 seconds
|
||||
setTimeout(() => setSuccess(null), 5000);
|
||||
} else {
|
||||
alert("Błąd podczas usuwania umowy.");
|
||||
setError(data.error || "Nie udało się usunąć umowy.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error deleting contract:", error);
|
||||
alert("Błąd podczas usuwania umowy.");
|
||||
setError("Wystąpił błąd podczas usuwania umowy. Spróbuj ponownie.");
|
||||
} finally {
|
||||
setDeleting(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const cancelDelete = () => {
|
||||
if (!deleting) {
|
||||
setShowDeleteModal(false);
|
||||
setContractToDelete(null);
|
||||
setError(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSearchChange = (e) => {
|
||||
setSearchTerm(e.target.value);
|
||||
@@ -264,6 +280,67 @@ export default function ContractsMainPage() {
|
||||
</Button>
|
||||
</Link>{" "}
|
||||
</PageHeader>
|
||||
|
||||
{/* Success Message */}
|
||||
{success && (
|
||||
<div className="mb-6 bg-green-50 border border-green-200 rounded-lg p-4 flex items-start">
|
||||
<svg
|
||||
className="w-5 h-5 text-green-600 mr-3 mt-0.5 flex-shrink-0"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||
/>
|
||||
</svg>
|
||||
<div className="flex-1">
|
||||
<p className="text-sm font-medium text-green-800">{success}</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setSuccess(null)}
|
||||
className="text-green-600 hover:text-green-800 ml-3"
|
||||
>
|
||||
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Error Message */}
|
||||
{error && (
|
||||
<div className="mb-6 bg-red-50 border border-red-200 rounded-lg p-4 flex items-start">
|
||||
<svg
|
||||
className="w-5 h-5 text-red-600 mr-3 mt-0.5 flex-shrink-0"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||
/>
|
||||
</svg>
|
||||
<div className="flex-1">
|
||||
<p className="text-sm font-medium text-red-800">{error}</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setError(null)}
|
||||
className="text-red-600 hover:text-red-800 ml-3"
|
||||
>
|
||||
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Statistics Cards */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
|
||||
<Card>
|
||||
@@ -573,26 +650,28 @@ export default function ContractsMainPage() {
|
||||
</svg>
|
||||
Szczegóły
|
||||
</Link>
|
||||
<Button
|
||||
variant="danger"
|
||||
size="sm"
|
||||
onClick={() => handleDelete(contract.contract_id)}
|
||||
>
|
||||
<svg
|
||||
className="w-4 h-4 mr-1"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
{session?.user?.role === 'team_lead' && (
|
||||
<Button
|
||||
variant="danger"
|
||||
size="sm"
|
||||
onClick={() => initiateDelete(contract)}
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
Usuń
|
||||
</Button>
|
||||
<svg
|
||||
className="w-4 h-4 mr-1"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
Usuń
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
@@ -620,6 +699,124 @@ export default function ContractsMainPage() {
|
||||
</p>{" "}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Delete Confirmation Modal */}
|
||||
{showDeleteModal && contractToDelete && (
|
||||
<div
|
||||
className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-[9999]"
|
||||
onClick={(e) => e.target === e.currentTarget && !deleting && cancelDelete()}
|
||||
>
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg p-6 w-full max-w-md mx-4">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div className="flex items-center">
|
||||
<div className="flex-shrink-0 w-10 h-10 bg-red-100 rounded-full flex items-center justify-center">
|
||||
<svg
|
||||
className="w-6 h-6 text-red-600"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<h3 className="ml-3 text-lg font-semibold text-gray-900 dark:text-white">
|
||||
Potwierdź usunięcie
|
||||
</h3>
|
||||
</div>
|
||||
{!deleting && (
|
||||
<button
|
||||
onClick={cancelDelete}
|
||||
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
|
||||
>
|
||||
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mb-6">
|
||||
<p className="text-gray-700 dark:text-gray-300 mb-3">
|
||||
Czy na pewno chcesz usunąć umowę <strong className="font-semibold">"{contractToDelete.contract_number}"</strong>
|
||||
{contractToDelete.contract_name && (
|
||||
<> — <strong className="font-semibold">{contractToDelete.contract_name}</strong></>
|
||||
)}?
|
||||
</p>
|
||||
<p className="text-sm text-red-600 dark:text-red-400">
|
||||
Ta operacja jest nieodwracalna.
|
||||
</p>
|
||||
{contractToDelete.customer && (
|
||||
<p className="mt-2 text-sm text-gray-600 dark:text-gray-400">
|
||||
Zleceniodawca: <strong>{contractToDelete.customer}</strong>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex gap-3 justify-end">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={cancelDelete}
|
||||
disabled={deleting}
|
||||
>
|
||||
Anuluj
|
||||
</Button>
|
||||
<Button
|
||||
variant="danger"
|
||||
onClick={handleDelete}
|
||||
disabled={deleting}
|
||||
>
|
||||
{deleting ? (
|
||||
<>
|
||||
<svg
|
||||
className="animate-spin -ml-1 mr-2 h-4 w-4 text-white inline-block"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle
|
||||
className="opacity-25"
|
||||
cx="12"
|
||||
cy="12"
|
||||
r="10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="4"
|
||||
/>
|
||||
<path
|
||||
className="opacity-75"
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||
/>
|
||||
</svg>
|
||||
Usuwanie...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<svg
|
||||
className="w-4 h-4 mr-2 inline-block"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"
|
||||
/>
|
||||
</svg>
|
||||
Usuń umowę
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</PageContainer>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/Card";
|
||||
import Button from "@/components/ui/Button";
|
||||
@@ -8,8 +8,9 @@ import { Input } from "@/components/ui/Input";
|
||||
import { formatDateForInput } from "@/lib/utils";
|
||||
import { useTranslation } from "@/lib/i18n";
|
||||
|
||||
export default function ContractForm() {
|
||||
export default function ContractForm({ initialData = null }) {
|
||||
const { t } = useTranslation();
|
||||
const isEdit = !!initialData;
|
||||
const [form, setForm] = useState({
|
||||
contract_number: "",
|
||||
contract_name: "",
|
||||
@@ -23,6 +24,21 @@ export default function ContractForm() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const router = useRouter();
|
||||
|
||||
// Update form when initialData changes (for edit mode)
|
||||
useEffect(() => {
|
||||
if (initialData) {
|
||||
setForm({
|
||||
contract_number: initialData.contract_number || "",
|
||||
contract_name: initialData.contract_name || "",
|
||||
customer_contract_number: initialData.customer_contract_number || "",
|
||||
customer: initialData.customer || "",
|
||||
investor: initialData.investor || "",
|
||||
date_signed: initialData.date_signed || "",
|
||||
finish_date: initialData.finish_date || "",
|
||||
});
|
||||
}
|
||||
}, [initialData]);
|
||||
|
||||
function handleChange(e) {
|
||||
setForm({ ...form, [e.target.name]: e.target.value });
|
||||
}
|
||||
@@ -34,21 +50,32 @@ export default function ContractForm() {
|
||||
try {
|
||||
console.log("Submitting form:", form);
|
||||
|
||||
const res = await fetch("/api/contracts", {
|
||||
method: "POST",
|
||||
const url = isEdit
|
||||
? `/api/contracts/${initialData.contract_id}`
|
||||
: "/api/contracts";
|
||||
const method = isEdit ? "PUT" : "POST";
|
||||
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
const contract = await res.json();
|
||||
router.push(`/contracts/${contract.contract_id}`);
|
||||
router.push(`/contracts/${contract.contract_id || initialData.contract_id}`);
|
||||
} else {
|
||||
alert(t('contracts.failedToCreateContract'));
|
||||
const errorMessage = isEdit
|
||||
? t('contracts.failedToUpdateContract')
|
||||
: t('contracts.failedToCreateContract');
|
||||
alert(errorMessage);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error creating contract:", error);
|
||||
alert(t('contracts.failedToCreateContract'));
|
||||
console.error(`Error ${isEdit ? 'updating' : 'creating'} contract:`, error);
|
||||
const errorMessage = isEdit
|
||||
? t('contracts.failedToUpdateContract')
|
||||
: t('contracts.failedToCreateContract');
|
||||
alert(errorMessage);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -189,7 +216,7 @@ export default function ContractForm() {
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||
></path>
|
||||
</svg>
|
||||
{t('common.creating')}
|
||||
{isEdit ? t('common.updating') : t('common.creating')}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
@@ -203,10 +230,10 @@ export default function ContractForm() {
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M12 4v16m8-8H4"
|
||||
d={isEdit ? "M5 13l4 4L19 7" : "M12 4v16m8-8H4"}
|
||||
/>
|
||||
</svg>
|
||||
{t('contracts.createContract')}
|
||||
{isEdit ? t('contracts.updateContract') : t('contracts.createContract')}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
|
||||
@@ -287,6 +287,11 @@ const translations = {
|
||||
contract: "Umowa",
|
||||
newContract: "Nowa umowa",
|
||||
editContract: "Edytuj umowę",
|
||||
editContractDescription: "Edycja szczegółów umowy",
|
||||
editing: "Edycja umowy",
|
||||
backToContract: "Powrót do umowy",
|
||||
updateContract: "Zaktualizuj umowę",
|
||||
failedToUpdateContract: "Nie udało się zaktualizować umowy. Sprawdź dane i spróbuj ponownie.",
|
||||
deleteContract: "Usuń umowę",
|
||||
contractNumber: "Numer umowy",
|
||||
contractName: "Nazwa umowy",
|
||||
@@ -940,8 +945,14 @@ const translations = {
|
||||
contracts: {
|
||||
title: "Contracts",
|
||||
subtitle: "Manage your contracts and agreements",
|
||||
contract: "Contract",
|
||||
newContract: "New Contract",
|
||||
editContract: "Edit Contract",
|
||||
editContractDescription: "Edit contract details",
|
||||
editing: "Editing contract",
|
||||
backToContract: "Back to Contract",
|
||||
updateContract: "Update Contract",
|
||||
failedToUpdateContract: "Failed to update contract. Please check your data and try again.",
|
||||
deleteContract: "Delete Contract",
|
||||
contractNumber: "Contract Number",
|
||||
contractName: "Contract Name",
|
||||
@@ -968,7 +979,40 @@ const translations = {
|
||||
signedOn: "Signed:",
|
||||
finishOn: "Finish:",
|
||||
customerLabel: "Customer:",
|
||||
investorLabel: "Investor:"
|
||||
investorLabel: "Investor:",
|
||||
loadingContractDetails: "Loading contract details...",
|
||||
contractNotFound: "Contract not found.",
|
||||
backToContracts: "Back to Contracts",
|
||||
addProject: "Add Project",
|
||||
contractInformation: "Contract Information",
|
||||
summary: "Summary",
|
||||
projectsCount: "Projects Count",
|
||||
projects: "projects",
|
||||
contractStatus: "Contract Status",
|
||||
contractDocuments: "Contract Documents",
|
||||
uploadDocument: "Upload Document",
|
||||
associatedProjects: "Associated Projects",
|
||||
noProjectsYet: "No projects yet",
|
||||
getStartedMessage: "Get started by creating the first project for this contract",
|
||||
createFirstProject: "Create First Project",
|
||||
viewDetails: "View Details",
|
||||
createNewContract: "Create New Contract",
|
||||
addNewContractDescription: "Add a new contract to your portfolio",
|
||||
contractDetails: "Contract Details",
|
||||
failedToCreateContract: "Failed to create contract. Please check your data and try again.",
|
||||
uploadDocumentTitle: "Upload Document",
|
||||
descriptionOptional: "Description (optional)",
|
||||
descriptionPlaceholder: "Short description of the document...",
|
||||
uploading: "Uploading...",
|
||||
dropFilesHere: "Drop files here or click to browse",
|
||||
supportedFiles: "PDF, DOC, XLS, Images up to 10MB",
|
||||
chooseFile: "Choose File",
|
||||
failedToUploadFile: "Failed to upload file",
|
||||
loadingFiles: "Loading files...",
|
||||
noDocumentsUploaded: "No documents uploaded",
|
||||
download: "Download",
|
||||
confirmDeleteFile: "Are you sure you want to delete this file?",
|
||||
failedToDeleteFile: "Failed to delete file"
|
||||
},
|
||||
|
||||
tasks: {
|
||||
|
||||
Reference in New Issue
Block a user