Skip to content
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

refactor(image): ♻️ file select event to model update #406

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/extensions/components/image/ImageUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { computed, ref, unref } from 'vue'
import type { ImageForm } from './types'

import { getIcon } from '@/constants/icons'
import Logger from '@/utils/logger'

interface Props {
modelValue?: ImageForm
Expand All @@ -28,21 +29,27 @@ const form = computed({
set: val => emit('update:modelValue', val)
})

async function onFileSelected(event: { isTrusted: boolean }) {
const { file } = unref(form)
if (!file || !event.isTrusted) return
const onFileSelected = async (files: File | File[]) => {
const file = files instanceof File ? files : files[0]
if (!file) {
throw new Error('No files to upload')
}

try {
loading.value = true
const data = await props.upload?.(file)
if (!data) return
loading.value = true
const data = await props.upload?.(file)
if (!data) {
throw new Error('No link received after upload')
}

form.value = {
...unref(form),
src: data
}
form.value = {
...unref(form),
src: data
}
} catch (err) {
Logger.error(`Failed to execute upload file: ${err}`)
} finally {
loading.value = false
loading.value = false
}
}
</script>
Expand All @@ -55,7 +62,7 @@ async function onFileSelected(event: { isTrusted: boolean }) {
accept="image/*"
:loading="loading"
:prepend-icon="getIcon('fileImagePlus')"
@change="onFileSelected"
@update:model-value="onFileSelected"
@click:clear="form.src = undefined"
/>

Expand Down
Loading