budibase/packages/builder/src/components/design/NavigationPanel/NewScreenModal.svelte

122 lines
3.4 KiB
Svelte
Raw Normal View History

<script>
import { goto } from "@sveltech/routify"
import { store, backendUiStore, allScreens } from "builderStore"
import { Input, Select, ModalContent, Toggle } from "@budibase/bbui"
import getTemplates from "builderStore/store/screenTemplates"
import analytics from "analytics"
import { onMount } from "svelte"
import api from "builderStore/api"
2020-10-08 15:53:06 +02:00
const CONTAINER = "@budibase/standard-components/container"
2020-02-03 10:50:30 +01:00
let name = ""
2020-08-18 18:14:35 +02:00
let routeError
2020-10-08 15:53:06 +02:00
let baseComponent = CONTAINER
2020-10-09 12:58:46 +02:00
let templateIndex
let draftScreen
let createLink = true
let roleId = "BASIC"
2020-02-25 16:21:23 +01:00
$: templates = getTemplates($store, $backendUiStore.tables)
$: route = !route && $allScreens.length === 0 ? "*" : route
2020-10-08 15:53:06 +02:00
$: baseComponents = Object.values($store.components)
.filter(componentDefinition => componentDefinition.baseComponent)
.map(c => c._component)
2020-10-09 12:58:46 +02:00
$: {
if (templates && templateIndex === undefined) {
templateIndex = 0
templateChanged(0)
}
}
const templateChanged = newTemplateIndex => {
if (newTemplateIndex === undefined) return
const template = templates[newTemplateIndex]
draftScreen = templates[newTemplateIndex].create()
if (draftScreen.props._instanceName) {
name = draftScreen.props._instanceName
}
if (draftScreen.props._component) {
baseComponent = draftScreen.props._component
}
if (draftScreen.routing) {
route = draftScreen.routing.route
}
}
const save = async () => {
if (!route) {
routeError = "URL is required"
2020-08-18 18:14:35 +02:00
} else {
if (routeExists(route, roleId)) {
routeError = "This URL is already taken for this access role"
2020-08-18 18:14:35 +02:00
} else {
routeError = ""
}
}
2020-10-08 15:53:06 +02:00
if (routeError) return false
draftScreen.props._instanceName = name
draftScreen.props._component = baseComponent
draftScreen.routing = { route, roleId }
const createdScreen = await store.actions.screens.create(draftScreen)
if (createLink) {
await store.actions.components.links.save(route, name)
}
2020-11-20 12:41:17 +01:00
await store.actions.routing.fetch()
if (templateIndex !== undefined) {
const template = templates[templateIndex]
analytics.captureEvent("Screen Created", {
template: template.id || template.name,
})
}
$goto(`./${createdScreen._id}`)
2020-02-03 10:50:30 +01:00
}
const routeExists = (route, roleId) => {
return $allScreens.some(
screen =>
screen.routing.route.toLowerCase() === route.toLowerCase() &&
screen.routing.roleId === roleId
2020-05-07 11:53:34 +02:00
)
2020-02-25 11:01:07 +01:00
}
const routeChanged = event => {
if (!event.target.value.startsWith("/")) {
route = "/" + event.target.value
2020-02-25 11:01:07 +01:00
}
}
</script>
2020-10-08 10:34:22 +02:00
<ModalContent title="New Screen" confirmText="Create Screen" onConfirm={save}>
2020-10-08 17:15:12 +02:00
<Select
label="Choose a Template"
bind:value={templateIndex}
secondary
2020-10-09 12:58:46 +02:00
on:change={ev => templateChanged(ev.target.value)}>
2020-10-08 17:15:12 +02:00
{#if templates}
{#each templates as template, index}
<option value={index}>{template.name}</option>
{/each}
{/if}
</Select>
<Input label="Name" bind:value={name} />
<Input
label="Url"
error={routeError}
bind:value={route}
on:change={routeChanged} />
<Select label="Access" bind:value={roleId} secondary>
{#each $backendUiStore.roles as role}
<option value={role._id}>{role.name}</option>
{/each}
</Select>
<Toggle text="Create link in navigation bar" bind:checked={createLink} />
2020-10-08 10:34:22 +02:00
</ModalContent>