|
32 | 32 | const properties = schema?.properties || {}; |
33 | 33 | const requiredFields = schema?.required || []; |
34 | 34 |
|
| 35 | + // Helper function to extract data from record (handles potential nesting) |
| 36 | + function getRecordData(record: any): any { |
| 37 | + if (!record) return record; |
| 38 | +
|
| 39 | + // Check if data is nested under the entity name |
| 40 | + // e.g., { "Guitar": { "name": "Fender", "price": 1000 }, "dynamic_entity_id": "123" } |
| 41 | + if ( |
| 42 | + entityName && |
| 43 | + record[entityName] && |
| 44 | + typeof record[entityName] === "object" |
| 45 | + ) { |
| 46 | + return record[entityName]; |
| 47 | + } |
| 48 | +
|
| 49 | + // Otherwise return the record as-is (flat structure) |
| 50 | + return record; |
| 51 | + } |
| 52 | +
|
35 | 53 | let dataRecords = $state(data.dataRecords || []); |
36 | 54 | let searchQuery = $state(""); |
37 | 55 | let showCreateModal = $state(false); |
|
56 | 74 |
|
57 | 75 | function initializeFormData(record?: any) { |
58 | 76 | formData = {}; |
| 77 | + const recordData = record ? getRecordData(record) : null; |
59 | 78 | Object.keys(properties).forEach((fieldName) => { |
60 | | - formData[fieldName] = record ? record[fieldName] : ""; |
| 79 | + formData[fieldName] = recordData ? recordData[fieldName] : ""; |
61 | 80 | }); |
62 | 81 | validationErrors = {}; |
63 | 82 | } |
|
534 | 553 | class="divide-y divide-gray-200 bg-white dark:divide-gray-700 dark:bg-gray-800" |
535 | 554 | > |
536 | 555 | {#each filteredRecords as record, index} |
| 556 | + {@const recordData = getRecordData(record)} |
537 | 557 | <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50"> |
538 | 558 | {#each Object.keys(properties).slice(0, 4) as fieldName} |
539 | 559 | <td |
540 | 560 | class="max-w-xs truncate px-6 py-4 text-sm text-gray-900 dark:text-gray-100" |
541 | 561 | > |
542 | | - {record[fieldName] !== undefined && |
543 | | - record[fieldName] !== null |
544 | | - ? String(record[fieldName]) |
| 562 | + {recordData[fieldName] !== undefined && |
| 563 | + recordData[fieldName] !== null |
| 564 | + ? String(recordData[fieldName]) |
545 | 565 | : "-"} |
546 | 566 | </td> |
547 | 567 | {/each} |
|
929 | 949 | <div class="p-6"> |
930 | 950 | <dl class="space-y-4"> |
931 | 951 | {#each Object.entries(properties) as [fieldName, fieldDef]} |
| 952 | + {@const recordData = getRecordData(selectedRecord)} |
932 | 953 | <div> |
933 | 954 | <dt class="text-sm font-medium text-gray-500 dark:text-gray-400"> |
934 | 955 | {fieldName} |
|
937 | 958 | {/if} |
938 | 959 | </dt> |
939 | 960 | <dd class="mt-1 text-sm text-gray-900 dark:text-gray-100"> |
940 | | - {selectedRecord[fieldName] !== undefined && |
941 | | - selectedRecord[fieldName] !== null |
942 | | - ? String(selectedRecord[fieldName]) |
| 961 | + {recordData[fieldName] !== undefined && |
| 962 | + recordData[fieldName] !== null |
| 963 | + ? String(recordData[fieldName]) |
943 | 964 | : "-"} |
944 | 965 | </dd> |
945 | 966 | </div> |
|
0 commit comments