|
| 1 | +import { Head, useForm, router } from '@inertiajs/react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import AppLayout from '@/Layouts/AppLayout'; |
| 4 | +import { Button } from '@/Components/Common/Button'; |
| 5 | +import type { PageProps } from '@/types'; |
| 6 | + |
| 7 | +interface Tenant { |
| 8 | + id: number; |
| 9 | + name: string; |
| 10 | + slug: string; |
| 11 | + email: string | null; |
| 12 | + phone: string | null; |
| 13 | + address: string | null; |
| 14 | + city: string | null; |
| 15 | + country: string | null; |
| 16 | + currency_code: string; |
| 17 | + timezone: string; |
| 18 | + date_format: string; |
| 19 | + logo_path: string | null; |
| 20 | +} |
| 21 | + |
| 22 | +interface Props extends PageProps { |
| 23 | + tenant: Tenant; |
| 24 | + timezones: string[]; |
| 25 | + currencies: string[]; |
| 26 | + dateFormats: Record<string, string>; |
| 27 | +} |
| 28 | + |
| 29 | +export default function CompanySettings({ tenant, timezones, currencies, dateFormats }: Props) { |
| 30 | + const { data, setData, patch, processing, errors } = useForm({ |
| 31 | + name: tenant.name, |
| 32 | + email: tenant.email ?? '', |
| 33 | + phone: tenant.phone ?? '', |
| 34 | + address: tenant.address ?? '', |
| 35 | + city: tenant.city ?? '', |
| 36 | + country: tenant.country ?? '', |
| 37 | + currency_code: tenant.currency_code, |
| 38 | + timezone: tenant.timezone, |
| 39 | + date_format: tenant.date_format, |
| 40 | + }); |
| 41 | + |
| 42 | + const [logoFile, setLogoFile] = useState<File | null>(null); |
| 43 | + |
| 44 | + function submitSettings(e: React.FormEvent) { |
| 45 | + e.preventDefault(); |
| 46 | + patch('/settings/company'); |
| 47 | + } |
| 48 | + |
| 49 | + function submitLogo(e: React.FormEvent) { |
| 50 | + e.preventDefault(); |
| 51 | + if (!logoFile) return; |
| 52 | + const form = new FormData(); |
| 53 | + form.append('logo', logoFile); |
| 54 | + router.post('/settings/company/logo', form); |
| 55 | + } |
| 56 | + |
| 57 | + return ( |
| 58 | + <AppLayout> |
| 59 | + <Head title="Company Settings" /> |
| 60 | + <div className="max-w-2xl space-y-8"> |
| 61 | + <h1 className="text-2xl font-semibold text-slate-900">Company Settings</h1> |
| 62 | + |
| 63 | + <form onSubmit={submitSettings} className="rounded-lg border border-slate-200 bg-white p-6 shadow-sm space-y-5"> |
| 64 | + <h2 className="text-sm font-semibold text-slate-700 border-b border-slate-100 pb-2">Company Profile</h2> |
| 65 | + |
| 66 | + <div className="grid grid-cols-2 gap-4"> |
| 67 | + <div> |
| 68 | + <label className="block text-xs font-medium text-slate-600 mb-1">Company Name *</label> |
| 69 | + <input type="text" required value={data.name} onChange={(e) => setData('name', e.target.value)} |
| 70 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 71 | + {errors.name && <p className="text-xs text-red-600 mt-1">{errors.name}</p>} |
| 72 | + </div> |
| 73 | + <div> |
| 74 | + <label className="block text-xs font-medium text-slate-600 mb-1">Email</label> |
| 75 | + <input type="email" value={data.email} onChange={(e) => setData('email', e.target.value)} |
| 76 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 77 | + </div> |
| 78 | + <div> |
| 79 | + <label className="block text-xs font-medium text-slate-600 mb-1">Phone</label> |
| 80 | + <input type="text" value={data.phone} onChange={(e) => setData('phone', e.target.value)} |
| 81 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 82 | + </div> |
| 83 | + <div> |
| 84 | + <label className="block text-xs font-medium text-slate-600 mb-1">City</label> |
| 85 | + <input type="text" value={data.city} onChange={(e) => setData('city', e.target.value)} |
| 86 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 87 | + </div> |
| 88 | + <div> |
| 89 | + <label className="block text-xs font-medium text-slate-600 mb-1">Country</label> |
| 90 | + <input type="text" value={data.country} onChange={(e) => setData('country', e.target.value)} |
| 91 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + |
| 95 | + <div> |
| 96 | + <label className="block text-xs font-medium text-slate-600 mb-1">Address</label> |
| 97 | + <textarea rows={3} value={data.address} onChange={(e) => setData('address', e.target.value)} |
| 98 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 99 | + </div> |
| 100 | + |
| 101 | + <h2 className="text-sm font-semibold text-slate-700 border-b border-slate-100 pb-2 pt-2">Localisation</h2> |
| 102 | + |
| 103 | + <div className="grid grid-cols-3 gap-4"> |
| 104 | + <div> |
| 105 | + <label className="block text-xs font-medium text-slate-600 mb-1">Base Currency *</label> |
| 106 | + <select value={data.currency_code} onChange={(e) => setData('currency_code', e.target.value)} |
| 107 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none"> |
| 108 | + {currencies.map((c) => <option key={c} value={c}>{c}</option>)} |
| 109 | + </select> |
| 110 | + </div> |
| 111 | + <div> |
| 112 | + <label className="block text-xs font-medium text-slate-600 mb-1">Timezone *</label> |
| 113 | + <select value={data.timezone} onChange={(e) => setData('timezone', e.target.value)} |
| 114 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none"> |
| 115 | + {timezones.map((tz) => <option key={tz} value={tz}>{tz}</option>)} |
| 116 | + </select> |
| 117 | + </div> |
| 118 | + <div> |
| 119 | + <label className="block text-xs font-medium text-slate-600 mb-1">Date Format *</label> |
| 120 | + <select value={data.date_format} onChange={(e) => setData('date_format', e.target.value)} |
| 121 | + className="w-full rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none"> |
| 122 | + {Object.entries(dateFormats).map(([fmt, label]) => ( |
| 123 | + <option key={fmt} value={fmt}>{label}</option> |
| 124 | + ))} |
| 125 | + </select> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + |
| 129 | + <div className="flex justify-end pt-2"> |
| 130 | + <Button type="submit" disabled={processing}>{processing ? 'Saving…' : 'Save Settings'}</Button> |
| 131 | + </div> |
| 132 | + </form> |
| 133 | + |
| 134 | + {/* Logo upload */} |
| 135 | + <form onSubmit={submitLogo} className="rounded-lg border border-slate-200 bg-white p-6 shadow-sm space-y-4"> |
| 136 | + <h2 className="text-sm font-semibold text-slate-700 border-b border-slate-100 pb-2">Company Logo</h2> |
| 137 | + {tenant.logo_path && ( |
| 138 | + <img src={`/storage/${tenant.logo_path}`} alt="Company logo" className="h-16 object-contain rounded" /> |
| 139 | + )} |
| 140 | + <div> |
| 141 | + <input type="file" accept="image/*" onChange={(e) => setLogoFile(e.target.files?.[0] ?? null)} |
| 142 | + className="text-sm text-slate-600" /> |
| 143 | + <p className="mt-1 text-xs text-slate-400">PNG, JPG, GIF or SVG · max 1 MB</p> |
| 144 | + </div> |
| 145 | + <div className="flex justify-end"> |
| 146 | + <Button type="submit" variant="secondary" disabled={!logoFile}>Upload Logo</Button> |
| 147 | + </div> |
| 148 | + </form> |
| 149 | + </div> |
| 150 | + </AppLayout> |
| 151 | + ); |
| 152 | +} |
0 commit comments