1
0
Fork 0
mirror of synced 2024-09-21 11:53:49 +12:00
budibase/packages/builder/src/components/start/CreateAppModal.svelte

319 lines
8.5 KiB
Svelte
Raw Normal View History

<script>
import { writable, get as svelteGet } from "svelte/store"
2021-02-24 07:37:37 +13:00
import { notifier } from "builderStore/store/notifications"
2021-01-07 06:28:22 +13:00
import {
store,
automationStore,
backendUiStore,
hostingStore,
} from "builderStore"
2020-08-04 01:59:50 +12:00
import { string, object } from "yup"
2020-08-04 02:26:28 +12:00
import api, { get } from "builderStore/api"
2020-08-04 01:59:50 +12:00
import Form from "@svelteschool/svelte-forms"
2020-05-27 22:54:53 +12:00
import Spinner from "components/common/Spinner.svelte"
2020-07-31 20:46:23 +12:00
import { API, Info, User } from "./Steps"
import Indicator from "./Indicator.svelte"
2020-10-27 22:17:27 +13:00
import { Button } from "@budibase/bbui"
2021-03-18 00:40:24 +13:00
import { goto } from "@roxi/routify"
2020-05-27 22:54:53 +12:00
import { fade } from "svelte/transition"
import { post } from "builderStore/api"
import analytics from "analytics"
2021-01-07 06:28:22 +13:00
import { onMount } from "svelte"
2020-05-27 22:54:53 +12:00
//Move this to context="module" once svelte-forms is updated so that it can bind to stores correctly
const createAppStore = writable({ currentStep: 0, values: {} })
2020-05-27 02:25:37 +12:00
2020-09-26 01:47:42 +12:00
export let template
2020-05-27 22:54:53 +12:00
let lastApiKey
let fetchApiKeyPromise
const infoValidation = {
applicationName: string().required("Your application must have a name."),
}
const userValidation = {
email: string()
.email()
.required("Your application needs a first user."),
2021-01-08 04:39:49 +13:00
password: string().required("Please enter a password for your first user."),
roleId: string().required("You need to select a role for your user."),
}
2020-08-04 01:59:50 +12:00
let submitting = false
let errors = {}
let validationErrors = {}
let validationSchemas = [infoValidation, userValidation]
2020-08-04 01:59:50 +12:00
2021-01-29 08:35:04 +13:00
function buildStep(component) {
2021-01-07 10:25:52 +13:00
return {
component,
errors,
}
}
// steps need to be initialized for cypress from the get go
let steps = [buildStep(Info), buildStep(User)]
onMount(async () => {
let hostingInfo = await hostingStore.actions.fetch()
2021-01-07 10:25:52 +13:00
// re-init the steps based on whether self hosting or cloud hosted
if (hostingInfo.type === "self") {
await hostingStore.actions.fetchDeployedApps()
const existingAppNames = svelteGet(hostingStore).deployedAppNames
2021-01-15 06:02:05 +13:00
infoValidation.applicationName = string()
.required("Your application must have a name.")
2021-02-24 07:37:37 +13:00
.test(
"non-existing-app-name",
"App with same name already exists. Please try another app name.",
value =>
2021-02-25 00:57:53 +13:00
!existingAppNames.some(
appName => appName.toLowerCase() === value.toLowerCase()
2021-02-24 07:37:37 +13:00
)
)
2021-01-07 10:25:52 +13:00
steps = [buildStep(Info), buildStep(User)]
2021-01-08 04:39:49 +13:00
validationSchemas = [infoValidation, userValidation]
}
})
2020-08-04 01:59:50 +12:00
// Handles form navigation
const back = () => {
if ($createAppStore.currentStep > 0) {
$createAppStore.currentStep -= 1
}
}
const next = () => {
$createAppStore.currentStep += 1
}
// $: errors = validationSchemas.validate(values);
$: getErrors(
$createAppStore.values,
validationSchemas[$createAppStore.currentStep]
)
async function getErrors(values, schema) {
try {
validationErrors = {}
await object(schema).validate(values, { abortEarly: false })
} catch (error) {
validationErrors = extractErrors(error)
}
}
const checkValidity = async (values, currentStep) => {
const validity = await object()
.shape(validationSchemas[currentStep])
.isValid(values)
currentStepIsValid = validity
// Check full form on last step
if (currentStep === steps.length - 1) {
// Make one big schema from all the small ones
const fullSchema = Object.assign({}, ...validationSchemas)
// Check full form schema
2020-10-28 04:28:13 +13:00
const formIsValid = await object()
.shape(fullSchema)
.isValid(values)
2020-08-04 01:59:50 +12:00
fullFormIsValid = formIsValid
}
}
async function createNewApp() {
2020-08-04 01:59:50 +12:00
submitting = true
try {
2021-03-16 07:32:20 +13:00
// Create form data to create app
let data = new FormData()
data.append("name", $createAppStore.values.applicationName)
data.append("useTemplate", template != null)
if (template) {
data.append("templateName", template.name)
data.append("templateKey", template.key)
data.append("templateFile", template.file)
}
2020-08-04 02:26:28 +12:00
// Create App
2021-03-16 07:32:20 +13:00
const appResp = await post("/api/applications", data, {})
2020-08-04 02:26:28 +12:00
const appJson = await appResp.json()
2021-02-24 07:37:37 +13:00
if (!appResp.ok) {
throw new Error(appJson.message)
}
analytics.captureEvent("App Created", {
name: $createAppStore.values.applicationName,
2020-08-04 02:26:28 +12:00
appId: appJson._id,
2020-09-26 01:47:42 +12:00
template,
2020-08-04 01:59:50 +12:00
})
2020-08-04 02:26:28 +12:00
// Select Correct Application/DB in prep for creating user
2020-11-20 05:56:23 +13:00
const applicationPkg = await get(
`/api/applications/${appJson._id}/appPackage`
)
2020-08-04 02:26:28 +12:00
const pkg = await applicationPkg.json()
if (applicationPkg.ok) {
backendUiStore.actions.reset()
2020-11-05 06:09:45 +13:00
await store.actions.initialise(pkg)
2020-11-25 07:11:34 +13:00
await automationStore.actions.fetch()
2020-08-04 02:26:28 +12:00
} else {
throw new Error(pkg)
}
// Create user
const user = {
2020-12-05 01:22:45 +13:00
email: $createAppStore.values.email,
2020-08-04 02:26:28 +12:00
password: $createAppStore.values.password,
roleId: $createAppStore.values.roleId,
2020-08-04 02:26:28 +12:00
}
const userResp = await api.post(`/api/users`, user)
const json = await userResp.json()
$goto(`./${appJson._id}`)
2020-08-04 01:59:50 +12:00
} catch (error) {
console.error(error)
2021-02-24 07:37:37 +13:00
notifier.danger(error)
submitting = false
2020-05-27 21:44:15 +12:00
}
}
2020-05-27 02:25:37 +12:00
2020-08-04 01:59:50 +12:00
async function updateKey([key, value]) {
const response = await api.put(`/api/keys/${key}`, { value })
const res = await response.json()
return res
}
function extractErrors({ inner }) {
if (!inner) return {}
2020-08-04 01:59:50 +12:00
return inner.reduce((acc, err) => {
return { ...acc, [err.path]: err.message }
}, {})
}
let currentStepIsValid = false
let fullFormIsValid = false
$: checkValidity($createAppStore.values, $createAppStore.currentStep)
2020-05-27 02:25:37 +12:00
let onChange = () => {}
</script>
2020-10-08 21:35:11 +13:00
<div class="container">
<div class="sidebar">
{#each steps as { active, done }, i}
<Indicator
active={$createAppStore.currentStep === i}
done={i < $createAppStore.currentStep}
step={i + 1} />
{/each}
</div>
<div class="body">
<div class="heading">
<h3 class="header">Get Started with Budibase</h3>
</div>
<div class="step">
<Form bind:values={$createAppStore.values}>
{#each steps as step, i (i)}
<div class:hidden={$createAppStore.currentStep !== i}>
<svelte:component
this={step.component}
{template}
{validationErrors}
options={step.options}
name={step.name} />
</div>
{/each}
</Form>
2020-07-31 20:46:23 +12:00
</div>
2020-10-08 21:35:11 +13:00
<div class="footer">
{#if $createAppStore.currentStep > 0}
<Button medium secondary on:click={back}>Back</Button>
{/if}
{#if $createAppStore.currentStep < steps.length - 1}
<Button medium blue on:click={next} disabled={!currentStepIsValid}>
Next
</Button>
{/if}
{#if $createAppStore.currentStep === steps.length - 1}
<Button
medium
blue
on:click={createNewApp}
2020-10-08 21:35:11 +13:00
disabled={!fullFormIsValid || submitting}>
{submitting ? 'Loading...' : 'Submit'}
</Button>
{/if}
2020-05-27 02:25:37 +12:00
</div>
2020-05-27 22:54:53 +12:00
</div>
<img src="/assets/bb-logo.svg" alt="budibase icon" />
2020-10-08 21:35:11 +13:00
{#if submitting}
<div in:fade class="spinner-container">
<Spinner />
<span class="spinner-text">Creating your app...</span>
</div>
{/if}
</div>
2020-05-27 02:25:37 +12:00
<style>
2020-05-27 22:54:53 +12:00
.container {
2020-08-04 01:59:50 +12:00
min-height: 600px;
2020-07-31 20:46:23 +12:00
display: grid;
grid-template-columns: 80px 1fr;
2020-05-27 22:54:53 +12:00
position: relative;
}
2020-07-31 20:46:23 +12:00
.sidebar {
display: grid;
border-bottom-left-radius: 0.5rem;
border-top-left-radius: 0.5rem;
grid-gap: 30px;
align-content: center;
2020-10-30 09:42:34 +13:00
background: var(--grey-1);
2020-07-31 20:46:23 +12:00
}
2020-05-27 02:25:37 +12:00
.heading {
display: flex;
flex-direction: row;
align-items: center;
2020-05-27 03:37:11 +12:00
margin-bottom: 20px;
2020-05-27 02:25:37 +12:00
}
.header {
2020-05-27 02:25:37 +12:00
margin: 0;
font-size: 24px;
font-weight: 600;
2020-05-27 02:25:37 +12:00
}
.body {
2020-07-31 20:46:23 +12:00
padding: 40px 60px 60px 60px;
2020-05-27 02:25:37 +12:00
display: grid;
2020-08-04 01:59:50 +12:00
align-items: center;
2020-07-31 20:46:23 +12:00
grid-template-rows: auto 1fr auto;
2020-05-27 02:25:37 +12:00
}
.footer {
display: grid;
2020-07-31 20:46:23 +12:00
grid-gap: 15px;
grid-template-columns: auto auto;
justify-content: end;
2020-05-27 02:25:37 +12:00
}
2020-05-27 22:54:53 +12:00
.spinner-container {
2020-10-30 09:42:34 +13:00
background: var(--background);
2020-05-27 22:54:53 +12:00
position: absolute;
border-radius: 5px;
left: 0;
top: 0;
right: 0;
bottom: 0;
display: grid;
justify-items: center;
align-content: center;
grid-gap: 50px;
}
.spinner-text {
font-size: 2em;
}
2020-08-04 01:59:50 +12:00
.hidden {
display: none;
2020-05-27 23:48:38 +12:00
}
2020-08-04 02:30:26 +12:00
img {
position: absolute;
top: 20px;
left: 20px;
height: 40px;
}
2020-05-27 02:25:37 +12:00
</style>