forked from fahrPool/fahrPool
feat: App-Icon-Upload auf der Firmenseite
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
befec482f9
commit
0a6f51c39b
|
|
@ -5,6 +5,7 @@ import { createClient } from "@/lib/supabase/server";
|
|||
import { ladeAdminKontext } from "@/lib/firma/admin-kontext";
|
||||
import { validateFirmaEinstellungen } from "@/lib/firma/einstellungen";
|
||||
import { logoPfad, validateLogo } from "@/lib/firma/logo";
|
||||
import { appIconPfad, validateAppIcon } from "@/lib/pwa/app-icon";
|
||||
|
||||
export type ActionResult = { success: true } | { success: false; error: string };
|
||||
|
||||
|
|
@ -70,3 +71,44 @@ export async function entferneLogo(): Promise<ActionResult> {
|
|||
revalidatePath("/", "layout");
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function ladeAppIconHoch(formData: FormData): Promise<ActionResult> {
|
||||
const kontext = await ladeAdminKontext();
|
||||
if (!kontext.ok) return { success: false, error: kontext.error };
|
||||
|
||||
const datei = formData.get("app_icon");
|
||||
if (!(datei instanceof File)) return { success: false, error: "Bitte eine Datei auswählen." };
|
||||
const validation = validateAppIcon({ type: datei.type, size: datei.size });
|
||||
if (!validation.valid) return { success: false, error: validation.error };
|
||||
|
||||
const supabase = await createClient();
|
||||
const pfad = appIconPfad(kontext.firma.id, validation.endung);
|
||||
const { error: uploadFehler } = await supabase.storage
|
||||
.from("firmen-logos")
|
||||
.upload(pfad, datei, { contentType: datei.type });
|
||||
if (uploadFehler) return { success: false, error: "App-Icon-Upload fehlgeschlagen." };
|
||||
|
||||
const { error } = await supabase.from("firmen").update({ app_icon_pfad: pfad }).eq("id", kontext.firma.id);
|
||||
if (error) {
|
||||
await supabase.storage.from("firmen-logos").remove([pfad]);
|
||||
return { success: false, error: "App-Icon konnte nicht gespeichert werden." };
|
||||
}
|
||||
if (kontext.firma.app_icon_pfad) {
|
||||
await supabase.storage.from("firmen-logos").remove([kontext.firma.app_icon_pfad]);
|
||||
}
|
||||
revalidatePath("/", "layout");
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function entferneAppIcon(): Promise<ActionResult> {
|
||||
const kontext = await ladeAdminKontext();
|
||||
if (!kontext.ok) return { success: false, error: kontext.error };
|
||||
if (!kontext.firma.app_icon_pfad) return { success: true };
|
||||
|
||||
const supabase = await createClient();
|
||||
const { error } = await supabase.from("firmen").update({ app_icon_pfad: null }).eq("id", kontext.firma.id);
|
||||
if (error) return { success: false, error: "App-Icon konnte nicht entfernt werden." };
|
||||
await supabase.storage.from("firmen-logos").remove([kontext.firma.app_icon_pfad]);
|
||||
revalidatePath("/", "layout");
|
||||
return { success: true };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@ import { createAdminClient } from "@/lib/supabase/admin";
|
|||
import { ladeAdminKontext } from "@/lib/firma/admin-kontext";
|
||||
import { ladeFirmenSmtp, type SmtpAnzeige } from "@/lib/firma/smtp";
|
||||
import { FirmaEinstellungenForm } from "@/components/firma/FirmaEinstellungenForm";
|
||||
import { AppIconForm } from "@/components/firma/AppIconForm";
|
||||
import { LogoForm } from "@/components/firma/LogoForm";
|
||||
import { SmtpForm } from "@/components/firma/SmtpForm";
|
||||
import { iconUrl } from "@/lib/pwa/app-icon";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
|
||||
export default async function FirmaPage() {
|
||||
|
|
@ -38,6 +40,11 @@ export default async function FirmaPage() {
|
|||
<h2 className="mb-4 font-semibold text-asphalt-grey">Logo</h2>
|
||||
<LogoForm hatLogo={Boolean(kontext.firma.logo_pfad)} />
|
||||
</Card>
|
||||
<Card>
|
||||
<h2 className="mb-4 font-semibold text-asphalt-grey">App-Icon</h2>
|
||||
{/* iconUrl nutzt node:crypto, deshalb hier im Server berechnet */}
|
||||
<AppIconForm hatAppIcon={Boolean(kontext.firma.app_icon_pfad)} vorschauUrl={iconUrl(kontext.firma, "192")} />
|
||||
</Card>
|
||||
<Card>
|
||||
<h2 className="mb-4 font-semibold text-asphalt-grey">E-Mail-Versand (SMTP)</h2>
|
||||
<SmtpForm smtp={smtp} />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import { entferneAppIcon, ladeAppIconHoch } from "@/app/firma/actions";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { FIELD_CLASSES, FormField } from "@/components/ui/FormField";
|
||||
|
||||
export function AppIconForm({ hatAppIcon, vorschauUrl }: { hatAppIcon: boolean; vorschauUrl: string }) {
|
||||
const [fehler, setFehler] = useState<string | null>(null);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const form = event.currentTarget;
|
||||
const formData = new FormData(form);
|
||||
startTransition(async () => {
|
||||
const ergebnis = await ladeAppIconHoch(formData);
|
||||
setFehler(ergebnis.success ? null : ergebnis.error);
|
||||
if (ergebnis.success) form.reset();
|
||||
});
|
||||
}
|
||||
|
||||
function handleEntfernen() {
|
||||
startTransition(async () => {
|
||||
const ergebnis = await entferneAppIcon();
|
||||
setFehler(ergebnis.success ? null : ergebnis.error);
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit}>
|
||||
{fehler && (
|
||||
<div className="mb-4">
|
||||
<Alert>{fehler}</Alert>
|
||||
</div>
|
||||
)}
|
||||
{/* eslint-disable-next-line @next/next/no-img-element -- oeffentliche App-Icon-Route, zeigt auch den Fallback */}
|
||||
<img src={vorschauUrl} alt="Aktuelles App-Icon" className="mb-4 h-24 w-24 rounded-2xl object-cover" />
|
||||
<FormField label="App-Icon (PNG, JPG oder SVG, quadratisch, max. 1 MB)" htmlFor="firma-app-icon">
|
||||
<input
|
||||
id="firma-app-icon"
|
||||
name="app_icon"
|
||||
type="file"
|
||||
accept="image/png,image/jpeg,image/svg+xml"
|
||||
required
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<p className="mb-4 text-sm text-asphalt-grey/70">
|
||||
Erscheint auf dem Startbildschirm. Ohne eigenes App-Icon wird es aus dem Logo erzeugt. Bereits installierte
|
||||
iPhone-Apps übernehmen ein neues Icon erst nach Neuinstallation.
|
||||
</p>
|
||||
<div className="flex flex-col gap-3">
|
||||
<Button type="submit" disabled={isPending}>
|
||||
App-Icon hochladen
|
||||
</Button>
|
||||
{hatAppIcon && (
|
||||
<Button type="button" variant="secondary" disabled={isPending} onClick={handleEntfernen}>
|
||||
App-Icon entfernen
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue