import { useState, useRef, useEffect } from 'react' import { createFileRoute } from '@tanstack/react-router' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { queryOptions } from '@tanstack/react-query' import { api } from '@/lib/api-client' import { useAuthStore } from '@/stores/auth.store' 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 { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Skeleton } from '@/components/ui/skeleton' import { Badge } from '@/components/ui/badge' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog' import { AvatarUpload } from '@/components/shared/avatar-upload' import { Switch } from '@/components/ui/switch' import { moduleListOptions, moduleMutations, moduleKeys, type ModuleConfig } from '@/api/modules' import { Save, Plus, Trash2, MapPin, Building, ImageIcon, Blocks, Lock } from 'lucide-react' import { toast } from 'sonner' interface StoreSettings { id: string name: string phone: string | null email: string | null address: { street?: string; city?: string; state?: string; zip?: string } | null timezone: string logoFileId: string | null notes: string | null } interface Location { id: string name: string phone: string | null email: string | null address: { street?: string; city?: string; state?: string; zip?: string } | null timezone: string | null isActive: boolean } function storeOptions() { return queryOptions({ queryKey: ['store'], queryFn: () => api.get('/v1/store'), }) } function locationsOptions() { return queryOptions({ queryKey: ['locations'], queryFn: () => api.get<{ data: Location[] }>('/v1/locations'), }) } export const Route = createFileRoute('/_authenticated/settings')({ component: SettingsPage, }) function SettingsPage() { const queryClient = useQueryClient() const { data: store, isLoading } = useQuery(storeOptions()) const { data: locationsData } = useQuery(locationsOptions()) const [addLocationOpen, setAddLocationOpen] = useState(false) // Store edit state const [editing, setEditing] = useState(false) const [fields, setFields] = useState>({}) const updateMutation = useMutation({ mutationFn: (data: Record) => api.patch('/v1/store', data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['store'] }) toast.success('Store settings updated') setEditing(false) }, onError: (err) => toast.error(err.message), }) function startEdit() { if (!store) return setFields({ name: store.name, phone: store.phone ?? '', email: store.email ?? '', street: store.address?.street ?? '', city: store.address?.city ?? '', state: store.address?.state ?? '', zip: store.address?.zip ?? '', timezone: store.timezone, notes: store.notes ?? '', }) setEditing(true) } function saveEdit() { updateMutation.mutate({ name: fields.name, phone: fields.phone || null, email: fields.email || null, address: { street: fields.street, city: fields.city, state: fields.state, zip: fields.zip }, timezone: fields.timezone, notes: fields.notes || null, }) } if (isLoading) { return
} if (!store) { return

No store configured

} const locations = locationsData?.data ?? [] return (

Settings

{/* Store Info */} Store Information {!editing && } {editing && (
)}
{/* Logo upload */}
{editing ? (
setFields((p) => ({ ...p, name: e.target.value }))} />
setFields((p) => ({ ...p, timezone: e.target.value }))} placeholder="America/Chicago" />
setFields((p) => ({ ...p, phone: e.target.value }))} />
setFields((p) => ({ ...p, email: e.target.value }))} />
setFields((p) => ({ ...p, street: e.target.value }))} />
setFields((p) => ({ ...p, city: e.target.value }))} />
setFields((p) => ({ ...p, state: e.target.value }))} />
setFields((p) => ({ ...p, zip: e.target.value }))} />