1
0
Fork 0
mirror of synced 2024-06-26 10:00:41 +12:00
budibase/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte

75 lines
1.9 KiB
Svelte
Raw Normal View History

2020-12-19 07:19:43 +13:00
<script>
2021-04-01 22:29:47 +13:00
import { goto } from "@roxi/routify"
2021-06-19 00:33:44 +12:00
import { datasources } from "stores/backend"
import { notifications } from "@budibase/bbui"
import { Input, Label, ModalContent, Modal, Context } from "@budibase/bbui"
2020-12-19 07:19:43 +13:00
import TableIntegrationMenu from "../TableIntegrationMenu/index.svelte"
import CreateTableModal from "components/backend/TableNavigator/modals/CreateTableModal.svelte"
2020-12-19 07:19:43 +13:00
import analytics from "analytics"
import { getContext } from "svelte"
2020-12-19 07:19:43 +13:00
const modalContext = getContext(Context.Modal)
2020-12-19 07:19:43 +13:00
let tableModal
2020-12-19 07:19:43 +13:00
let name
let error = ""
2020-12-19 07:19:43 +13:00
let integration
$: checkOpenModal(integration && integration.type === "BUDIBASE")
2020-12-19 07:19:43 +13:00
function checkValid(evt) {
const datasourceName = evt.target.value
2021-01-16 02:42:55 +13:00
if (
2021-05-04 22:32:22 +12:00
$datasources?.list.some(datasource => datasource.name === datasourceName)
2021-01-16 02:42:55 +13:00
) {
2020-12-19 07:19:43 +13:00
error = `Datasource with name ${datasourceName} already exists. Please choose another name.`
return
}
error = ""
}
function checkOpenModal(isInternal) {
if (isInternal) {
tableModal.show()
}
}
2020-12-19 07:19:43 +13:00
async function saveDatasource() {
const { type, plus, ...config } = integration
2020-12-19 07:19:43 +13:00
// Create datasource
const response = await datasources.save({
2020-12-19 07:19:43 +13:00
name,
source: type,
2021-01-16 02:42:55 +13:00
config,
2021-06-15 06:07:13 +12:00
plus,
2020-12-19 07:19:43 +13:00
})
notifications.success(`Datasource ${name} created successfully.`)
analytics.captureEvent("Datasource Created", { name, type })
2020-12-19 07:19:43 +13:00
// Navigate to new datasource
2020-12-31 00:46:37 +13:00
$goto(`./datasource/${response._id}`)
2020-12-19 07:19:43 +13:00
}
</script>
<Modal bind:this={tableModal} on:hide={modalContext.hide}>
<CreateTableModal bind:name />
</Modal>
2020-12-19 07:19:43 +13:00
<ModalContent
title="Create Datasource"
2021-05-04 22:07:26 +12:00
size="L"
2020-12-19 07:19:43 +13:00
confirmText="Create"
onConfirm={saveDatasource}
disabled={error || !name || !integration?.type}
>
2020-12-19 07:19:43 +13:00
<Input
data-cy="datasource-name-input"
label="Datasource Name"
on:input={checkValid}
bind:value={name}
{error}
/>
2021-04-23 00:38:35 +12:00
<Label>Datasource Type</Label>
2020-12-19 07:19:43 +13:00
<TableIntegrationMenu bind:integration />
</ModalContent>