53 lines
1.9 KiB
TypeScript
53 lines
1.9 KiB
TypeScript
/**
|
|
* Appwrite-Anbindung für das Kontaktformular.
|
|
*
|
|
* In der Appwrite Console anlegen:
|
|
* 1. Database (z. B. ID: "contacts")
|
|
* 2. Collection (z. B. ID: "messages") mit String-Attributen: name, email, company, message
|
|
* 3. Unter "Settings" der Collection: Create-Berechtigung für "Any" aktivieren (öffentliches Formular)
|
|
* 4. IDs in .env setzen: VITE_APPWRITE_DATABASE_ID, VITE_APPWRITE_CONTACT_COLLECTION_ID
|
|
*/
|
|
import { Client, Databases, ID } from "appwrite";
|
|
|
|
const CONTACT_DATABASE_ID = import.meta.env.VITE_APPWRITE_DATABASE_ID ?? "698124a20035e8f6dc42";
|
|
const CONTACT_COLLECTION_ID = import.meta.env.VITE_APPWRITE_CONTACT_COLLECTION_ID ?? "contact_submissions";
|
|
|
|
function getDatabases(): Databases {
|
|
const endpoint = import.meta.env.VITE_APPWRITE_ENDPOINT;
|
|
const projectId = import.meta.env.VITE_APPWRITE_PROJECT_ID;
|
|
if (!endpoint || !projectId) {
|
|
throw new Error(
|
|
"Appwrite ist nicht konfiguriert. Bitte VITE_APPWRITE_ENDPOINT und VITE_APPWRITE_PROJECT_ID setzen. " +
|
|
"Lokal: .env anlegen (z. B. mit „npm run setup:env“) und Build/Dev-Server neu starten. " +
|
|
"Server: gleiche Variablen in der Build-Umgebung setzen (z. B. in .env vor „npm run build“)."
|
|
);
|
|
}
|
|
const client = new Client().setEndpoint(endpoint).setProject(projectId);
|
|
return new Databases(client);
|
|
}
|
|
|
|
let _databases: Databases | null = null;
|
|
|
|
export type ContactFormData = {
|
|
name: string;
|
|
email: string;
|
|
company: string;
|
|
message: string;
|
|
};
|
|
|
|
export async function createContactDocument(data: ContactFormData) {
|
|
if (!_databases) _databases = getDatabases();
|
|
return _databases.createDocument<ContactFormData>(
|
|
CONTACT_DATABASE_ID,
|
|
CONTACT_COLLECTION_ID,
|
|
ID.unique(),
|
|
{
|
|
name: data.name,
|
|
email: data.email,
|
|
company: data.company,
|
|
message: data.message,
|
|
},
|
|
[] // Keine Dokument-Permissions; nur Collection-Berechtigung „Create für Any“ wird genutzt
|
|
);
|
|
}
|