feat: implement contact message storage and retrieval functionality
This commit is contained in:
parent
7b4b955c99
commit
f05f5a01ad
3
.gitignore
vendored
3
.gitignore
vendored
@ -40,3 +40,6 @@ yarn-error.log*
|
|||||||
# typescript
|
# typescript
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
next-env.d.ts
|
next-env.d.ts
|
||||||
|
|
||||||
|
# dummy contact submissions (generated at runtime)
|
||||||
|
data/contact-messages.json
|
||||||
|
|||||||
@ -1,6 +1,33 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { mkdir, readFile, writeFile } from "fs/promises";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
// Dummy endpoint sementara, ganti dengan API asli saat sudah tersedia.
|
export const runtime = "nodejs";
|
||||||
|
|
||||||
|
const DATA_DIR = path.join(process.cwd(), "data");
|
||||||
|
const DATA_FILE = path.join(DATA_DIR, "contact-messages.json");
|
||||||
|
|
||||||
|
interface ContactMessage {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
subject: string;
|
||||||
|
message: string;
|
||||||
|
created_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readMessages(): Promise<ContactMessage[]> {
|
||||||
|
try {
|
||||||
|
const raw = await readFile(DATA_FILE, "utf-8");
|
||||||
|
const parsed = JSON.parse(raw);
|
||||||
|
return Array.isArray(parsed) ? parsed : [];
|
||||||
|
} catch {
|
||||||
|
// File belum ada atau isinya rusak, mulai dari array kosong.
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dummy endpoint sementara, data disimpan ke data/contact-messages.json.
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
|
|
||||||
@ -11,7 +38,25 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json({ status: 422, message: "Data belum lengkap", errors: Object.fromEntries(missing.map((field) => [field, [`${field} harus diisi`]])) }, { status: 422 });
|
return NextResponse.json({ status: 422, message: "Data belum lengkap", errors: Object.fromEntries(missing.map((field) => [field, [`${field} harus diisi`]])) }, { status: 422 });
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("[contact] dummy submission:", body);
|
const entry: ContactMessage = {
|
||||||
|
id: crypto.randomUUID(),
|
||||||
|
name: String(body.name).trim(),
|
||||||
|
email: String(body.email).trim(),
|
||||||
|
subject: String(body.subject).trim(),
|
||||||
|
message: String(body.message).trim(),
|
||||||
|
created_at: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
return NextResponse.json({ status: 200, message: "Pesan berhasil dikirim", data: body });
|
try {
|
||||||
|
const messages = await readMessages();
|
||||||
|
messages.push(entry);
|
||||||
|
|
||||||
|
await mkdir(DATA_DIR, { recursive: true });
|
||||||
|
await writeFile(DATA_FILE, JSON.stringify(messages, null, 2), "utf-8");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[contact] gagal menyimpan pesan:", error);
|
||||||
|
return NextResponse.json({ status: 500, message: "Gagal menyimpan pesan" }, { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ status: 200, message: "Pesan berhasil dikirim", data: entry });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user