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

117 lines
3.1 KiB
Svelte
Raw Normal View History

<script>
import { store, allScreens, selectedAccessRole } from "builderStore"
import { tables } from "stores/backend"
import { roles } from "stores/backend"
import { Input, Select, ModalContent, Toggle } from "@budibase/bbui"
import getTemplates from "builderStore/store/screenTemplates"
import analytics from "analytics"
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 = $selectedAccessRole || "BASIC"
2020-02-25 16:21:23 +01:00
$: templates = getTemplates($store, $tables.list)
$: route = !route && $allScreens.length === 0 ? "*" : route
2020-10-09 12:58:46 +02:00
$: {
if (templates && templateIndex === undefined) {
templateIndex = 0
templateChanged(0)
}
}
2021-05-04 12:32:22 +02:00
const templateChanged = newTemplateIndex => {
2020-10-09 12:58:46 +02:00
if (newTemplateIndex === undefined) return
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 }
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,
})
}
2020-02-03 10:50:30 +01:00
}
const routeExists = (route, roleId) => {
return $allScreens.some(
2021-05-04 12:32:22 +02:00
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
}
2021-05-04 12:32:22 +02:00
const routeChanged = event => {
2021-04-20 13:48:37 +02:00
if (!event.detail.startsWith("/")) {
route = "/" + event.detail
2020-02-25 11:01:07 +01:00
}
route = route.replace(/ +/g, "-")
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}
2021-05-04 12:32:22 +02:00
on:change={ev => templateChanged(ev.detail)}
2021-04-20 13:48:37 +02:00
options={templates}
placeholder={null}
2021-05-04 12:32:22 +02:00
getOptionLabel={x => x.name}
getOptionValue={(x, idx) => idx}
/>
<Input label="Name" bind:value={name} />
<Input
label="Url"
error={routeError}
bind:value={route}
on:change={routeChanged}
/>
2021-04-20 13:48:37 +02:00
<Select
label="Access"
bind:value={roleId}
options={$roles}
2021-05-04 12:32:22 +02:00
getOptionLabel={x => x.name}
getOptionValue={x => x._id}
/>
2021-04-20 13:48:37 +02:00
<Toggle text="Create link in navigation bar" bind:value={createLink} />
2020-10-08 10:34:22 +02:00
</ModalContent>