diff --git a/components/ModalTemplateAddEditGrid.vue b/components/ModalTemplateAddEditGrid.vue index 562da840..353f9ea2 100644 --- a/components/ModalTemplateAddEditGrid.vue +++ b/components/ModalTemplateAddEditGrid.vue @@ -1,5 +1,4 @@ @@ -131,7 +121,6 @@ onMounted(() => { :options="JSON.stringify(gridColumnsOptions)" :label="formatMessage('add_grid_columns_label')" is-full-width - autofocus @on-select="handleChangeColumnNumber" > diff --git a/components/ModalTemplateMoveGridWidget.vue b/components/ModalTemplateMoveGridWidget.vue index bbe8d4a0..d9e7ac38 100644 --- a/components/ModalTemplateMoveGridWidget.vue +++ b/components/ModalTemplateMoveGridWidget.vue @@ -72,7 +72,6 @@ onMounted(() => { :value="JSON.stringify(selectedOptionValue)" :options="JSON.stringify(options)" is-full-width - autofocus @on-select="handleChange" > diff --git a/domains/grid/components/AddWidget.vue b/domains/grid/components/AddWidget.vue index b2c5b1dd..adc096c1 100644 --- a/domains/grid/components/AddWidget.vue +++ b/domains/grid/components/AddWidget.vue @@ -13,15 +13,15 @@ const props = defineProps() const component = shallowRef() const WIDGET_COMPONENTS: Record = { - [GRID_WIDGET_TYPE.TEXT]: 'Text', - [GRID_WIDGET_TYPE.IMAGE]: 'Basic', - [GRID_WIDGET_TYPE.IFRAME]: 'Basic', - [GRID_WIDGET_TYPE.X]: 'GenericPlatform', - [GRID_WIDGET_TYPE.INSTAGRAM]: 'GenericPlatform', - [GRID_WIDGET_TYPE.SPOTIFY]: 'GenericPlatform', - [GRID_WIDGET_TYPE.SOUNDCLOUD]: 'GenericPlatform', - [GRID_WIDGET_TYPE.WARPCAST]: 'Basic', - [GRID_WIDGET_TYPE.YOUTUBE]: 'GenericPlatform', + [GRID_WIDGET_TYPE.enum.TEXT]: 'Text', + [GRID_WIDGET_TYPE.enum.IMAGE]: 'Basic', + [GRID_WIDGET_TYPE.enum.IFRAME]: 'Basic', + [GRID_WIDGET_TYPE.enum.X]: 'Basic', + [GRID_WIDGET_TYPE.enum.INSTAGRAM]: 'Basic', + [GRID_WIDGET_TYPE.enum.SPOTIFY]: 'Basic', + [GRID_WIDGET_TYPE.enum.SOUNDCLOUD]: 'Basic', + [GRID_WIDGET_TYPE.enum.WARPCAST]: 'Basic', + [GRID_WIDGET_TYPE.enum.YOUTUBE]: 'Basic', } const loadComponent = (type?: string): Component | undefined => { diff --git a/domains/grid/components/AddWidgetBasic.vue b/domains/grid/components/AddWidgetBasic.vue index 4832740a..59c15ae6 100644 --- a/domains/grid/components/AddWidgetBasic.vue +++ b/domains/grid/components/AddWidgetBasic.vue @@ -12,68 +12,54 @@ const { formatMessage } = useIntl() const { closeModal, showModal } = useModal() const { addGridWidget, updateGridWidget, getGridById } = useGrid() const { tempGrid, selectedGridId } = storeToRefs(useGridStore()) - -const TEXTAREA_FOCUS_DELAY = 10 // small delay for focusing textarea after element render -const inputValue = ref('') -const inputError = ref('') - -const canSubmit = computed(() => inputValue.value && !inputError.value) +const schema = WIDGET_SCHEMA_MAP[props.type] const isEdit = computed(() => !!props.id) - -const handleSave = () => { +const { + inputValues, + canSubmit, + getFieldErrorMessage, + handleFieldChange, + handleFormErrors, +} = useForm(schema, (await schema?.safeParseAsync(props.properties))?.data) + +const handleSave = async () => { if (!canSubmit.value) { return } - const properties = { - src: inputValue.value, - } - - if (isEdit.value) { - updateGridWidget(props.id, { - properties, - w: props.width, - h: props.height, - }) - } else { - const newWidget: GridWidgetWithoutCords = createWidgetObject({ - type: props.type, - properties, - w: props.width, - h: props.height, - }) - - addGridWidget(newWidget, getGridById(tempGrid.value, selectedGridId.value)) + try { + const properties = await schema?.parseAsync(inputValues.value) + + if (isEdit.value) { + updateGridWidget(props.id, { + properties, + w: props.width, + h: props.height, + }) + } else { + const newWidget: GridWidgetWithoutCords = createWidgetObject({ + type: props.type, + properties, + w: props.width, + h: props.height, + }) + + addGridWidget( + newWidget, + getGridById(tempGrid.value, selectedGridId.value) + ) + } + + handleCancel() + } catch (error: unknown) { + handleFormErrors(error) } - - handleCancel() } const handleCancel = () => { closeModal() } -const handleInput = async (customEvent: CustomEvent) => { - const event = customEvent.detail.event - const input = event.target as HTMLInputElement - inputError.value = '' - - // if no value is entered we just exit here - if (!input.value) { - return - } - - // validation - try { - new URL(input.value) - inputValue.value = encodeURI(input.value) - } catch (error) { - console.warn(error) - inputError.value = formatMessage('errors_invalid_url') - return - } -} - const handleBackToSelection = () => { showModal({ template: 'AddGridWidget', @@ -83,18 +69,6 @@ const handleBackToSelection = () => { }, }) } - -onMounted(() => { - setTimeout(() => { - const textarea = document?.querySelector( - 'lukso-textarea' - ) as unknown as HTMLElement - - textarea?.shadowRoot?.querySelector('textarea')?.focus() - }, TEXTAREA_FOCUS_DELAY) - - inputValue.value = props.properties?.src || '' -})