Update data section routing to properly handle datasource entities/table sync and handle having no data

This commit is contained in:
Andrew Kingston 2023-06-26 16:34:22 +01:00
parent 6d38bdcd64
commit a3204ad17a
5 changed files with 71 additions and 28 deletions

View File

@ -2,8 +2,17 @@
import { Button, Layout } from "@budibase/bbui" import { Button, Layout } from "@budibase/bbui"
import DatasourceNavigator from "components/backend/DatasourceNavigator/DatasourceNavigator.svelte" import DatasourceNavigator from "components/backend/DatasourceNavigator/DatasourceNavigator.svelte"
import Panel from "components/design/Panel.svelte" import Panel from "components/design/Panel.svelte"
import { isActive, goto } from "@roxi/routify" import { isActive, goto, redirect } from "@roxi/routify"
import BetaButton from "./_components/BetaButton.svelte" import BetaButton from "./_components/BetaButton.svelte"
import { datasources } from "stores/backend"
$: {
// If we ever don't have any data other than the users table, prompt the
// user to add some
if (!$datasources.hasData) {
$redirect("./new")
}
}
</script> </script>
<!-- routify:options index=1 --> <!-- routify:options index=1 -->

View File

@ -4,11 +4,13 @@
import { onMount } from "svelte" import { onMount } from "svelte"
onMount(async () => { onMount(async () => {
const { list, selected } = $datasources const { list, selected, hasData } = $datasources
if (selected) { if (selected) {
$redirect(`./${selected?._id}`) $redirect(`./${selected?._id}`)
} else { } else if (hasData && list?.length) {
$redirect(`./${list[0]._id}`) $redirect(`./${list[0]._id}`)
} else {
$redirect("../new")
} }
}) })
</script> </script>

View File

@ -1,17 +1,13 @@
<script> <script>
import { redirect } from "@roxi/routify" import { redirect } from "@roxi/routify"
import { onMount } from "svelte" import { TableNames } from "constants"
import { datasources } from "stores/backend" import { datasources } from "stores/backend"
$: hasData = $: {
$datasources.list.find(x => (x._id = "bb_internal"))?.entities?.length > if ($datasources.hasData) {
1 || $datasources.list.length > 1 $redirect(`./table/${TableNames.USERS}`)
onMount(() => {
if (!hasData) {
$redirect("./new")
} else { } else {
$redirect("./table") $redirect("./new")
} }
}) }
</script> </script>

View File

@ -1,14 +1,16 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import { tables } from "stores/backend" import { datasources, tables } from "stores/backend"
import { redirect } from "@roxi/routify" import { redirect } from "@roxi/routify"
import { TableNames } from "constants"
onMount(async () => { onMount(() => {
const { list, selected } = $tables if ($tables.selected) {
if (selected) { $redirect(`./${$tables.selected._id}`)
$redirect(`./${selected?._id}`) } else if ($datasources.hasData) {
} else if (list?.length) { $redirect(`./${TableNames.USERS}`)
$redirect(`./${list[0]._id}`) } else {
$redirect("../new")
} }
}) })
</script> </script>

View File

@ -1,9 +1,14 @@
import { writable, derived, get } from "svelte/store" import { writable, derived, get } from "svelte/store"
import { IntegrationTypes, DEFAULT_BB_DATASOURCE_ID } from "constants/backend" import {
IntegrationTypes,
DEFAULT_BB_DATASOURCE_ID,
BUDIBASE_INTERNAL_DB_ID,
} from "constants/backend"
import { queries, tables } from "./" import { queries, tables } from "./"
import { API } from "api" import { API } from "api"
import { DatasourceFeature } from "@budibase/types" import { DatasourceFeature } from "@budibase/types"
import { notifications } from "@budibase/bbui" import { notifications } from "@budibase/bbui"
import { TableNames } from "constants"
export class ImportTableError extends Error { export class ImportTableError extends Error {
constructor(message) { constructor(message) {
@ -24,13 +29,42 @@ export function createDatasourcesStore() {
schemaError: null, schemaError: null,
}) })
const derivedStore = derived(store, $store => ({ const derivedStore = derived([store, tables], ([$store, $tables]) => {
...$store, // Set the internal datasource entities from the table list, which we're
selected: $store.list?.find(ds => ds._id === $store.selectedDatasourceId), // able to keep updated unlike the egress generated definition of the
hasDefaultData: $store.list.some( // internal datasource
datasource => datasource._id === DEFAULT_BB_DATASOURCE_ID let internalDS = $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID)
), let otherDS = $store.list?.find(ds => ds._id !== BUDIBASE_INTERNAL_DB_ID)
})) if (internalDS) {
internalDS = {
...internalDS,
entities: $tables.list?.filter(table => {
return (
table.sourceId === BUDIBASE_INTERNAL_DB_ID &&
table._id !== TableNames.USERS
)
}),
}
}
// Build up enriched DS list
// Only add the internal DS if we have at least one non-users table
let list = []
if (internalDS?.entities?.length) {
list.push(internalDS)
}
list = list.concat(otherDS || [])
return {
...$store,
list,
selected: list?.find(ds => ds._id === $store.selectedDatasourceId),
hasDefaultData: list?.some(
datasource => datasource._id === DEFAULT_BB_DATASOURCE_ID
),
hasData: !!internalDS?.entities?.length || list?.length > 1,
}
})
const fetch = async () => { const fetch = async () => {
const datasources = await API.getDatasources() const datasources = await API.getDatasources()