Skip to content

Beautiful Open-Source Form Builder

3.9 KB
Raw Blame
<template>
  <div class="flex flex-wrap flex-col flex-grow">
    <create-form-base-modal
      :show="showInitialFormModal"
      :default-import-source="defaultImportSource"
      @form-generated="formGenerated"
      @form-imported="formImported"
      @close="showInitialFormModal = false"
    />
    <VTransition name="fade">
      <FormEditor
        v-if="editorReady"
        ref="editor"
        class="w-full flex flex-grow"
        :error="error"
        :is-guest="isGuest"
        :loading="workspacesLoading"
        @open-register="appStore.quickRegisterModal = true"
      />
    </VTransition>
  </div>
</template>

<script setup>
import FormEditor from "~/components/open/forms/components/FormEditor.vue"
import CreateFormBaseModal from "../../../components/pages/forms/create/CreateFormBaseModal.vue"
import { initForm } from "~/composables/forms/initForm.js"
import { useQueryClient } from "@tanstack/vue-query"

import { WindowMessageTypes } from "~/composables/useWindowMessage"

const appStore = useAppStore()
const workingFormStore = useWorkingFormStore()
const route = useRoute()
const queryClient = useQueryClient()

let template = null
if (route.query.template) {
  const { data, suspense } = useTemplates().detail(route.query.template)
  await suspense()
  template = data.value
}

// Use workspaces query composable for invalidation functionality
const { invalidateAll } = useWorkspaces()

// Store values
const workspacesLoading = computed(() => {
  // For guest mode, we'll manage loading state manually
  return !stateReady.value
})
const form = storeToRefs(workingFormStore).content

useOpnSeoMeta({
  title: "Create a new Form for free",
})
definePageMeta({
  middleware: ["guest", "self-hosted"],
  layout: 'empty'
})

// Data
const stateReady = ref(false)
const error = ref("")
const isGuest = ref(true)
const showInitialFormModal = ref(false)
const editorBootstrapped = ref(false)
const hasInitialTemplate = computed(() => !!(template && template.structure))
const editorReady = computed(() => stateReady.value && (editorBootstrapped.value || hasInitialTemplate.value))
const supportedGuestImportSources = ['typeform', 'tally', 'fillout']
const defaultImportSource = computed(() => {
  const source = route.query.import
  return supportedGuestImportSources.includes(source) ? source : null
})

// Component ref
const editor = ref(null)

onMounted(() => {
  // Set guest workspace data in query cache instead of store
  const guestWorkspace = {
    id: null,
    name: "Guest Workspace",
    plan_tier: 'free',
    features: [],
    limits: {},
    required_tiers: {},
  }
  
  // Manually set the workspace data in query cache
  queryClient.setQueryData(["workspaces", "list"], [guestWorkspace])

  form.value = initForm({}, true)
  if (template && template.structure) {
    form.value = useForm({ ...form.value.data(), ...template.structure })
  } else {
    // No template loaded, ask how to start
    showInitialFormModal.value = true
  }
  stateReady.value = true

  const scheduleEditorBootstrap = () => {
    editorBootstrapped.value = true
  }

  if (import.meta.client && typeof window.requestIdleCallback === 'function') {
    window.requestIdleCallback(scheduleEditorBootstrap, { timeout: 120 })
  } else if (import.meta.client) {
    window.setTimeout(scheduleEditorBootstrap, 16)
  } else {
    editorBootstrapped.value = true
  }

  // Set up window message listener for after-login
  const afterLoginMessage = useWindowMessage(WindowMessageTypes.AFTER_LOGIN)
  afterLoginMessage.listen(() => {
    afterLogin()
  }, { useMessageChannel: false })
})

const afterLogin = () => {
  isGuest.value = false
  invalidateAll() // Refetch all workspace queries
  setTimeout(() => {
    if (editor) {
      editor.value.saveFormCreate()
    }
  }, 500)
}

const formGenerated = (newForm) => {
  form.value = useForm({ ...form.value.data(), ...newForm })
}

const formImported = (importedForm) => {
  form.value = useForm({ ...form.value.data(), ...importedForm })
}
</script>