-
Notifications
You must be signed in to change notification settings - Fork 19
Swag form #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Swag form #196
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| <script setup lang="ts"> | ||
| import { onMounted, onBeforeUnmount, ref } from 'vue'; | ||
| import type { BlockProps } from './types'; | ||
| import useVisualEditing from '~/composables/useVisualEditing'; | ||
|
|
||
|
|
@@ -15,13 +16,63 @@ const { data: block, refresh } = useAsyncData(props.uuid, () => | |
| 'alignment', | ||
| 'show_labels', | ||
| 'inline', | ||
| { form: ['hubspot_form_id', 'typeform_form_id', 'route_to_meeting_link_on_success'] }, | ||
| { | ||
| form: [ | ||
| 'hubspot_form_id', | ||
| 'typeform_form_id', | ||
| 'internal_form_url', | ||
| 'route_to_meeting_link_on_success', | ||
| 'form_config', | ||
| ], | ||
| }, | ||
| ], | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const { data: formConfigSlug } = useAsyncData(`form-config-slug-${block.value?.form?.form_config}`, async () => { | ||
| if (block.value?.form?.form_config && typeof block.value.form.form_config === 'string') { | ||
| const config = await $directus.request( | ||
| $readItem('internal_form_config', block.value.form.form_config, { | ||
| fields: ['slug'], | ||
| }), | ||
| ); | ||
|
|
||
| return config.slug; | ||
| } | ||
|
|
||
| return null; | ||
| }); | ||
|
|
||
| autoApply(`[data-block-id="${props.uuid}"]`, refresh); | ||
|
|
||
| const iframeRef = ref<HTMLIFrameElement | null>(null); | ||
|
|
||
| const handleMessage = (event: MessageEvent) => { | ||
| if (!iframeRef.value || !block.value?.form.internal_form_url) return; | ||
|
|
||
| const allowedOrigin = new URL(block.value.form.internal_form_url).origin; | ||
|
|
||
| if (allowedOrigin !== '*' && event.origin !== allowedOrigin) return; | ||
|
|
||
LZylstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const { type, height, scrollToTop } = event.data || {}; | ||
|
|
||
| if (type === 'set-height' && typeof height === 'number') { | ||
| iframeRef.value.style.height = `${height}px`; | ||
|
|
||
| if (scrollToTop) { | ||
| window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| onMounted(() => { | ||
| window.addEventListener('message', handleMessage); | ||
| }); | ||
|
|
||
| onBeforeUnmount(() => { | ||
| window.removeEventListener('message', handleMessage); | ||
| }); | ||
LZylstra marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+69
to
+79
|
||
| </script> | ||
|
|
||
| <template> | ||
|
|
@@ -44,8 +95,9 @@ autoApply(`[data-block-id="${props.uuid}"]`, refresh); | |
| : undefined | ||
| " | ||
| /> | ||
|
|
||
| <BaseTypeForm | ||
| v-if="block && block.form.typeform_form_id" | ||
| v-else-if="block && block.form.typeform_form_id" | ||
| :data-block-id="props.uuid" | ||
| :form-id="block.form.typeform_form_id" | ||
| :labels="block.show_labels" | ||
|
|
@@ -62,4 +114,63 @@ autoApply(`[data-block-id="${props.uuid}"]`, refresh); | |
| : undefined | ||
| " | ||
| /> | ||
|
|
||
| <div | ||
| v-else-if="block && block.form.internal_form_url" | ||
| class="form" | ||
| :class="[{ 'hide-labels': !block.show_labels, inline: block.inline }, `align-${block.alignment ?? 'center'}`]" | ||
| :data-block-id="props.uuid" | ||
| :data-directus=" | ||
| isVisualEditingEnabled | ||
| ? setAttr({ | ||
| collection: 'block_form', | ||
| item: block.id, | ||
| fields: ['alignment', 'show_labels', 'inline', 'form'], | ||
| mode: 'modal', | ||
| }) | ||
| : undefined | ||
| " | ||
| > | ||
| <iframe | ||
| :src="formConfigSlug ? `${block.form.internal_form_url}?config=${formConfigSlug}` : block.form.internal_form_url" | ||
| ref="iframeRef" | ||
| class="w-full border-none" | ||
| scrolling="no" | ||
| frameborder="0" | ||
| /> | ||
| </div> | ||
| </template> | ||
|
|
||
| <style scoped lang="scss"> | ||
| .form { | ||
| &.align-left { | ||
| text-align: left; | ||
| } | ||
|
|
||
| &.align-center { | ||
| text-align: center; | ||
| } | ||
|
|
||
| &.inline { | ||
| display: inline-block; | ||
| } | ||
|
|
||
| :deep(iframe) { | ||
| width: 100%; | ||
| max-width: 100%; | ||
| margin: 0 auto; | ||
| transition: var(--duration-150) var(--ease-out); | ||
|
|
||
| &:hover { | ||
| border-color: var(--gray-400); | ||
| transition: none; | ||
| } | ||
|
|
||
| &:focus-within { | ||
| border-color: var(--primary); | ||
| outline: none; | ||
| box-shadow: 0px 0px var(--space-1) 0px var(--primary-100); | ||
| } | ||
| } | ||
| } | ||
| </style> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,19 @@ | ||
| import type { User } from '../system/index.js'; | ||
| import type { InternalFormConfig } from './internal-form-config.js'; | ||
|
|
||
| export interface Form { | ||
| /** @primaryKey */ | ||
| id: string; | ||
| sort: number | null; | ||
| user_created: string | User | null; | ||
| date_created: string | null; | ||
| user_updated: string | User | null; | ||
| date_updated: string | null; | ||
| type: string | null; | ||
| title: string | null; | ||
| hubspot_form_id: string | null; | ||
| typeform_form_id: string | null; | ||
| route_to_meeting_link_on_success: boolean | null; | ||
| type?: 'hubspot' | 'typeform' | 'internal' | null; | ||
| title?: string | null; | ||
| hubspot_form_id?: string | null; | ||
| route_to_meeting_link_on_success?: boolean | null; | ||
| typeform_form_id?: string | null; | ||
| internal_form_url?: string | null; | ||
| form_config?: InternalFormConfig | string | null; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { User } from '../system/index.js'; | ||
| import type { Product } from './product.js'; | ||
|
|
||
| export interface InternalFormConfig { | ||
| /** @primaryKey */ | ||
| id: string; | ||
| status?: 'published' | 'draft' | 'archived'; | ||
| sort?: number | null; | ||
| user_created?: User | string | null; | ||
| date_created?: string | null; | ||
| user_updated?: User | string | null; | ||
| date_updated?: string | null; | ||
| Title?: string | null; | ||
| slug?: string | null; | ||
LZylstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| description?: string | null; | ||
| allow_multiple?: boolean | null; | ||
| max_quantity?: number | null; | ||
| } | ||
|
|
||
| export interface InternalFormConfigProduct { | ||
| /** @primaryKey */ | ||
| id: number; | ||
| internal_form_config_id?: InternalFormConfig | string | null; | ||
| products_id?: Product | string | null; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type { File } from '../system/index.js'; | ||
|
|
||
| export interface Product { | ||
| /** @primaryKey */ | ||
| id: string; | ||
| status?: 'active' | 'out_of_stock' | 'backordered' | 'hidden' | 'inactive'; | ||
| /** @description Name of this product */ | ||
| title?: string | null; | ||
| /** @description Unique URL for this product */ | ||
| slug?: string | null; | ||
| description?: string | null; | ||
| /** @description Main thumbnail used for this product */ | ||
| thumbnail?: File | string | null; | ||
| /** @description What colors are available? */ | ||
| color?: 'gray' | null; | ||
| /** @description What sizes are available? */ | ||
| size?: 'gray' | null; | ||
| } | ||
LZylstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message handler lacks proper error handling for malformed URLs. If
block.value.form.internal_form_urlcontains an invalid URL, thenew URL()constructor will throw an exception, potentially causing the component to crash. Wrap the URL construction in a try-catch block to handle invalid URLs gracefully.