Skip to content

Commit

Permalink
fix load time when uploading images
Browse files Browse the repository at this point in the history
  • Loading branch information
fivaz committed Apr 20, 2024
1 parent 81bb3d1 commit 91dff98
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/lib/components/task-form/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,35 +226,43 @@ 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<AnyTask, 'id'>) {
if (data.goal) {
const goalDocRef = doc(db, 'users', userId, 'goals', data.goal.id);
const goalTaskCollectionRef = collection(goalDocRef, 'tasks');

// const taskSnap = await getDoc(taskRef);
void addDoc(goalTaskCollectionRef, data);
}
}

export async function addTask(data: Omit<AnyTask, 'id'>, 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<AnyTask, 'id'>) {
Expand Down

0 comments on commit 91dff98

Please sign in to comment.