-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: navigate to tasks page after task updating in offline mode
- Loading branch information
1 parent
02012af
commit e8da273
Showing
5 changed files
with
102 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,67 @@ | ||
import { getTask } from '@/services/tasks'; | ||
import { getTasksCollection } from '@/services/tasks'; | ||
import { TaskFormControl } from './task-form-control'; | ||
import { useEffect, useState } from 'react'; | ||
import { Task as ITask } from '@/types'; | ||
import { onSnapshot, query } from 'firebase/firestore'; | ||
|
||
interface TaskProps { | ||
id: string | null; | ||
} | ||
|
||
export const Task = ({ id }: TaskProps) => { | ||
const [task, setTask] = useState<ITask | null | undefined>(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
// TODO: add loading state | ||
useEffect(() => { | ||
if (id) { | ||
getTask(id) | ||
.then((data) => setTask(data)) | ||
.catch((error) => console.error('Error getting task: ', error)); | ||
} | ||
const q = query(getTasksCollection()); | ||
const unsubscribe = onSnapshot( | ||
q, | ||
(snapshot) => { | ||
setIsLoading(true); | ||
setError(null); | ||
|
||
const doc = snapshot.docs.find((doc) => doc.id === id); | ||
|
||
if (!doc) { | ||
setTask(undefined); | ||
return; | ||
} | ||
|
||
const todo = { | ||
...(doc.data() as Omit<ITask, 'id'>), | ||
id: doc.id, | ||
}; | ||
|
||
setTask(todo); | ||
setIsLoading(false); | ||
}, | ||
(error) => { | ||
setError('Failed to fetch task. Please try again later.'); | ||
console.error('Firestore error: ', error); | ||
|
||
setIsLoading(false); | ||
} | ||
); | ||
|
||
return () => unsubscribe(); | ||
}, [id]); | ||
|
||
if (!id) { | ||
return <div>Task id was not provided</div>; | ||
} | ||
|
||
if (isLoading) { | ||
return <div>Loading tasks...</div>; | ||
} | ||
|
||
if (!task) { | ||
return <div>Task not found</div>; | ||
} | ||
|
||
if (error) { | ||
return <div>Error: {error}</div>; | ||
} | ||
|
||
return <TaskFormControl task={task} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters