budibase/packages/builder/src/components/userInterface/NewScreen.svelte

80 lines
2.0 KiB
Svelte
Raw Normal View History

<script>
import { store } from "builderStore"
import { pipe } from "components/common/core"
2020-02-03 10:50:30 +01:00
import { isRootComponent } from "./pagesParsing/searchComponents"
import { splitName } from "./pagesParsing/splitRootComponentName.js"
import { Input, Select, Button, Spacer } from "@budibase/bbui"
import { Modal } from "components/common/Modal"
2020-02-03 10:50:30 +01:00
import { find, filter, some, map, includes } from "lodash/fp"
import { assign } from "lodash"
export let visible
2020-02-03 10:50:30 +01:00
let dialog
2020-02-03 10:50:30 +01:00
let layoutComponents
let layoutComponent
let screens
let name = ""
2020-08-18 18:14:35 +02:00
let routeError
2020-02-25 16:21:23 +01:00
2020-05-07 11:53:34 +02:00
$: layoutComponents = Object.values($store.components).filter(
componentDefinition => componentDefinition.container
)
2020-02-03 10:50:30 +01:00
2020-02-25 11:01:07 +01:00
$: layoutComponent = layoutComponent
2020-05-07 11:53:34 +02:00
? layoutComponents.find(
component => component._component === layoutComponent._component
)
2020-02-25 16:21:23 +01:00
: layoutComponents[0]
2020-02-03 10:50:30 +01:00
$: route = !route && $store.screens.length === 0 ? "*" : route
2020-02-03 10:50:30 +01:00
const save = () => {
if (!route) {
2020-08-18 18:14:35 +02:00
routeError = "Url is required"
} else {
if (routeNameExists(route)) {
2020-08-18 18:14:35 +02:00
routeError = "This url is already taken"
} else {
routeError = ""
}
}
if (routeError) {
return false
}
store.createScreen(name, route, layoutComponent._component)
name = ""
route = ""
2020-02-03 10:50:30 +01:00
}
2020-02-25 11:01:07 +01:00
const routeNameExists = route => {
2020-05-07 11:53:34 +02:00
return $store.screens.some(
screen => screen.route.toLowerCase() === route.toLowerCase()
)
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>
<Modal
bind:visible
title="New Screen"
confirmText="Create Screen"
onConfirm={save}>
<Input label="Name" bind:value={name} />
<Input
label="Url"
error={routeError}
bind:value={route}
on:change={routeChanged} />
<Select label="Layout Component" bind:value={layoutComponent} secondary>
{#each layoutComponents as { _component, name }}
<option value={_component}>{name}</option>
{/each}
</Select>
2020-09-03 09:59:05 +02:00
</Modal>