wip: converts components to use separate stores

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-23 12:16:54 +01:00
parent ac335e4301
commit b4f5522a5e
18 changed files with 51 additions and 123 deletions

View File

@ -1,6 +1,7 @@
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { get } from "svelte/store" import { get } from "svelte/store"
import { backendUiStore, store } from "builderStore" import { store } from "builderStore"
import { tables as tablesStore, queries as queriesStores } from 'builderStore/store/backend/'
import { findComponentPath } from "./storeUtils" import { findComponentPath } from "./storeUtils"
import { makePropSafe } from "@budibase/string-templates" import { makePropSafe } from "@budibase/string-templates"
import { TableNames } from "../constants" import { TableNames } from "../constants"
@ -164,7 +165,7 @@ const getContextBindings = (asset, componentId) => {
*/ */
const getUserBindings = () => { const getUserBindings = () => {
let bindings = [] let bindings = []
const tables = get(backendUiStore).tables const tables = get(tablesStore).list
const userTable = tables.find(table => table._id === TableNames.USERS) const userTable = tables.find(table => table._id === TableNames.USERS)
const schema = { const schema = {
...userTable.schema, ...userTable.schema,
@ -223,10 +224,10 @@ export const getSchemaForDatasource = (datasource, isForm = false) => {
if (datasource) { if (datasource) {
const { type } = datasource const { type } = datasource
if (type === "query") { if (type === "query") {
const queries = get(backendUiStore).queries const queries = get(queriesStores).queries
table = queries.find(query => query._id === datasource._id) table = queries.find(query => query._id === datasource._id)
} else { } else {
const tables = get(backendUiStore).tables const tables = get(tablesStore).tables
table = tables.find(table => table._id === datasource.tableId) table = tables.find(table => table._id === datasource.tableId)
} }
if (table) { if (table) {

View File

@ -1,5 +1,4 @@
import { writable, get } from "svelte/store" import { writable } from "svelte/store"
import { cloneDeep } from "lodash/fp"
import api from "../api" import api from "../api"
const INITIAL_BACKEND_UI_STATE = { const INITIAL_BACKEND_UI_STATE = {
@ -16,18 +15,6 @@ export const getBackendUiStore = () => {
store.actions = { store.actions = {
reset: () => store.set({ ...INITIAL_BACKEND_UI_STATE }), reset: () => store.set({ ...INITIAL_BACKEND_UI_STATE }),
database: {
select: async db => {
const [tables] = await Promise.all([
api.get(`/api/tables`).then(r => r.json()),
])
store.update(state => {
state.tables = tables
return state
})
},
},
rows: { rows: {
save: () => save: () =>
store.update(state => { store.update(state => {
@ -40,40 +27,6 @@ export const getBackendUiStore = () => {
return state return state
}), }),
}, },
views: {
select: view =>
store.update(state => {
state.selectedView = view
state.selectedTable = {}
return state
}),
delete: async view => {
await api.delete(`/api/views/${view}`)
await store.actions.tables.fetch()
},
save: async view => {
const response = await api.post(`/api/views`, view)
const json = await response.json()
const viewMeta = {
name: view.name,
...json,
}
store.update(state => {
const viewTable = state.tables.find(
table => table._id === view.tableId
)
if (view.originalName) delete viewTable.views[view.originalName]
viewTable.views[view.name] = viewMeta
state.tables = state.tables
state.selectedView = viewMeta
return state
})
},
},
} }
return store return store

View File

@ -2,7 +2,6 @@ import { get, writable } from "svelte/store"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { import {
allScreens, allScreens,
backendUiStore,
hostingStore, hostingStore,
currentAsset, currentAsset,
mainLayout, mainLayout,

View File

@ -1,5 +1,6 @@
<script> <script>
import { backendUiStore, automationStore } from "builderStore" import { automationStore } from "builderStore"
import { database } from 'builderStore/store/backend/'
import { goto } from "@sveltech/routify" import { goto } from "@sveltech/routify"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { Input, ModalContent } from "@budibase/bbui" import { Input, ModalContent } from "@budibase/bbui"
@ -8,7 +9,7 @@
let name let name
$: valid = !!name $: valid = !!name
$: instanceId = $backendUiStore.selectedDatabase._id $: instanceId = $database._id
async function createAutomation() { async function createAutomation() {
await automationStore.actions.create({ await automationStore.actions.create({

View File

@ -1,6 +1,7 @@
<script> <script>
import { goto } from "@sveltech/routify" import { goto } from "@sveltech/routify"
import { automationStore, backendUiStore } from "builderStore" import { automationStore } from "builderStore"
import { database } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { DropdownMenu } from "@budibase/bbui" import { DropdownMenu } from "@budibase/bbui"
import { DropdownContainer, DropdownItem } from "components/common/Dropdowns" import { DropdownContainer, DropdownItem } from "components/common/Dropdowns"
@ -11,7 +12,7 @@
let anchor let anchor
let dropdown let dropdown
let confirmDeleteDialog let confirmDeleteDialog
$: instanceId = $backendUiStore.selectedDatabase._id $: instanceId = $database._id
function showModal() { function showModal() {
dropdown.hide() dropdown.hide()

View File

@ -1,5 +1,6 @@
<script> <script>
import { backendUiStore, automationStore } from "builderStore" import { automationStore } from "builderStore"
import { database } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import AutomationBlockSetup from "./AutomationBlockSetup.svelte" import AutomationBlockSetup from "./AutomationBlockSetup.svelte"
import { Button, Modal } from "@budibase/bbui" import { Button, Modal } from "@budibase/bbui"
@ -7,7 +8,7 @@
let webhookModal let webhookModal
$: instanceId = $backendUiStore.selectedDatabase._id $: instanceId = $database._id
$: automation = $automationStore.selectedAutomation?.automation $: automation = $automationStore.selectedAutomation?.automation
$: automationLive = automation?.live $: automationLive = automation?.live

View File

@ -1,5 +1,6 @@
<script> <script>
import { store, backendUiStore, automationStore } from "builderStore" import { automationStore } from "builderStore"
import { database } from 'builderStore/store/backend/'
import WebhookDisplay from "./WebhookDisplay.svelte" import WebhookDisplay from "./WebhookDisplay.svelte"
import { ModalContent } from "@budibase/bbui" import { ModalContent } from "@budibase/bbui"
import { onMount, onDestroy } from "svelte" import { onMount, onDestroy } from "svelte"
@ -10,8 +11,7 @@
let schemaURL let schemaURL
let propCount = 0 let propCount = 0
$: instanceId = $backendUiStore.selectedDatabase._id $: instanceId = $database._id
$: appId = $store.appId
$: automation = $automationStore.selectedAutomation?.automation $: automation = $automationStore.selectedAutomation?.automation
onMount(async () => { onMount(async () => {

View File

@ -1,5 +1,6 @@
<script> <script>
import { backendUiStore } from "builderStore" import { tables, views } from 'builderStore/store/backend/'
import CreateRowButton from "./buttons/CreateRowButton.svelte" import CreateRowButton from "./buttons/CreateRowButton.svelte"
import CreateColumnButton from "./buttons/CreateColumnButton.svelte" import CreateColumnButton from "./buttons/CreateColumnButton.svelte"
import CreateViewButton from "./buttons/CreateViewButton.svelte" import CreateViewButton from "./buttons/CreateViewButton.svelte"
@ -16,19 +17,19 @@
let hideAutocolumns = true let hideAutocolumns = true
let data = [] let data = []
let loading = false let loading = false
$: isUsersTable = $backendUiStore.selectedTable?._id === TableNames.USERS $: isUsersTable = $tables.selected?._id === TableNames.USERS
$: title = $backendUiStore.selectedTable.name $: title = $tables.selected.name
$: schema = $backendUiStore.selectedTable.schema $: schema = $tables.selected.schema
$: tableView = { $: tableView = {
schema, schema,
name: $backendUiStore.selectedView.name, name: $views.selected.name,
} }
// Fetch rows for specified table // Fetch rows for specified table
$: { $: {
if ($backendUiStore.selectedView?.name?.startsWith("all_")) { if ($views.selected?.name?.startsWith("all_")) {
loading = true loading = true
api.fetchDataForView($backendUiStore.selectedView).then(rows => { api.fetchDataForView($views.selected).then(rows => {
data = rows || [] data = rows || []
loading = false loading = false
}) })
@ -39,7 +40,7 @@
<Table <Table
{title} {title}
{schema} {schema}
tableId={$backendUiStore.selectedTable?._id} tableId={$tables.selected?._id}
{data} {data}
allowEditing={true} allowEditing={true}
bind:hideAutocolumns bind:hideAutocolumns
@ -50,7 +51,7 @@
title={isUsersTable ? 'Create New User' : 'Create New Row'} title={isUsersTable ? 'Create New User' : 'Create New Row'}
modalContentComponent={isUsersTable ? CreateEditUser : CreateEditRow} /> modalContentComponent={isUsersTable ? CreateEditUser : CreateEditRow} />
<CreateViewButton /> <CreateViewButton />
<ManageAccessButton resourceId={$backendUiStore.selectedTable?._id} /> <ManageAccessButton resourceId={$tables.selected?._id} />
{#if isUsersTable} {#if isUsersTable}
<EditRolesButton /> <EditRolesButton />
{/if} {/if}

View File

@ -1,8 +1,4 @@
<script> <script>
import { params } from "@sveltech/routify"
import { backendUiStore } from "builderStore"
import { notifier } from "builderStore/store/notifications"
import * as api from "./api"
import Table from "./Table.svelte" import Table from "./Table.svelte"
export let query = {} export let query = {}

View File

@ -1,7 +1,7 @@
<script> <script>
import { Button, Select } from "@budibase/bbui" import { Button, Select } from "@budibase/bbui"
import { backendUiStore } from "builderStore"
import { tables } from 'builderStore/store/backend/' import { tables, views } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import analytics from "analytics" import analytics from "analytics"
@ -24,7 +24,7 @@
export let onClosed export let onClosed
$: viewTable = $tables.list.find( $: viewTable = $tables.list.find(
({ _id }) => _id === $backendUiStore.selectedView.tableId ({ _id }) => _id === $views.selected.tableId
) )
$: fields = $: fields =
viewTable && viewTable &&
@ -37,7 +37,7 @@
) )
function saveView() { function saveView() {
backendUiStore.actions.views.save(view) views.save(view)
notifier.success(`View ${view.name} saved.`) notifier.success(`View ${view.name} saved.`)
onClosed() onClosed()
analytics.captureEvent("Added View Calculate", { field: view.field }) analytics.captureEvent("Added View Calculate", { field: view.field })

View File

@ -1,7 +1,6 @@
<script> <script>
import { Button, Input, Select, DatePicker } from "@budibase/bbui" import { Button, Input, Select, DatePicker } from "@budibase/bbui"
import { backendUiStore } from "builderStore" import { tables, views } from 'builderStore/store/backend/'
import { tables } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import analytics from "analytics" import analytics from "analytics"
@ -51,12 +50,12 @@
export let onClosed export let onClosed
$: viewTable = $tables.list.find( $: viewTable = $tables.list.find(
({ _id }) => _id === $backendUiStore.selectedView.tableId ({ _id }) => _id === $views.selected.tableId
) )
$: fields = viewTable && Object.keys(viewTable.schema) $: fields = viewTable && Object.keys(viewTable.schema)
function saveView() { function saveView() {
backendUiStore.actions.views.save(view) views.save(view)
notifier.success(`View ${view.name} saved.`) notifier.success(`View ${view.name} saved.`)
onClosed() onClosed()
analytics.captureEvent("Added View Filter", { analytics.captureEvent("Added View Filter", {

View File

@ -1,7 +1,6 @@
<script> <script>
import { Button, Select } from "@budibase/bbui" import { Button, Select } from "@budibase/bbui"
import { backendUiStore } from "builderStore" import { tables, views } from 'builderStore/store/backend/'
import { tables } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { FIELDS } from "constants/backend" import { FIELDS } from "constants/backend"
@ -9,7 +8,7 @@
export let onClosed export let onClosed
$: viewTable = $tables.list.find( $: viewTable = $tables.list.find(
({ _id }) => _id === $backendUiStore.selectedView.tableId ({ _id }) => _id === $views.selected.tableId
) )
$: fields = $: fields =
viewTable && viewTable &&
@ -18,7 +17,7 @@
.map(([key]) => key) .map(([key]) => key)
function saveView() { function saveView() {
backendUiStore.actions.views.save(view) views.save(view)
notifier.success(`View ${view.name} saved.`) notifier.success(`View ${view.name} saved.`)
onClosed() onClosed()
} }

View File

@ -1,7 +1,6 @@
<script> <script>
import { backendUiStore, store, allScreens } from "builderStore"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { DropdownMenu, Button, Input } from "@budibase/bbui" import { DropdownMenu } from "@budibase/bbui"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { DropdownContainer, DropdownItem } from "components/common/Dropdowns" import { DropdownContainer, DropdownItem } from "components/common/Dropdowns"
@ -10,8 +9,6 @@
let anchor let anchor
let dropdown let dropdown
let confirmDeleteDialog let confirmDeleteDialog
let error = ""
let willBeDeleted
function hideEditor() { function hideEditor() {
dropdown?.hide() dropdown?.hide()
@ -62,22 +59,4 @@
div.icon i { div.icon i {
font-size: 16px; font-size: 16px;
} }
.actions {
padding: var(--spacing-xl);
display: grid;
grid-gap: var(--spacing-xl);
min-width: 400px;
}
h5 {
margin: 0;
font-weight: 500;
}
footer {
display: flex;
justify-content: flex-end;
gap: var(--spacing-m);
}
</style> </style>

View File

@ -1,14 +1,13 @@
<script> <script>
import { goto } from "@sveltech/routify" import { goto } from "@sveltech/routify"
import { backendUiStore } from "builderStore" import { tables, views, database } from 'builderStore/store/backend/'
import { tables, database } from 'builderStore/store/backend/'
import { TableNames } from "constants" import { TableNames } from "constants"
import EditTablePopover from "./popovers/EditTablePopover.svelte" import EditTablePopover from "./popovers/EditTablePopover.svelte"
import EditViewPopover from "./popovers/EditViewPopover.svelte" import EditViewPopover from "./popovers/EditViewPopover.svelte"
import NavItem from "components/common/NavItem.svelte" import NavItem from "components/common/NavItem.svelte"
$: selectedView = $: selectedView =
$backendUiStore.selectedView && $backendUiStore.selectedView.name $views.selected && $views.selected.name
function selectTable(table) { function selectTable(table) {
tables.select(table) tables.select(table)
@ -16,7 +15,7 @@
} }
function selectView(view) { function selectView(view) {
backendUiStore.actions.views.select(view) views.select(view)
$goto(`./view/${view.name}`) $goto(`./view/${view.name}`)
} }

View File

@ -2,7 +2,7 @@
import { Input, Label, TextButton } from "@budibase/bbui" import { Input, Label, TextButton } from "@budibase/bbui"
import api from "builderStore/api" import api from "builderStore/api"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { backendUiStore } from "builderStore" import { database } from 'builderStore/store/backend/'
import analytics from "analytics" import analytics from "analytics"
let keys = { budibase: "" } let keys = { budibase: "" }
@ -46,7 +46,7 @@
</TextButton> </TextButton>
<div> <div>
<Label extraSmall grey>Instance ID (Webhooks)</Label> <Label extraSmall grey>Instance ID (Webhooks)</Label>
<span>{$backendUiStore.selectedDatabase._id}</span> <span>{$database._id}</span>
</div> </div>
</div> </div>

View File

@ -1,7 +1,6 @@
<script> <script>
import { goto, beforeUrlChange } from "@sveltech/routify" import { goto, beforeUrlChange } from "@sveltech/routify"
import { Button, Heading, Body, Spacer } from "@budibase/bbui" import { Button, Heading, Body, Spacer } from "@budibase/bbui"
import { backendUiStore } from "builderStore"
import { datasources, integrations, queries } from 'builderStore/store/backend/' import { datasources, integrations, queries } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte" import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte"
@ -22,7 +21,7 @@
} }
function onClickQuery(query) { function onClickQuery(query) {
if ($backendUiStore.selectedQueryId === query._id) { if ($queries.selected === query._id) {
return return
} }
queries.select(query) queries.select(query)

View File

@ -1,11 +1,11 @@
<script> <script>
import TableDataTable from "components/backend/DataTable/DataTable.svelte" import TableDataTable from "components/backend/DataTable/DataTable.svelte"
import { backendUiStore } from "builderStore" import { tables, database } from 'builderStore/store/backend/'
$: selectedTable = $backendUiStore.selectedTable $: selectedTable = $tables.selected
</script> </script>
{#if $backendUiStore.selectedDatabase._id && selectedTable.name} {#if $database._id && selectedTable.name}
<TableDataTable /> <TableDataTable />
{:else}<i>Create your first table to start building</i>{/if} {:else}<i>Create your first table to start building</i>{/if}

View File

@ -1,11 +1,11 @@
<script> <script>
import ViewDataTable from "components/backend/DataTable/ViewDataTable" import ViewDataTable from "components/backend/DataTable/ViewDataTable"
import { backendUiStore } from "builderStore" import { views, database } from 'builderStore/store/backend/'
$: selectedView = $backendUiStore.selectedView $: selectedView = $views.selected
</script> </script>
{#if $backendUiStore.selectedDatabase._id && selectedView} {#if $database._id && selectedView}
<ViewDataTable view={selectedView} /> <ViewDataTable view={selectedView} />
{:else}<i>Create your first table to start building</i>{/if} {:else}<i>Create your first table to start building</i>{/if}