From 9f11fc201bcc984ea6f7fb8139f080405ff2f4b4 Mon Sep 17 00:00:00 2001 From: work Date: Wed, 17 Apr 2024 02:38:15 +0200 Subject: [PATCH] add image to events --- src/lib/components/task-form/TaskForm.svelte | 112 ++++++++++++++++-- src/lib/components/task-form/service.ts | 17 ++- .../task-form/task-form.stories.svelte | 3 +- src/lib/user-utis.ts | 2 +- .../dashboard/profile/profile/Profile.svelte | 4 +- src/routes/register/register/Register.svelte | 4 +- static/manifest.json | 24 ++-- static/task-placeholder-image.webp | Bin 0 -> 2444 bytes 8 files changed, 130 insertions(+), 36 deletions(-) create mode 100644 static/task-placeholder-image.webp diff --git a/src/lib/components/task-form/TaskForm.svelte b/src/lib/components/task-form/TaskForm.svelte index 376d96b4..b43886a8 100644 --- a/src/lib/components/task-form/TaskForm.svelte +++ b/src/lib/components/task-form/TaskForm.svelte @@ -17,15 +17,24 @@ getDuration, getEndTime, removeTask, + storeImage, } from '$lib/components/task-form/service'; import Toggle from '$lib/components/toggle/Toggle.svelte'; import TypedCollection from '$lib/components/typed-collection/TypedCollection.svelte'; import { convertToAnyTask, hasErrors } from '$lib/task/utils'; - import { Transition } from '@rgossiaux/svelte-headlessui'; - import { XMark } from '@steeze-ui/heroicons'; + import { + Disclosure, + DisclosureButton, + DisclosurePanel, + Transition, + } from '@rgossiaux/svelte-headlessui'; + import { ChevronRight, Photo, XMark } from '@steeze-ui/heroicons'; import { Icon } from '@steeze-ui/svelte-icon'; import 'flatpickr/dist/themes/airbnb.css'; import { createEventDispatcher } from 'svelte'; // TODO check later how I should import a precompiled component https://github.com/sveltejs/svelte/issues/604 + import { storage } from '$lib/firebase'; + import { FirebaseError } from 'firebase/app'; + import { getDownloadURL, ref } from 'firebase/storage'; import Flatpickr from 'svelte-flatpickr'; import type { TaskIn } from './service'; @@ -51,26 +60,61 @@ let errorMessage = ''; + let file: File | null = null; + + let image: null | string = null; + $: isEditing = !!task.id; $: formName = `${isEditing ? 'Edit' : 'Add'} ${'startTime' in task ? 'Event' : 'Task'}`; - function onSubmit() { + async function getImage() { + if (!task.id) { + return; + } + + const imageRef = ref(storage, `users/${userId}/tasks/${task.id}`); + try { + const foundImage = await getDownloadURL(imageRef); + if (foundImage) { + image = foundImage; + } + } catch (error) { + if (error instanceof FirebaseError && error.code !== 'storage/object-not-found') { + return console.log('error', error); + } + } + } + + getImage(); + + async function onSubmit() { if (hasErrors(taskIn, errorMessage)) { return; } const { id, ...data } = convertToAnyTask(taskIn); + let eventId = id; if (id) { - editTaskWithPrompt(id, data, userId, targetDate, wasRecurring); + void editTaskWithPrompt(id, data, userId, targetDate, wasRecurring); } else { - addTask(data, userId); + eventId = await addTask(data, userId); + } + if (file) { + void storeImage(userId, eventId, file); } dispatch('close'); } + function handleChange(event: Event & { currentTarget: EventTarget & HTMLInputElement }) { + if (event.currentTarget.files) { + [file] = event.currentTarget.files; + image = URL.createObjectURL(file); + } + } + let goalType: Goal; @@ -115,14 +159,56 @@ -