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

135 lines
3.9 KiB
Svelte
Raw Normal View History

<script>
import { writable, get as svelteGet } from "svelte/store"
2021-12-09 07:51:24 +13:00
import { notifications, Input, ModalContent, Dropzone } from "@budibase/bbui"
import { store, automationStore, hostingStore } from "builderStore"
2021-12-09 07:52:06 +13:00
import { admin, auth } from "stores/portal"
import api, { get, post } from "builderStore/api"
2021-09-21 22:47:14 +12:00
import analytics, { Events } from "analytics"
2021-01-07 06:28:22 +13:00
import { onMount } from "svelte"
2021-05-08 00:13:51 +12:00
import { goto } from "@roxi/routify"
import { createValidationStore } from "helpers/validation/yup"
import * as appValidation from "helpers/validation/yup/app"
2020-05-27 22:54:53 +12:00
2020-09-26 01:47:42 +12:00
export let template
2021-12-15 04:30:20 +13:00
const values = writable({ name: "", url: null })
const validation = createValidationStore()
$: validation.check($values)
onMount(async () => {
await setupValidation()
})
2020-08-04 01:59:50 +12:00
const setupValidation = async () => {
await hostingStore.actions.fetchDeployedApps()
const apps = svelteGet(hostingStore).deployedApps
appValidation.name(validation, { apps })
appValidation.url(validation, { apps })
appValidation.file(validation, { template })
// init validation
validation.check($values)
2020-08-04 01:59:50 +12:00
}
async function createNewApp() {
2020-08-04 01:59:50 +12:00
try {
2021-03-16 07:32:20 +13:00
// Create form data to create app
let data = new FormData()
data.append("name", $values.name.trim())
if ($values.url) {
data.append("url", $values.url.trim())
}
data.append("useTemplate", template != null)
if (template) {
data.append("templateName", template.name)
data.append("templateKey", template.key)
data.append("templateFile", $values.file)
2021-03-16 07:32:20 +13:00
}
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)
}
2021-09-21 22:47:14 +12:00
analytics.captureEvent(Events.APP.CREATED, {
name: $values.name,
2021-09-21 22:47:14 +12:00
appId: appJson.instance._id,
templateToUse: 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(
2021-05-17 08:25:37 +12:00
`/api/applications/${appJson.instance._id}/appPackage`
2020-11-20 05:56:23 +13:00
)
2020-08-04 02:26:28 +12:00
const pkg = await applicationPkg.json()
if (applicationPkg.ok) {
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()
// update checklist - incase first app
await admin.init()
2020-08-04 02:26:28 +12:00
} else {
throw new Error(pkg)
}
// Create user
const user = {
roleId: $values.roleId,
2020-08-04 02:26:28 +12:00
}
const userResp = await api.post(`/api/users/metadata/self`, user)
await userResp.json()
2021-11-05 02:03:18 +13:00
await auth.setInitInfo({})
2021-05-17 08:25:37 +12:00
$goto(`/builder/app/${appJson.instance._id}`)
2020-08-04 01:59:50 +12:00
} catch (error) {
console.error(error)
notifications.error(error)
2020-05-27 21:44:15 +12:00
}
}
// auto add slash to url
$: {
if ($values.url && !$values.url.startsWith("/")) {
$values.url = `/${$values.url}`
}
}
2020-05-27 02:25:37 +12:00
</script>
<ModalContent
title={"Create your app"}
confirmText={template?.fromFile ? "Import app" : "Create app"}
onConfirm={createNewApp}
disabled={!$validation.valid}
>
{#if template?.fromFile}
<Dropzone
error={$validation.touched.file && $validation.errors.file}
gallery={false}
label="File to import"
value={[$values.file]}
on:change={e => {
$values.file = e.detail?.[0]
$validation.touched.file = true
}}
/>
{/if}
<Input
bind:value={$values.name}
error={$validation.touched.name && $validation.errors.name}
on:blur={() => ($validation.touched.name = true)}
label="Name"
placeholder={$auth.user.firstName
? `${$auth.user.firstName}s app`
: "My app"}
/>
<Input
bind:value={$values.url}
error={$validation.touched.url && $validation.errors.url}
on:blur={() => ($validation.touched.url = true)}
label="URL"
placeholder={$values.name
? "/" + encodeURIComponent($values.name).toLowerCase()
: "/"}
/>
</ModalContent>