|
| 1 | +import { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { ObjectGridTable } from '@objectos/ui'; |
| 3 | +import type { ObjectConfig } from '@objectql/types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Enhanced ObjectListView using ObjectGridTable |
| 7 | + * This is an improved version that uses metadata-driven AG Grid table |
| 8 | + */ |
| 9 | + |
| 10 | +interface EnhancedObjectListViewProps { |
| 11 | + objectName: string; |
| 12 | + user: any; |
| 13 | +} |
| 14 | + |
| 15 | +export function EnhancedObjectListView({ objectName, user }: EnhancedObjectListViewProps) { |
| 16 | + const [data, setData] = useState<any[]>([]); |
| 17 | + const [objectConfig, setObjectConfig] = useState<ObjectConfig | null>(null); |
| 18 | + const [loading, setLoading] = useState(false); |
| 19 | + const [error, setError] = useState<string | null>(null); |
| 20 | + |
| 21 | + const getHeaders = useCallback(() => { |
| 22 | + const headers: Record<string, string> = { 'Content-Type': 'application/json' }; |
| 23 | + // Use the actual user ID from props instead of hard-coded value |
| 24 | + if (user?.id || user?._id) { |
| 25 | + headers['x-user-id'] = user.id || user._id; |
| 26 | + } |
| 27 | + return headers; |
| 28 | + }, [user]); |
| 29 | + |
| 30 | + // Fetch object metadata |
| 31 | + useEffect(() => { |
| 32 | + if (!objectName) return; |
| 33 | + |
| 34 | + fetch(`/api/metadata/object/${objectName}`, { headers: getHeaders() }) |
| 35 | + .then(async res => { |
| 36 | + if (!res.ok) { |
| 37 | + throw new Error(await res.text() || res.statusText); |
| 38 | + } |
| 39 | + return res.json(); |
| 40 | + }) |
| 41 | + .then(config => { |
| 42 | + setObjectConfig(config); |
| 43 | + }) |
| 44 | + .catch(err => { |
| 45 | + console.error('Failed to load object metadata:', err); |
| 46 | + setError(err.message); |
| 47 | + }); |
| 48 | + }, [objectName, getHeaders]); |
| 49 | + |
| 50 | + // Fetch data |
| 51 | + useEffect(() => { |
| 52 | + if (!objectName) return; |
| 53 | + |
| 54 | + setLoading(true); |
| 55 | + setError(null); |
| 56 | + |
| 57 | + fetch(`/api/data/${objectName}`, { headers: getHeaders() }) |
| 58 | + .then(async res => { |
| 59 | + if (!res.ok) { |
| 60 | + const contentType = res.headers.get("content-type"); |
| 61 | + if (contentType && contentType.indexOf("application/json") !== -1) { |
| 62 | + const json = await res.json(); |
| 63 | + throw new Error(json.error || "Failed to load data"); |
| 64 | + } |
| 65 | + throw new Error(await res.text() || res.statusText); |
| 66 | + } |
| 67 | + return res.json(); |
| 68 | + }) |
| 69 | + .then(result => { |
| 70 | + const items = Array.isArray(result) ? result : (result.list || []); |
| 71 | + setData(items); |
| 72 | + }) |
| 73 | + .catch(err => { |
| 74 | + console.error(err); |
| 75 | + setError(err.message); |
| 76 | + setData([]); |
| 77 | + }) |
| 78 | + .finally(() => setLoading(false)); |
| 79 | + }, [objectName, getHeaders]); |
| 80 | + |
| 81 | + const handleSelectionChanged = (selectedRows: any[]) => { |
| 82 | + console.log('Selected rows:', selectedRows); |
| 83 | + // You can add more selection handling logic here |
| 84 | + }; |
| 85 | + |
| 86 | + if (error) { |
| 87 | + return ( |
| 88 | + <div className="p-6"> |
| 89 | + <div className="mb-4 p-4 text-destructive bg-destructive/10 rounded-lg border border-destructive/20"> |
| 90 | + {error} |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + if (!objectConfig) { |
| 97 | + return ( |
| 98 | + <div className="flex items-center justify-center h-full"> |
| 99 | + <div className="text-muted-foreground">Loading object metadata...</div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return ( |
| 105 | + <div className="flex flex-col h-full bg-background p-6"> |
| 106 | + <div className="mb-4"> |
| 107 | + <h2 className="text-2xl font-bold">{objectConfig.label || objectName}</h2> |
| 108 | + {objectConfig.description && ( |
| 109 | + <p className="text-muted-foreground mt-1">{objectConfig.description}</p> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + |
| 113 | + {loading ? ( |
| 114 | + <div className="flex items-center justify-center flex-1"> |
| 115 | + <div className="text-muted-foreground">Loading data...</div> |
| 116 | + </div> |
| 117 | + ) : ( |
| 118 | + <ObjectGridTable |
| 119 | + objectConfig={objectConfig} |
| 120 | + data={data} |
| 121 | + height="calc(100vh - 200px)" |
| 122 | + pagination={true} |
| 123 | + pageSize={20} |
| 124 | + rowSelection="multiple" |
| 125 | + onSelectionChanged={handleSelectionChanged} |
| 126 | + /> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + ); |
| 130 | +} |
0 commit comments