From 91dff988f9daf1aac9d1a4e0f43ba1b80efceb6a Mon Sep 17 00:00:00 2001 From: work Date: Sat, 20 Apr 2024 23:45:24 +0200 Subject: [PATCH] fix load time when uploading images --- src/lib/components/task-form/service.ts | 28 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/lib/components/task-form/service.ts b/src/lib/components/task-form/service.ts index a5aa7d7a..bf350509 100644 --- a/src/lib/components/task-form/service.ts +++ b/src/lib/components/task-form/service.ts @@ -226,13 +226,18 @@ export async function editTask( userId: string, file?: File | null, ) { - if (file) { - data.image = await storeImage(userId, id, file); - } - const taskDocRef = doc(db, 'users', userId, 'tasks', id); void setDoc(taskDocRef, data); - void editTaskInGoal(userId, data, taskDocRef); + + if (file) { + const image = await storeImage(userId, id, file); + + void updateDoc(taskDocRef, { image }); + + void editTaskInGoal(userId, { ...data, image }, taskDocRef); + } else { + void editTaskInGoal(userId, data, taskDocRef); + } } async function addTaskToGoal(userId: string, data: Omit) { @@ -240,7 +245,6 @@ async function addTaskToGoal(userId: string, data: Omit) { const goalDocRef = doc(db, 'users', userId, 'goals', data.goal.id); const goalTaskCollectionRef = collection(goalDocRef, 'tasks'); - // const taskSnap = await getDoc(taskRef); void addDoc(goalTaskCollectionRef, data); } } @@ -248,13 +252,17 @@ async function addTaskToGoal(userId: string, data: Omit) { export async function addTask(data: Omit, userId: string, file?: File | null) { const newTaskRef = doc(collection(db, 'users', userId, 'tasks')); + void setDoc(newTaskRef, data); + if (file) { - data.image = await storeImage(userId, newTaskRef.id, file); - } + const image = await storeImage(userId, newTaskRef.id, file); - void setDoc(newTaskRef, data); + await updateDoc(newTaskRef, { image }); - void addTaskToGoal(userId, data); + void addTaskToGoal(userId, { ...data, image }); + } else { + void addTaskToGoal(userId, data); + } } async function deleteTaskFromGoal(userId: string, taskId: string, data: Omit) {