Add full project files
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
"use client";
|
||||
|
||||
import { useForm } from "react-hook-form";
|
||||
import type { Resolver } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { studioSettingsSchema, type StudioSettingsFormValues } from "@/lib/validations";
|
||||
import { updateStudioSettings } from "@/actions/settings";
|
||||
import { CURRENCIES } from "@/lib/constants";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { toast } from "sonner";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import type { StudioSettings } from "@prisma/client";
|
||||
|
||||
interface Props {
|
||||
initialData: StudioSettings;
|
||||
}
|
||||
|
||||
export function SettingsForm({ initialData }: Props) {
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
setValue,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm<StudioSettingsFormValues>({
|
||||
resolver: zodResolver(studioSettingsSchema) as Resolver<StudioSettingsFormValues>,
|
||||
defaultValues: {
|
||||
studioName: initialData.studioName,
|
||||
studioEmail: initialData.studioEmail ?? "",
|
||||
studioPhone: initialData.studioPhone ?? "",
|
||||
studioAddress: initialData.studioAddress ?? "",
|
||||
logoUrl: initialData.logoUrl ?? "",
|
||||
vatNumber: initialData.vatNumber ?? "",
|
||||
companyRegNumber: initialData.companyRegNumber ?? "",
|
||||
invoiceHeader: initialData.invoiceHeader ?? "",
|
||||
invoiceFooter: initialData.invoiceFooter ?? "",
|
||||
defaultTermsAndConditions: initialData.defaultTermsAndConditions ?? "",
|
||||
defaultCurrency: initialData.defaultCurrency,
|
||||
defaultMarkupPercent: Number(initialData.defaultMarkupPercent),
|
||||
defaultDiscountPercent: Number(initialData.defaultDiscountPercent),
|
||||
defaultTaxPercent: Number(initialData.defaultTaxPercent),
|
||||
},
|
||||
});
|
||||
|
||||
const onSubmit = async (data: StudioSettingsFormValues) => {
|
||||
const result = await updateStudioSettings(data);
|
||||
if ("success" in result && result.success) {
|
||||
toast.success("Settings saved");
|
||||
} else {
|
||||
toast.error("Failed to save settings");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form
|
||||
onSubmit={handleSubmit(onSubmit as Parameters<typeof handleSubmit>[0])}
|
||||
className="space-y-8"
|
||||
>
|
||||
{/* Studio Identity */}
|
||||
<section className="space-y-4">
|
||||
<div>
|
||||
<h2 className="text-base font-semibold text-white">Studio Identity</h2>
|
||||
<p className="text-sm text-zinc-400">Your company details shown on quotes and invoices.</p>
|
||||
</div>
|
||||
<Separator className="bg-zinc-800" />
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Studio Name *</Label>
|
||||
<Input {...register("studioName")} className="bg-zinc-900 border-zinc-700" />
|
||||
{errors.studioName && <p className="text-red-400 text-xs">{errors.studioName.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Email</Label>
|
||||
<Input {...register("studioEmail")} type="email" className="bg-zinc-900 border-zinc-700" />
|
||||
{errors.studioEmail && <p className="text-red-400 text-xs">{errors.studioEmail.message}</p>}
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Phone</Label>
|
||||
<Input {...register("studioPhone")} className="bg-zinc-900 border-zinc-700" />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>VAT Number</Label>
|
||||
<Input {...register("vatNumber")} className="bg-zinc-900 border-zinc-700" />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Company Reg. Number</Label>
|
||||
<Input {...register("companyRegNumber")} className="bg-zinc-900 border-zinc-700" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Physical / Billing Address</Label>
|
||||
<Textarea
|
||||
{...register("studioAddress")}
|
||||
className="bg-zinc-900 border-zinc-700 resize-none"
|
||||
rows={3}
|
||||
placeholder={"123 Studio Lane\nCape Town, 8001\nSouth Africa"}
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Branding */}
|
||||
<section className="space-y-4">
|
||||
<div>
|
||||
<h2 className="text-base font-semibold text-white">Branding</h2>
|
||||
<p className="text-sm text-zinc-400">Logo and quote/invoice header/footer content.</p>
|
||||
</div>
|
||||
<Separator className="bg-zinc-800" />
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Logo URL</Label>
|
||||
<Input
|
||||
{...register("logoUrl")}
|
||||
className="bg-zinc-900 border-zinc-700"
|
||||
placeholder="https://yourstudio.com/logo.png"
|
||||
/>
|
||||
<p className="text-xs text-zinc-500">Paste a publicly accessible image URL. Appears at the top of every PDF quote.</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Quote / Invoice Header</Label>
|
||||
<Textarea
|
||||
{...register("invoiceHeader")}
|
||||
className="bg-zinc-900 border-zinc-700 resize-none font-mono text-sm"
|
||||
rows={4}
|
||||
placeholder="Shown at the top of every quote, below the logo. Plain text or basic HTML."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Quote / Invoice Footer</Label>
|
||||
<Textarea
|
||||
{...register("invoiceFooter")}
|
||||
className="bg-zinc-900 border-zinc-700 resize-none font-mono text-sm"
|
||||
rows={4}
|
||||
placeholder="Shown at the bottom of every quote. Plain text or basic HTML."
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Default Terms */}
|
||||
<section className="space-y-4">
|
||||
<div>
|
||||
<h2 className="text-base font-semibold text-white">Default Terms & Conditions</h2>
|
||||
<p className="text-sm text-zinc-400">Pre-filled on every new quote. Can be overridden per-job.</p>
|
||||
</div>
|
||||
<Separator className="bg-zinc-800" />
|
||||
<div className="space-y-1.5">
|
||||
<Textarea
|
||||
{...register("defaultTermsAndConditions")}
|
||||
className="bg-zinc-900 border-zinc-700 resize-none text-sm"
|
||||
rows={6}
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Financial Defaults */}
|
||||
<section className="space-y-4">
|
||||
<div>
|
||||
<h2 className="text-base font-semibold text-white">Financial Defaults</h2>
|
||||
<p className="text-sm text-zinc-400">Applied to new jobs and rate cards. Can be overridden per-job.</p>
|
||||
</div>
|
||||
<Separator className="bg-zinc-800" />
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Default Currency</Label>
|
||||
<Select
|
||||
onValueChange={(v) => setValue("defaultCurrency", v ?? "ZAR")}
|
||||
defaultValue={initialData.defaultCurrency}
|
||||
>
|
||||
<SelectTrigger className="bg-zinc-900 border-zinc-700"><SelectValue /></SelectTrigger>
|
||||
<SelectContent className="bg-zinc-900 border-zinc-700">
|
||||
{CURRENCIES.map((c) => (
|
||||
<SelectItem key={c.code} value={c.code}>{c.symbol} {c.name}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Default Markup %</Label>
|
||||
<Input
|
||||
{...register("defaultMarkupPercent")}
|
||||
type="number"
|
||||
min={0}
|
||||
max={100}
|
||||
step={0.5}
|
||||
className="bg-zinc-900 border-zinc-700"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Default Discount %</Label>
|
||||
<Input
|
||||
{...register("defaultDiscountPercent")}
|
||||
type="number"
|
||||
min={0}
|
||||
max={100}
|
||||
step={0.5}
|
||||
className="bg-zinc-900 border-zinc-700"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Default Tax (VAT) %</Label>
|
||||
<Input
|
||||
{...register("defaultTaxPercent")}
|
||||
type="number"
|
||||
min={0}
|
||||
max={100}
|
||||
step={0.5}
|
||||
className="bg-zinc-900 border-zinc-700"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div className="flex justify-end pt-2">
|
||||
<Button type="submit" disabled={isSubmitting} className="min-w-32">
|
||||
{isSubmitting ? <Loader2 className="h-4 w-4 animate-spin" /> : "Save Settings"}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user