Files
baserow/web-frontend/modules/builder/components/PageEditorContent.vue
Peter Evans 82f9a7f9d1 feat: add an HTTP request data source to the application builder (#6553)
* feat: add an HTTP request data source to the application builder
2026-11-12 09:36:00 +00:00

193 lines
5.5 KiB
Vue

<template>
<div class="page-editor">
<PageHeader :loading="pageLoading" />
<div class="layout__col-2-2 page-editor__content">
<div :style="{ width: `calc(100% - ${panelWidth}px)` }">
<div
v-if="pageLoading"
class="skeleton page-preview__wrapper"
aria-hidden="true"
>
<div class="page-preview" :style="{ 'max-width': previewMaxWidth }">
<div class="page-preview__skeleton">
<SkeletonBlock width="55%" height="28px"></SkeletonBlock>
<SkeletonBlock width="85%"></SkeletonBlock>
<SkeletonBlock width="70%"></SkeletonBlock>
<SkeletonBlock height="180px"></SkeletonBlock>
<SkeletonBlock width="40%"></SkeletonBlock>
</div>
</div>
</div>
<PagePreview v-else />
</div>
<div
class="page-editor__side-panel"
:style="{ width: `${panelWidth}px` }"
>
<PageSidePanels />
</div>
</div>
</div>
</template>
<script setup>
import { ref, computed, watch, provide } from 'vue'
import PageHeader from '@baserow/modules/builder/components/page/header/PageHeader'
import PagePreview from '@baserow/modules/builder/components/page/PagePreview'
import PageSidePanels from '@baserow/modules/builder/components/page/PageSidePanels'
import { DataProviderType } from '@baserow/modules/core/dataProviderTypes'
import ApplicationBuilderFormulaInput from '@baserow/modules/builder/components/ApplicationBuilderFormulaInput'
import { DATA_SCOPES } from '@baserow/modules/builder/constants'
import _ from 'lodash'
const props = defineProps({
workspace: {
type: Object,
required: true,
},
builder: {
type: Object,
required: true,
},
page: {
type: Object,
required: true,
},
loading: {
type: Boolean,
required: false,
default: false,
},
})
const mode = 'editing'
const { $store, $registry } = useNuxtApp()
const panelWidth = ref(360)
// Only wait for the initial data source fetch. `dataSource/getLoading` is also
// true during every create, update, move, etc. and showing the skeleton then
// would unmount the header (closing the data source modal) and the preview.
const pageLoading = computed(
() => props.loading || !$store.getters['dataSource/getFetched'](props.page)
)
// The preview of the skeleton is the same width as the device that's selected,
// so that it doesn't resize when the elements arrive.
const previewMaxWidth = computed(() => {
const deviceTypeSelected = $store.getters['page/getDeviceTypeSelected']
if (!deviceTypeSelected) {
return 'unset'
}
const maxWidth = $registry
.get('device', deviceTypeSelected)
.getMaxWidth(props.builder)
return maxWidth ? `${maxWidth}px` : 'unset'
})
const applicationContext = computed(() => ({
workspace: props.workspace,
builder: props.builder,
mode,
dataScope: DATA_SCOPES.EXCLUDE_TARGET_SUBTREE,
}))
const sharedPage = computed(() =>
$store.getters['page/getSharedPage'](props.builder)
)
const dataSources = computed(() => {
return $store.getters['dataSource/getPageDataSources'](props.page)
})
const sharedDataSources = computed(() => {
return $store.getters['dataSource/getPageDataSources'](sharedPage.value)
})
const dispatchContext = computed(() => {
return DataProviderType.getAllDataSourceDispatchContext(
$registry.getAll('builderDataProvider'),
{ ...applicationContext.value, page: props.page }
)
})
const applicationDispatchContext = computed(() => {
return DataProviderType.getAllDataSourceDispatchContext(
$registry.getAll('builderDataProvider'),
{ builder: props.builder, mode }
)
})
provide('workspace', props.workspace)
provide('builder', props.builder)
provide('currentPage', props.page)
provide('mode', mode)
provide('formulaComponent', ApplicationBuilderFormulaInput)
provide('applicationContext', applicationContext)
/**
* What the content of the data sources depends on. A test of a data source only
* changes its sample data and the schema built from it, which leave the content
* as it is, so they don't fetch it again.
*/
const withoutTestResults = (dataSourcesToWatch) =>
dataSourcesToWatch.map(({ sample_data, schema, ...rest }) =>
_.cloneDeep(rest)
)
// Watchers
watch(
() => withoutTestResults(dataSources.value),
(newDataSources, oldDataSources) => {
if (!_.isEqual(newDataSources, oldDataSources)) {
$store.dispatch('dataSourceContent/debouncedFetchPageDataSourceContent', {
page: props.page,
data: dispatchContext.value,
mode,
})
}
},
{ deep: true }
)
watch(
() => withoutTestResults(sharedDataSources.value),
(newDataSources, oldDataSources) => {
if (!_.isEqual(newDataSources, oldDataSources)) {
$store.dispatch('dataSourceContent/debouncedFetchPageDataSourceContent', {
page: sharedPage.value,
data: dispatchContext.value,
})
}
},
{ deep: true }
)
watch(
dispatchContext,
(newDispatchContext, oldDispatchContext) => {
if (!_.isEqual(newDispatchContext, oldDispatchContext)) {
$store.dispatch('dataSourceContent/debouncedFetchPageDataSourceContent', {
page: props.page,
data: newDispatchContext,
mode,
})
}
},
{ deep: true }
)
watch(
applicationDispatchContext,
(newDispatchContext, oldDispatchContext) => {
if (!_.isEqual(newDispatchContext, oldDispatchContext)) {
$store.dispatch('dataSourceContent/debouncedFetchPageDataSourceContent', {
page: sharedPage.value,
data: newDispatchContext,
})
}
},
{ deep: true }
)
</script>