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

118 lines
2.7 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, Modal, Button, Spacer } from "@budibase/bbui"
2020-02-03 10:50:30 +01:00
import { find, filter, some, map, includes } from "lodash/fp"
import { assign } from "lodash"
export const show = () => {
dialog.show()
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
2020-02-03 10:50:30 +01:00
store.createScreen(name, route, layoutComponent._component)
name = ""
route = ""
dialog.hide()
2020-02-03 10:50:30 +01:00
}
const cancel = () => {
dialog.hide()
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:this={dialog} minWidth="500px">
2020-09-03 09:59:05 +02:00
<h2>New Screen</h2>
<Spacer extraLarge />
2020-09-03 09:59:05 +02:00
<div data-cy="new-screen-dialog">
<div class="bb-margin-xl">
<Input label="Name" bind:value={name} />
</div>
<div class="bb-margin-xl">
<Input
label="Url"
error={routeError}
bind:value={route}
on:change={routeChanged} />
</div>
<div class="bb-margin-xl">
<label>Layout Component</label>
<Select bind:value={layoutComponent} secondary>
{#each layoutComponents as { _component, name }}
<option value={_component}>{name}</option>
{/each}
</Select>
</div>
2020-09-03 09:59:05 +02:00
</div>
<Spacer extraLarge />
2020-02-03 10:50:30 +01:00
2020-09-05 01:03:08 +02:00
<div data-cy="create-screen-footer" class="modal-footer">
<Button secondary medium on:click={cancel}>Cancel</Button>
<Button blue medium on:click={save}>Create Screen</Button>
</div>
2020-09-03 09:59:05 +02:00
</Modal>
<style>
h2 {
font-size: var(--font-size-xl);
margin: 0;
font-family: var(--font-sans);
font-weight: 600;
}
2019-10-07 07:03:41 +02:00
.modal-footer {
display: flex;
justify-content: space-between;
2020-09-03 09:59:05 +02:00
}
</style>