Add Contact standard object, related lists, meilisearch, pagination, search, AI assistant

This commit is contained in:
Francisco Gaona
2026-01-16 18:01:26 +01:00
parent 51c82d3d95
commit 20fc90a3fb
62 changed files with 6613 additions and 278 deletions

View File

@@ -1,11 +1,19 @@
<script setup lang="ts">
import { ref, computed, onMounted, watch, nextTick } from 'vue'
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useApi } from '@/composables/useApi'
import { useFields, useViewState } from '@/composables/useFieldViews'
import ListView from '@/components/views/ListView.vue'
import DetailView from '@/components/views/DetailViewEnhanced.vue'
import EditView from '@/components/views/EditViewEnhanced.vue'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
const route = useRoute()
const router = useRouter()
@@ -38,6 +46,7 @@ const error = ref<string | null>(null)
// Use view state composable
const {
records,
totalCount,
currentRecord,
loading: dataLoading,
saving,
@@ -48,6 +57,27 @@ const {
handleSave,
} = useViewState(`/runtime/objects/${objectApiName.value}/records`)
const handleAiRecordCreated = (event: Event) => {
const detail = (event as CustomEvent).detail || {}
if (
detail?.objectApiName &&
detail.objectApiName.toLowerCase() !== objectApiName.value.toLowerCase()
) {
return
}
if (view.value === 'list') {
initializeListRecords()
}
}
onMounted(() => {
window.addEventListener('ai-record-created', handleAiRecordCreated)
})
onBeforeUnmount(() => {
window.removeEventListener('ai-record-created', handleAiRecordCreated)
})
// Compute breadcrumbs based on the current route and object data
const updateBreadcrumbs = () => {
if (!objectDefinition.value) {
@@ -121,6 +151,20 @@ const editConfig = computed(() => {
return buildEditViewConfig(objectDefinition.value)
})
const listPageSize = computed(() => listConfig.value?.pageSize ?? 25)
const maxFrontendRecords = computed(() => listConfig.value?.maxFrontendRecords ?? 500)
const searchQuery = ref('')
const searchSummary = ref('')
const searchLoading = ref(false)
const deleteDialogOpen = ref(false)
const deleteSubmitting = ref(false)
const pendingDeleteRows = ref<any[]>([])
const deleteSummary = ref<{ deletedIds: string[]; deniedIds: string[] } | null>(null)
const isSearchActive = computed(() => searchQuery.value.trim().length > 0)
const pendingDeleteCount = computed(() => pendingDeleteRows.value.length)
const deniedDeleteCount = computed(() => deleteSummary.value?.deniedIds.length ?? 0)
// Fetch object definition
const fetchObjectDefinition = async () => {
try {
@@ -155,17 +199,51 @@ const handleBack = () => {
router.push(`/${objectApiName.value.toLowerCase()}/`)
}
const handleNavigate = (relatedObjectApiName: string, relatedRecordId: string) => {
router.push(`/${relatedObjectApiName.toLowerCase()}/${relatedRecordId}/detail`)
}
const handleCreateRelated = (relatedObjectApiName: string, _parentId: string) => {
router.push(`/${relatedObjectApiName.toLowerCase()}/new`)
}
const handleDelete = async (rows: any[]) => {
if (confirm(`Delete ${rows.length} record(s)? This action cannot be undone.`)) {
try {
const ids = rows.map(r => r.id)
await deleteRecords(ids)
pendingDeleteRows.value = rows
deleteSummary.value = null
deleteDialogOpen.value = true
}
const resetDeleteDialog = () => {
deleteDialogOpen.value = false
deleteSubmitting.value = false
pendingDeleteRows.value = []
deleteSummary.value = null
}
const confirmDelete = async () => {
if (pendingDeleteRows.value.length === 0) {
resetDeleteDialog()
return
}
deleteSubmitting.value = true
try {
const ids = pendingDeleteRows.value.map(r => r.id)
const result = await deleteRecords(ids)
const deletedIds = result?.deletedIds ?? []
const deniedIds = result?.deniedIds ?? []
deleteSummary.value = { deletedIds, deniedIds }
if (deniedIds.length === 0) {
resetDeleteDialog()
if (view.value !== 'list') {
await router.push(`/${objectApiName.value.toLowerCase()}/`)
}
} catch (e: any) {
error.value = e.message || 'Failed to delete records'
}
} catch (e: any) {
error.value = e.message || 'Failed to delete records'
} finally {
deleteSubmitting.value = false
}
}
@@ -191,6 +269,88 @@ const handleCancel = () => {
}
}
const loadListRecords = async (
page = 1,
options?: { append?: boolean; pageSize?: number }
) => {
const pageSize = options?.pageSize ?? listPageSize.value
const result = await fetchRecords({ page, pageSize }, { append: options?.append })
const resolvedTotal = result?.totalCount ?? totalCount.value ?? records.value.length
totalCount.value = resolvedTotal
return result
}
const searchListRecords = async (
page = 1,
options?: { append?: boolean; pageSize?: number }
) => {
if (!isSearchActive.value) {
return initializeListRecords()
}
searchLoading.value = true
try {
const pageSize = options?.pageSize ?? listPageSize.value
const response = await api.post('/ai/search', {
objectApiName: objectApiName.value,
query: searchQuery.value.trim(),
page,
pageSize,
})
const data = response?.data ?? []
const total = response?.totalCount ?? data.length
records.value = options?.append ? [...records.value, ...data] : data
totalCount.value = total
searchSummary.value = response?.explanation || ''
return response
} catch (e: any) {
error.value = e.message || 'Failed to search records'
return null
} finally {
searchLoading.value = false
}
}
const initializeListRecords = async () => {
const firstResult = await loadListRecords(1)
const resolvedTotal = firstResult?.totalCount ?? totalCount.value ?? records.value.length
const shouldPrefetchAll =
resolvedTotal <= maxFrontendRecords.value && records.value.length < resolvedTotal
if (shouldPrefetchAll) {
await loadListRecords(1, { append: false, pageSize: maxFrontendRecords.value })
}
}
const handlePageChange = async (page: number, pageSize: number) => {
if (isSearchActive.value) {
await searchListRecords(page, { append: page > 1, pageSize })
return
}
const loadedPages = Math.ceil(records.value.length / pageSize)
if (page > loadedPages && totalCount.value > records.value.length) {
await loadListRecords(page, { append: true, pageSize })
}
}
const handleLoadMore = async (page: number, pageSize: number) => {
if (isSearchActive.value) {
await searchListRecords(page, { append: true, pageSize })
return
}
await loadListRecords(page, { append: true, pageSize })
}
const handleSearch = async (query: string) => {
const trimmed = query.trim()
searchQuery.value = trimmed
if (!trimmed) {
searchSummary.value = ''
await initializeListRecords()
return
}
await searchListRecords(1, { append: false, pageSize: listPageSize.value })
}
// Watch for route changes
watch(() => route.params, async (newParams, oldParams) => {
// Reset current record when navigating to 'new'
@@ -205,7 +365,7 @@ watch(() => route.params, async (newParams, oldParams) => {
// Fetch records if navigating back to list
if (!newParams.recordId && !newParams.view) {
await fetchRecords()
await initializeListRecords()
}
}, { deep: true })
@@ -214,7 +374,7 @@ onMounted(async () => {
await fetchObjectDefinition()
if (view.value === 'list') {
await fetchRecords()
await initializeListRecords()
} else if (recordId.value && recordId.value !== 'new') {
await fetchRecord(recordId.value)
}
@@ -259,13 +419,18 @@ onMounted(async () => {
v-else-if="view === 'list' && listConfig"
:config="listConfig"
:data="records"
:loading="dataLoading"
:loading="dataLoading || searchLoading"
:total-count="totalCount"
:search-summary="searchSummary"
:base-url="`/runtime/objects`"
selectable
@row-click="handleRowClick"
@create="handleCreate"
@edit="handleEdit"
@delete="handleDelete"
@search="handleSearch"
@page-change="handlePageChange"
@load-more="handleLoadMore"
/>
<!-- Detail View -->
@@ -279,6 +444,8 @@ onMounted(async () => {
@edit="handleEdit"
@delete="() => handleDelete([currentRecord])"
@back="handleBack"
@navigate="handleNavigate"
@create-related="handleCreateRelated"
/>
<!-- Edit View -->
@@ -295,6 +462,46 @@ onMounted(async () => {
@back="handleBack"
/>
</div>
<Dialog v-model:open="deleteDialogOpen">
<DialogContent class="sm:max-w-[520px]">
<DialogHeader>
<DialogTitle>Delete records</DialogTitle>
<DialogDescription>
This action cannot be undone.
</DialogDescription>
</DialogHeader>
<div class="space-y-2 text-sm">
<p>
You are about to delete {{ pendingDeleteCount }} record<span v-if="pendingDeleteCount !== 1">s</span>.
</p>
<p v-if="deleteSummary" class="text-muted-foreground">
Deleted {{ deleteSummary.deletedIds.length }} record<span v-if="deleteSummary.deletedIds.length !== 1">s</span>.
</p>
<p v-if="deniedDeleteCount > 0" class="text-destructive">
{{ deniedDeleteCount }} record<span v-if="deniedDeleteCount !== 1">s</span> could not be deleted due to missing permissions.
</p>
<p v-if="!deleteSummary" class="text-muted-foreground">
Records you do not have permission to delete will be skipped.
</p>
</div>
<DialogFooter>
<Button variant="outline" @click="resetDeleteDialog" :disabled="deleteSubmitting">
{{ deleteSummary ? 'Close' : 'Cancel' }}
</Button>
<Button
v-if="!deleteSummary"
variant="destructive"
@click="confirmDelete"
:disabled="deleteSubmitting"
>
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</NuxtLayout>
</template>

View File

@@ -71,7 +71,12 @@ const fetchPage = async () => {
if (page.value.objectApiName) {
loadingRecords.value = true
records.value = await api.get(`/runtime/objects/${page.value.objectApiName}/records`)
const response = await api.get(
`/runtime/objects/${page.value.objectApiName}/records`
)
records.value = Array.isArray(response)
? response
: response?.data || response?.records || []
}
} catch (e: any) {
error.value = e.message

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, computed, onMounted, watch, nextTick } from 'vue'
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useApi } from '@/composables/useApi'
import { useFields, useViewState } from '@/composables/useFieldViews'
@@ -31,6 +31,7 @@ const error = ref<string | null>(null)
// Use view state composable
const {
records,
totalCount,
currentRecord,
loading: dataLoading,
saving,
@@ -41,6 +42,27 @@ const {
handleSave,
} = useViewState(`/runtime/objects/${objectApiName.value}/records`)
const handleAiRecordCreated = (event: Event) => {
const detail = (event as CustomEvent).detail || {}
if (
detail?.objectApiName &&
detail.objectApiName.toLowerCase() !== objectApiName.value.toLowerCase()
) {
return
}
if (view.value === 'list') {
initializeListRecords()
}
}
onMounted(() => {
window.addEventListener('ai-record-created', handleAiRecordCreated)
})
onBeforeUnmount(() => {
window.removeEventListener('ai-record-created', handleAiRecordCreated)
})
// View configs
const listConfig = computed(() => {
if (!objectDefinition.value) return null
@@ -61,6 +83,9 @@ const editConfig = computed(() => {
return buildEditViewConfig(objectDefinition.value)
})
const listPageSize = computed(() => listConfig.value?.pageSize ?? 25)
const maxFrontendRecords = computed(() => listConfig.value?.maxFrontendRecords ?? 500)
// Fetch object definition
const fetchObjectDefinition = async () => {
try {
@@ -95,6 +120,14 @@ const handleBack = () => {
router.push(`/app/objects/${objectApiName.value}/`)
}
const handleNavigate = (relatedObjectApiName: string, relatedRecordId: string) => {
router.push(`/app/objects/${relatedObjectApiName}/${relatedRecordId}/detail`)
}
const handleCreateRelated = (relatedObjectApiName: string, _parentId: string) => {
router.push(`/app/objects/${relatedObjectApiName}/new`)
}
const handleDelete = async (rows: any[]) => {
if (confirm(`Delete ${rows.length} record(s)? This action cannot be undone.`)) {
try {
@@ -131,6 +164,39 @@ const handleCancel = () => {
}
}
const loadListRecords = async (
page = 1,
options?: { append?: boolean; pageSize?: number }
) => {
const pageSize = options?.pageSize ?? listPageSize.value
const result = await fetchRecords({ page, pageSize }, { append: options?.append })
const resolvedTotal = result?.totalCount ?? totalCount.value ?? records.value.length
totalCount.value = resolvedTotal
return result
}
const initializeListRecords = async () => {
const firstResult = await loadListRecords(1)
const resolvedTotal = firstResult?.totalCount ?? totalCount.value ?? records.value.length
const shouldPrefetchAll =
resolvedTotal <= maxFrontendRecords.value && records.value.length < resolvedTotal
if (shouldPrefetchAll) {
await loadListRecords(1, { append: false, pageSize: maxFrontendRecords.value })
}
}
const handlePageChange = async (page: number, pageSize: number) => {
const loadedPages = Math.ceil(records.value.length / pageSize)
if (page > loadedPages && totalCount.value > records.value.length) {
await loadListRecords(page, { append: true, pageSize })
}
}
const handleLoadMore = async (page: number, pageSize: number) => {
await loadListRecords(page, { append: true, pageSize })
}
// Watch for route changes
watch(() => route.params, async (newParams, oldParams) => {
// Reset current record when navigating to 'new'
@@ -145,7 +211,7 @@ watch(() => route.params, async (newParams, oldParams) => {
// Fetch records if navigating back to list
if (!newParams.recordId && !newParams.view) {
await fetchRecords()
await initializeListRecords()
}
}, { deep: true })
@@ -154,7 +220,7 @@ onMounted(async () => {
await fetchObjectDefinition()
if (view.value === 'list') {
await fetchRecords()
await initializeListRecords()
} else if (recordId.value && recordId.value !== 'new') {
await fetchRecord(recordId.value)
}
@@ -196,11 +262,14 @@ onMounted(async () => {
:config="listConfig"
:data="records"
:loading="dataLoading"
:total-count="totalCount"
selectable
@row-click="handleRowClick"
@create="handleCreate"
@edit="handleEdit"
@delete="handleDelete"
@page-change="handlePageChange"
@load-more="handleLoadMore"
/>
<!-- Detail View -->
@@ -212,6 +281,8 @@ onMounted(async () => {
@edit="handleEdit"
@delete="() => handleDelete([currentRecord])"
@back="handleBack"
@navigate="handleNavigate"
@create-related="handleCreateRelated"
/>
<!-- Edit View -->

View File

@@ -133,6 +133,7 @@ onMounted(async () => {
:config="domainDetailConfig"
:data="currentRecord"
:loading="dataLoading"
base-url="/central"
@edit="handleEdit"
@delete="() => handleDelete([currentRecord])"
@back="handleBack"

View File

@@ -168,6 +168,7 @@ onMounted(async () => {
:config="tenantDetailConfig"
:data="currentRecord"
:loading="dataLoading"
base-url="/central"
@edit="handleEdit"
@delete="() => handleDelete([currentRecord])"
@back="handleBack"

View File

@@ -138,6 +138,7 @@ onMounted(async () => {
:config="centralUserDetailConfig"
:data="currentRecord"
:loading="dataLoading"
base-url="/central"
@edit="handleEdit"
@delete="() => handleDelete([currentRecord])"
@back="handleBack"

View File

@@ -24,32 +24,78 @@
<!-- Fields Tab -->
<TabsContent value="fields" class="mt-6">
<div class="space-y-2">
<div
v-for="field in object.fields"
:key="field.id"
class="p-4 border rounded-lg bg-card"
>
<div class="flex items-center justify-between">
<div>
<h3 class="font-semibold">{{ field.label }}</h3>
<p class="text-sm text-muted-foreground">
Type: {{ field.type }} | API Name: {{ field.apiName }}
</p>
</div>
<div class="flex gap-2 text-xs">
<span
v-if="field.isRequired"
class="px-2 py-1 bg-destructive/10 text-destructive rounded"
>
Required
</span>
<span
v-if="field.isUnique"
class="px-2 py-1 bg-primary/10 text-primary rounded"
>
Unique
</span>
<div class="space-y-4">
<div class="flex justify-between items-center mb-4">
<h2 class="text-xl font-semibold">Fields</h2>
<Button @click="openFieldDialog('create')">
<Plus class="w-4 h-4 mr-2" />
New Field
</Button>
</div>
<div v-if="!object.fields || object.fields.length === 0" class="text-center py-8 text-muted-foreground">
No fields defined yet. Create one to get started.
</div>
<div v-else class="space-y-2">
<div
v-for="field in object.fields"
:key="field.id"
class="p-4 border rounded-lg bg-card hover:border-primary transition-colors"
>
<div class="flex items-center justify-between">
<div class="flex-1">
<h3 class="font-semibold">{{ field.label }}</h3>
<p class="text-sm text-muted-foreground">
Type: <span class="font-medium">{{ formatFieldType(field.type) }}</span> | API Name: <span class="font-mono">{{ field.apiName }}</span>
</p>
<p v-if="field.description" class="text-sm text-muted-foreground mt-1">
{{ field.description }}
</p>
</div>
<div class="flex items-center gap-3">
<div class="flex gap-2 text-xs">
<span
v-if="field.isRequired"
class="px-2 py-1 bg-destructive/10 text-destructive rounded"
>
Required
</span>
<span
v-if="field.isUnique"
class="px-2 py-1 bg-primary/10 text-primary rounded"
>
Unique
</span>
<span
v-if="field.isSystem"
class="px-2 py-1 bg-gray-200 text-gray-700 rounded text-xs"
>
System
</span>
</div>
<div class="flex gap-2">
<Button
v-if="!field.isSystem"
variant="ghost"
size="sm"
@click="openFieldDialog('edit', field)"
title="Edit field"
>
</Button>
<Button
v-if="!field.isSystem"
variant="ghost"
size="sm"
class="text-destructive hover:text-destructive"
@click="deleteField(field)"
title="Delete field"
>
<Trash2 class="w-4 h-4" />
</Button>
</div>
</div>
</div>
</div>
</div>
@@ -132,6 +178,8 @@
<PageLayoutEditor
:fields="object.fields"
:initial-layout="(selectedLayout.layoutConfig || selectedLayout.layout_config)?.fields || []"
:related-lists="object.relatedLists || []"
:initial-related-lists="(selectedLayout.layoutConfig || selectedLayout.layout_config)?.relatedLists || []"
:layout-name="selectedLayout.name"
@save="handleSaveLayout"
/>
@@ -141,6 +189,107 @@
</div>
</div>
</main>
<!-- Field Management Dialog -->
<Teleport to="body">
<div
v-if="showFieldDialog"
class="fixed inset-0 bg-black/50 flex items-center justify-center z-[100]"
>
<div class="bg-white rounded-lg shadow-lg max-w-3xl w-full mx-4 max-h-[90vh] overflow-y-auto">
<div class="sticky top-0 bg-white border-b p-6 flex items-center justify-between">
<h2 class="text-2xl font-bold">
{{ fieldDialogMode === 'create' ? 'Create New Field' : 'Edit Field' }}
</h2>
<button
@click="closeFieldDialog"
class="text-gray-500 hover:text-gray-700 text-2xl font-bold"
>
×
</button>
</div>
<div class="p-6 space-y-6">
<!-- Field Type Selection (only for creation) -->
<div v-if="fieldDialogMode === 'create'">
<FieldTypeSelector
v-model="fieldForm.type"
/>
</div>
<!-- Common Attributes -->
<div v-if="fieldForm.type">
<h3 class="text-lg font-semibold mb-4">Basic Properties</h3>
<FieldAttributesCommon
:label="fieldForm.label"
:api-name="fieldForm.apiName"
:description="fieldForm.description"
:placeholder="fieldForm.placeholder"
:help-text="fieldForm.helpText"
:display-order="fieldForm.displayOrder"
:is-required="fieldForm.isRequired"
:is-unique="fieldForm.isUnique"
:default-value="fieldForm.defaultValue"
:is-editing="fieldDialogMode === 'edit'"
:has-data="fieldForm.hasData"
@update="updateCommonAttributes"
/>
</div>
<!-- Type-Specific Attributes -->
<div v-if="fieldForm.type">
<h3 class="text-lg font-semibold mb-4">Type-Specific Settings</h3>
<FieldAttributesType
:field-type="fieldForm.type"
:attributes="fieldForm.typeAttributes"
@update="updateTypeAttributes"
/>
</div>
<!-- Lookup Field Selection -->
<div v-if="(fieldForm.type === 'lookup' || fieldForm.type === 'belongsTo') && fieldDialogMode === 'create'">
<h3 class="text-lg font-semibold mb-4">Related Object</h3>
<div class="grid grid-cols-4 gap-4">
<label class="text-sm font-medium leading-8">Select Object</label>
<div class="col-span-3">
<select
v-model="fieldForm.referenceObject"
class="w-full px-3 py-2 border rounded-md text-sm"
>
<option value="">-- Select an object --</option>
<option
v-for="obj in availableObjects"
:key="obj.id"
:value="obj.apiName"
>
{{ obj.label }} ({{ obj.apiName }})
</option>
</select>
</div>
</div>
</div>
<!-- Error Message -->
<div v-if="fieldDialogError" class="p-3 bg-red-100 text-red-800 rounded-md text-sm">
{{ fieldDialogError }}
</div>
<!-- Action Buttons -->
<div class="flex gap-3 justify-end pt-4">
<Button variant="outline" @click="closeFieldDialog">
Cancel
</Button>
<Button
:disabled="!fieldForm.label || !fieldForm.apiName || !fieldForm.type"
@click="saveField"
>
{{ fieldDialogMode === 'create' ? 'Create Field' : 'Update Field' }}
</Button>
</div>
</div>
</div>
</div>
</Teleport>
</NuxtLayout>
</div>
</template>
@@ -151,6 +300,9 @@ import { Button } from '@/components/ui/button'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import PageLayoutEditor from '@/components/PageLayoutEditor.vue'
import ObjectAccessSettings from '@/components/ObjectAccessSettings.vue'
import FieldTypeSelector from '@/components/fields/FieldTypeSelector.vue'
import FieldAttributesCommon from '@/components/fields/FieldAttributesCommon.vue'
import FieldAttributesType from '@/components/fields/FieldAttributesType.vue'
import type { PageLayout, FieldLayoutItem } from '~/types/page-layout'
const route = useRoute()
@@ -168,6 +320,81 @@ const layouts = ref<PageLayout[]>([])
const loadingLayouts = ref(false)
const selectedLayout = ref<PageLayout | null>(null)
// Field management state
const showFieldDialog = ref(false)
const fieldDialogMode = ref<'create' | 'edit'>('create')
const fieldDialogError = ref<string | null>(null)
const availableObjects = ref<any[]>([])
const fieldForm = ref({
id: '',
label: '',
apiName: '',
type: '',
description: '',
placeholder: '',
helpText: '',
displayOrder: 0,
isRequired: false,
isUnique: false,
defaultValue: '',
referenceObject: '',
typeAttributes: {},
hasData: false,
})
// Helper to format field type names
const formatFieldType = (type: string): string => {
const typeNames: Record<string, string> = {
'TEXT': 'Text',
'LONG_TEXT': 'Textarea',
'EMAIL': 'Email',
'PHONE': 'Phone',
'NUMBER': 'Number',
'CURRENCY': 'Currency',
'PERCENT': 'Percent',
'PICKLIST': 'Picklist',
'MULTI_PICKLIST': 'Multi-select',
'BOOLEAN': 'Checkbox',
'DATE': 'Date',
'DATE_TIME': 'DateTime',
'TIME': 'Time',
'URL': 'URL',
'LOOKUP': 'Lookup',
'FILE': 'File',
'IMAGE': 'Image',
'JSON': 'JSON',
}
return typeNames[type] || type
}
const convertFrontendToBackendType = (frontendType: string): string => {
const typeMap: Record<string, string> = {
'text': 'TEXT',
'textarea': 'LONG_TEXT',
'password': 'TEXT',
'email': 'EMAIL',
'number': 'NUMBER',
'currency': 'CURRENCY',
'percent': 'PERCENT',
'select': 'PICKLIST',
'multiSelect': 'MULTI_PICKLIST',
'boolean': 'BOOLEAN',
'date': 'DATE',
'datetime': 'DATE_TIME',
'time': 'TIME',
'url': 'URL',
'color': 'TEXT',
'json': 'JSON',
'lookup': 'LOOKUP',
'belongsTo': 'LOOKUP',
'markdown': 'LONG_TEXT',
'code': 'LONG_TEXT',
'file': 'FILE',
'image': 'IMAGE',
}
return typeMap[frontendType] || 'TEXT'
}
const fetchObject = async () => {
try {
loading.value = true
@@ -180,6 +407,14 @@ const fetchObject = async () => {
}
}
const fetchAvailableObjects = async () => {
try {
availableObjects.value = await api.get('/setup/objects')
} catch (e: any) {
console.error('Error fetching available objects:', e)
}
}
const fetchLayouts = async () => {
if (!object.value) return
@@ -194,6 +429,253 @@ const fetchLayouts = async () => {
}
}
const openFieldDialog = async (mode: 'create' | 'edit', field?: any) => {
fieldDialogMode.value = mode
fieldDialogError.value = null
if (mode === 'create') {
await fetchAvailableObjects()
fieldForm.value = {
id: '',
label: '',
apiName: '',
type: '',
description: '',
placeholder: '',
helpText: '',
displayOrder: (object.value?.fields?.length || 0) + 1,
isRequired: false,
isUnique: false,
defaultValue: '',
referenceObject: '',
typeAttributes: {},
hasData: false,
}
} else if (field) {
// Load field data for editing
const uiMetadata = field.ui_metadata ? JSON.parse(field.ui_metadata) : {}
fieldForm.value = {
id: field.id,
label: field.label,
apiName: field.apiName,
type: convertBackendToFrontendType(field.type),
description: field.description || '',
placeholder: uiMetadata.placeholder || '',
helpText: uiMetadata.helpText || '',
displayOrder: field.displayOrder || 0,
isRequired: field.isRequired || false,
isUnique: field.isUnique || false,
defaultValue: field.defaultValue || '',
referenceObject: field.referenceObject || '',
typeAttributes: extractTypeAttributes(field, uiMetadata),
hasData: false, // Would need to fetch this from backend
}
}
showFieldDialog.value = true
}
const convertBackendToFrontendType = (backendType: string): string => {
const typeMap: Record<string, string> = {
'TEXT': 'text',
'LONG_TEXT': 'textarea',
'EMAIL': 'email',
'PHONE': 'phone',
'NUMBER': 'number',
'CURRENCY': 'currency',
'PERCENT': 'percent',
'PICKLIST': 'select',
'MULTI_PICKLIST': 'multiSelect',
'BOOLEAN': 'boolean',
'DATE': 'date',
'DATE_TIME': 'datetime',
'TIME': 'time',
'URL': 'url',
'LOOKUP': 'lookup',
'FILE': 'file',
'IMAGE': 'image',
'JSON': 'json',
}
return typeMap[backendType] || 'text'
}
const extractTypeAttributes = (field: any, uiMetadata: any): any => {
const attrs: any = {}
if (field.type === 'PICKLIST' || field.type === 'MULTI_PICKLIST') {
attrs.options = uiMetadata.options || []
}
if (field.type === 'NUMBER' || field.type === 'CURRENCY') {
attrs.scale = field.scale || 0
attrs.min = uiMetadata.min
attrs.max = uiMetadata.max
if (field.type === 'CURRENCY') {
attrs.prefix = uiMetadata.prefix || '$'
}
}
if (field.type === 'TEXT' && field.length) {
attrs.maxLength = field.length
}
if (field.type === 'LONG_TEXT' && uiMetadata.rows) {
attrs.rows = uiMetadata.rows
}
if (field.type === 'LOOKUP') {
attrs.relationObject = field.referenceObject
attrs.relationDisplayField = uiMetadata.relationDisplayField || 'name'
}
return attrs
}
const closeFieldDialog = () => {
showFieldDialog.value = false
fieldDialogError.value = null
}
const updateCommonAttributes = (data: any) => {
Object.assign(fieldForm.value, data)
}
const updateTypeAttributes = (data: any) => {
fieldForm.value.typeAttributes = data
}
const saveField = async () => {
fieldDialogError.value = null
try {
// Validate
if (!fieldForm.value.label || !fieldForm.value.apiName || !fieldForm.value.type) {
fieldDialogError.value = 'Please fill in all required fields'
return
}
const apiName = route.params.apiName as string
// Prepare payload
const payload: any = {
label: fieldForm.value.label,
apiName: fieldForm.value.apiName,
type: fieldForm.value.type, // Use frontend type, backend will convert
description: fieldForm.value.description,
isRequired: fieldForm.value.isRequired,
isUnique: fieldForm.value.isUnique,
defaultValue: fieldForm.value.defaultValue,
}
// Extract type-specific database fields
const typeAttrs = fieldForm.value.typeAttributes || {}
// For text fields
if (fieldForm.value.type === 'text' && typeAttrs.maxLength) {
payload.length = typeAttrs.maxLength
}
// For number and currency fields
if ((fieldForm.value.type === 'number' || fieldForm.value.type === 'currency') && typeAttrs.scale !== undefined) {
payload.scale = typeAttrs.scale
if (typeAttrs.scale > 0) {
payload.precision = 10 // Default precision for decimals
}
}
// Merge UI metadata
const uiMetadata: any = {
placeholder: fieldForm.value.placeholder,
helpText: fieldForm.value.helpText,
}
// Add type-specific attributes to UI metadata
if (fieldForm.value.typeAttributes) {
Object.assign(uiMetadata, fieldForm.value.typeAttributes)
}
payload.uiMetadata = uiMetadata
if (fieldForm.value.referenceObject) {
payload.relationObject = fieldForm.value.referenceObject
payload.relationDisplayField = fieldForm.value.typeAttributes.relationDisplayField || 'name'
}
let result
if (fieldDialogMode.value === 'create') {
result = await api.post(`/setup/objects/${apiName}/fields`, payload)
} else {
// For updates, only send fields that changed
const updatePayload: any = {}
if (fieldForm.value.label) updatePayload.label = fieldForm.value.label
if (fieldForm.value.description) updatePayload.description = fieldForm.value.description
if (fieldForm.value.placeholder) updatePayload.placeholder = fieldForm.value.placeholder
if (fieldForm.value.helpText) updatePayload.helpText = fieldForm.value.helpText
updatePayload.isRequired = fieldForm.value.isRequired
updatePayload.isUnique = fieldForm.value.isUnique
updatePayload.displayOrder = fieldForm.value.displayOrder
if (Object.keys(uiMetadata).length > 0) {
updatePayload.uiMetadata = uiMetadata
}
result = await api.put(
`/setup/objects/${apiName}/fields/${fieldForm.value.apiName}`,
updatePayload,
)
}
// Update the object with new field
if (fieldDialogMode.value === 'create') {
object.value.fields.push(result)
} else {
const index = object.value.fields.findIndex((f: any) => f.id === fieldForm.value.id)
if (index !== -1) {
object.value.fields[index] = result
}
}
toast.success(
fieldDialogMode.value === 'create'
? 'Field created successfully'
: 'Field updated successfully',
)
closeFieldDialog()
} catch (e: any) {
fieldDialogError.value = e.message || 'An error occurred while saving the field'
console.error('Error saving field:', e)
}
}
const deleteField = async (field: any) => {
if (!confirm(`Are you sure you want to delete the field "${field.label}"? This action cannot be undone.`)) {
return
}
try {
const apiName = route.params.apiName as string
await api.delete(`/setup/objects/${apiName}/fields/${field.apiName}`)
// Remove from the list
object.value.fields = object.value.fields.filter((f: any) => f.id !== field.id)
// Also remove from page layouts
for (const layout of layouts.value) {
const layoutConfig = layout.layoutConfig || layout.layout_config || { fields: [] }
if (layoutConfig.fields) {
layoutConfig.fields = layoutConfig.fields.filter(
(f: any) => f.fieldId !== field.id,
)
}
}
toast.success('Field deleted successfully')
} catch (e: any) {
toast.error(`Failed to delete field: ${e.message}`)
console.error('Error deleting field:', e)
}
}
const handleCreateLayout = async () => {
const name = prompt('Enter a name for the new layout:')
if (!name) return
@@ -203,7 +685,7 @@ const handleCreateLayout = async () => {
name,
objectId: object.value.id,
isDefault: layouts.value.length === 0,
layoutConfig: { fields: [] },
layoutConfig: { fields: [], relatedLists: [] },
})
layouts.value.push(newLayout)
@@ -219,12 +701,12 @@ const handleSelectLayout = (layout: PageLayout) => {
selectedLayout.value = layout
}
const handleSaveLayout = async (fields: FieldLayoutItem[]) => {
const handleSaveLayout = async (layoutConfig: { fields: FieldLayoutItem[]; relatedLists: string[] }) => {
if (!selectedLayout.value) return
try {
const updated = await updatePageLayout(selectedLayout.value.id, {
layoutConfig: { fields },
layoutConfig,
})
// Update the layout in the list
@@ -254,17 +736,19 @@ const handleDeleteLayout = async (layoutId: string) => {
}
}
const handleAccessUpdate = (orgWideDefault: string) => {
if (object.value) {
object.value.orgWideDefault = orgWideDefault
}
}
// Watch for tab changes to load layouts
watch(activeTab, (newTab) => {
if (newTab === 'layouts' && layouts.value.length === 0 && !loadingLayouts.value) {
fetchLayouts()
}
})
const handleAccessUpdate = (orgWideDefault: string) => {
if (object.value) {
object.value.orgWideDefault = orgWideDefault
}
}
onMounted(async () => {
await fetchObject()
// If we start on layouts tab, load them