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

69 lines
1.7 KiB
Svelte
Raw Normal View History

<script>
import { store } from "builderStore"
2020-10-08 10:35:11 +02:00
import { Input, Select, ModalContent } from "@budibase/bbui"
import { find, filter, some } from "lodash/fp"
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>
2020-10-08 10:34:22 +02:00
<ModalContent 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-10-08 10:34:22 +02:00
</ModalContent>