Skip to content

Commit 16bea8f

Browse files
committed
Fixed build issues
1 parent 12be7c1 commit 16bea8f

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { NextResponse } from 'next/server';
2-
import fs from 'fs/promises';
3-
import path from 'path';
1+
import { NextResponse } from "next/server";
2+
import fs from "fs/promises";
3+
import path from "path";
44

5-
const submissionsDir = path.resolve(process.cwd(), 'contact-submissions');
6-
const submissionsFile = path.resolve(submissionsDir, 'submissions.json');
5+
const submissionsDir = path.resolve(process.cwd(), "contact-submissions");
6+
const submissionsFile = path.resolve(submissionsDir, "submissions.json");
77

88
async function getSubmissions() {
99
try {
10-
const data = await fs.readFile(submissionsFile, 'utf8');
10+
const data = await fs.readFile(submissionsFile, "utf8");
1111
return JSON.parse(data);
1212
} catch (error) {
1313
return [];
@@ -20,6 +20,9 @@ export async function GET() {
2020
return NextResponse.json(submissions, { status: 200 });
2121
} catch (error) {
2222
console.error(error);
23-
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
23+
return NextResponse.json(
24+
{ message: "Internal Server Error" },
25+
{ status: 500 }
26+
);
2427
}
2528
}
Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
import { NextRequest, NextResponse } from 'next/server';
2-
import fs from 'fs/promises';
3-
import path from 'path';
1+
import { NextRequest, NextResponse } from "next/server";
2+
import fs from "fs/promises";
3+
import path from "path";
44

5-
const submissionsDir = path.resolve(process.cwd(), 'contact-submissions');
6-
const submissionsFile = path.resolve(submissionsDir, 'submissions.json');
5+
const submissionsDir = path.resolve(process.cwd(), "contact-submissions");
6+
const submissionsFile = path.resolve(submissionsDir, "submissions.json");
77

88
async function ensureSubmissionsDirExists() {
99
try {
1010
await fs.mkdir(submissionsDir, { recursive: true });
1111
} catch (error) {
12-
console.error('Error creating submissions directory:', error);
12+
console.error("Error creating submissions directory:", error);
1313
}
1414
}
1515

16-
async function getSubmissions() {
16+
interface Submission {
17+
name: string;
18+
email: string;
19+
message: string;
20+
date: string;
21+
}
22+
23+
async function getSubmissions(): Promise<Submission[]> {
1724
try {
1825
await ensureSubmissionsDirExists();
19-
const data = await fs.readFile(submissionsFile, 'utf8');
26+
const data = await fs.readFile(submissionsFile, "utf8");
2027
return JSON.parse(data);
21-
} catch (error) {
28+
} catch {
2229
return [];
2330
}
2431
}
2532

26-
async function saveSubmission(data: any) {
33+
async function saveSubmission(data: Submission) {
2734
const submissions = await getSubmissions();
2835
submissions.push(data);
2936
await fs.writeFile(submissionsFile, JSON.stringify(submissions, null, 2));
@@ -34,7 +41,10 @@ export async function POST(req: NextRequest) {
3441
const { name, email, message } = await req.json();
3542

3643
if (!name || !email || !message) {
37-
return NextResponse.json({ message: 'Missing required fields' }, { status: 400 });
44+
return NextResponse.json(
45+
{ message: "Missing required fields" },
46+
{ status: 400 }
47+
);
3848
}
3949

4050
const newSubmission = {
@@ -46,9 +56,12 @@ export async function POST(req: NextRequest) {
4656

4757
await saveSubmission(newSubmission);
4858

49-
return NextResponse.json({ message: 'Submission saved' }, { status: 200 });
59+
return NextResponse.json({ message: "Submission saved" }, { status: 200 });
5060
} catch (error) {
5161
console.error(error);
52-
return NextResponse.json({ message: 'Internal Server Error' }, { status: 500 });
62+
return NextResponse.json(
63+
{ message: "Internal Server Error" },
64+
{ status: 500 }
65+
);
5366
}
5467
}

next-js-app/src/app/contact-submissions/page.tsx

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1-
'use client';
1+
"use client";
22

3-
import { useState, useEffect } from 'react';
3+
import { useState, useEffect } from "react";
44

5-
const PASSWORD = 'password'; // Replace with a strong password
5+
const PASSWORD = process.env.SUBMISSIONS_PASSWORD; // Replace with a strong password
6+
7+
interface Submission {
8+
name: string;
9+
email: string;
10+
message: string;
11+
date: string;
12+
}
613

714
export default function ContactSubmissions() {
8-
const [password, setPassword] = useState('');
15+
const [password, setPassword] = useState("");
916
const [authenticated, setAuthenticated] = useState(false);
10-
const [submissions, setSubmissions] = useState([]);
11-
const [error, setError] = useState('');
17+
const [submissions, setSubmissions] = useState<Submission[]>([]);
18+
const [error, setError] = useState("");
1219

1320
const handlePasswordSubmit = (e: React.FormEvent) => {
1421
e.preventDefault();
1522
if (password === PASSWORD) {
1623
setAuthenticated(true);
1724
} else {
18-
setError('Incorrect password');
25+
setError("Incorrect password");
1926
}
2027
};
2128

2229
useEffect(() => {
2330
if (authenticated) {
2431
const fetchSubmissions = async () => {
2532
try {
26-
const response = await fetch('/api/contact-submissions');
33+
const response = await fetch("/api/contact-submissions");
2734
if (response.ok) {
2835
const data = await response.json();
2936
setSubmissions(data);
3037
} else {
31-
setError('Failed to fetch submissions.');
38+
setError("Failed to fetch submissions.");
3239
}
3340
} catch (error) {
34-
setError('Failed to fetch submissions.');
41+
setError("Failed to fetch submissions.");
3542
}
3643
};
3744
fetchSubmissions();
@@ -41,10 +48,16 @@ export default function ContactSubmissions() {
4148
if (!authenticated) {
4249
return (
4350
<div className="flex items-center justify-center h-screen">
44-
<form onSubmit={handlePasswordSubmit} className="bg-white dark:bg-gray-800 p-8 rounded-lg shadow-lg">
51+
<form
52+
onSubmit={handlePasswordSubmit}
53+
className="bg-white dark:bg-gray-800 p-8 rounded-lg shadow-lg"
54+
>
4555
<h2 className="text-2xl font-bold mb-4">Enter Password</h2>
4656
<div className="mb-4">
47-
<label htmlFor="password" a className="block text-sm font-medium text-gray-700 dark:text-gray-300">
57+
<label
58+
htmlFor="password"
59+
className="block text-sm font-medium text-gray-700 dark:text-gray-300"
60+
>
4861
Password
4962
</label>
5063
<input
@@ -74,12 +87,24 @@ export default function ContactSubmissions() {
7487
<p>No submissions yet.</p>
7588
) : (
7689
<div className="space-y-4">
77-
{submissions.map((submission: any, index: number) => (
78-
<div key={index} className="bg-white dark:bg-gray-800 p-4 rounded-lg shadow">
79-
<p><strong>Name:</strong> {submission.name}</p>
80-
<p><strong>Email:</strong> {submission.email}</p>
81-
<p><strong>Message:</strong> {submission.message}</p>
82-
<p><strong>Date:</strong> {new Date(submission.date).toLocaleString()}</p>
90+
{submissions.map((submission, index) => (
91+
<div
92+
key={index}
93+
className="bg-white dark:bg-gray-800 p-4 rounded-lg shadow"
94+
>
95+
<p>
96+
<strong>Name:</strong> {submission.name}
97+
</p>
98+
<p>
99+
<strong>Email:</strong> {submission.email}
100+
</p>
101+
<p>
102+
<strong>Message:</strong> {submission.message}
103+
</p>
104+
<p>
105+
<strong>Date:</strong>{" "}
106+
{new Date(submission.date).toLocaleString()}
107+
</p>
83108
</div>
84109
))}
85110
</div>

0 commit comments

Comments
 (0)