form
This commit is contained in:
42
src/lib/appwrite.ts
Normal file
42
src/lib/appwrite.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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 client = new Client()
|
||||
.setEndpoint(import.meta.env.VITE_APPWRITE_ENDPOINT)
|
||||
.setProject(import.meta.env.VITE_APPWRITE_PROJECT_ID);
|
||||
|
||||
const databases = new Databases(client);
|
||||
|
||||
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";
|
||||
|
||||
export type ContactFormData = {
|
||||
name: string;
|
||||
email: string;
|
||||
company: string;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export async function createContactDocument(data: ContactFormData) {
|
||||
return databases.createDocument<ContactFormData>(
|
||||
CONTACT_DATABASE_ID,
|
||||
CONTACT_COLLECTION_ID,
|
||||
ID.unique(),
|
||||
{
|
||||
name: data.name,
|
||||
email: data.email,
|
||||
company: data.company,
|
||||
message: data.message,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export { client, databases };
|
||||
@@ -6,6 +6,7 @@ import { Textarea } from "@/components/ui/textarea";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { ArrowLeft, Send } from "lucide-react";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
import { createContactDocument } from "@/lib/appwrite";
|
||||
|
||||
const Contact = () => {
|
||||
const { toast } = useToast();
|
||||
@@ -30,16 +31,23 @@ const Contact = () => {
|
||||
e.preventDefault();
|
||||
setIsSubmitting(true);
|
||||
|
||||
// Simulate form submission
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
|
||||
toast({
|
||||
title: "Nachricht gesendet!",
|
||||
description: "Wir melden uns innerhalb von 24 Stunden bei Ihnen.",
|
||||
});
|
||||
|
||||
setFormData({ name: "", email: "", company: "", message: "" });
|
||||
setIsSubmitting(false);
|
||||
try {
|
||||
await createContactDocument(formData);
|
||||
toast({
|
||||
title: "Nachricht gesendet!",
|
||||
description: "Wir melden uns innerhalb von 24 Stunden bei Ihnen.",
|
||||
});
|
||||
setFormData({ name: "", email: "", company: "", message: "" });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Speichern fehlgeschlagen.";
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Fehler beim Senden",
|
||||
description: message,
|
||||
});
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
11
src/vite-env.d.ts
vendored
11
src/vite-env.d.ts
vendored
@@ -1 +1,12 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_APPWRITE_PROJECT_ID: string;
|
||||
readonly VITE_APPWRITE_ENDPOINT: string;
|
||||
readonly VITE_APPWRITE_DATABASE_ID?: string;
|
||||
readonly VITE_APPWRITE_CONTACT_COLLECTION_ID?: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user