Skip to content

Commit

Permalink
fix: navigate to tasks page after task creation in offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-skumenko committed Aug 16, 2024
1 parent b262f89 commit 02012af
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
48 changes: 45 additions & 3 deletions src/components/create-task/task-form-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,45 @@ import { format } from 'date-fns';
import { Task, TaskFields } from '@/types';
import { DATE_FORMAT } from '@/constants';
import { taskFormSchema } from '@/components/task-form/task-form-schema';
import { createTask } from '@/services/tasks';
import { createTask, getTasksCollection } from '@/services/tasks';
import { useNavigate } from 'react-router-dom';
import { onSnapshot, query } from 'firebase/firestore';
import { useEffect, useRef, useState } from 'react';

export const TaskFormControl = () => {
const isFormSubmitted = useRef(false);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();

useEffect(() => {
const q = query(getTasksCollection());
const unsubscribe = onSnapshot(
q,
(snapshot) => {
setIsLoading(true);
setError(null);

const docChanges = snapshot.docChanges();
const isTaskCreated = docChanges.length && docChanges[0].type === 'added';

if (isTaskCreated && isFormSubmitted.current) {
navigate('/tasks');
}

setIsLoading(false);
},
(error) => {
setError('Failed navigate to tasks page. Please try again later.');
console.error('Firestore error: ', error);

setIsLoading(false);
}
);

return () => unsubscribe();
}, [navigate]);

const form = useForm<TaskFields>({
resolver: zodResolver(taskFormSchema),
defaultValues: {
Expand All @@ -20,19 +53,28 @@ export const TaskFormControl = () => {
});

const handleSubmit = async (values: TaskFields & Pick<Task, 'isCompleted'>) => {
isFormSubmitted.current = true;

const formattedValues = {
...values,
deadline: format(values.deadline, DATE_FORMAT),
isCompleted: false,
};

try {
await createTask(formattedValues);
navigate('/tasks');
createTask(formattedValues);
} catch (error) {
console.error('Error inserting task to db: ', error);
}
};

if (isLoading) {
return <div>Loading task...</div>;
}

if (error) {
return <div>Error: {error}</div>;
}

return <TaskForm form={form} onSubmit={handleSubmit} />;
};
4 changes: 2 additions & 2 deletions src/components/tasks/tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export const Tasks = () => {
const q = query(getTasksCollection());
const unsubscribe = onSnapshot(
q,
(querySnapshot) => {
(snapshot) => {
setIsLoading(true);
setError(null);

try {
const todos = querySnapshot.docs.map((doc) => ({
const todos = snapshot.docs.map((doc) => ({
...(doc.data() as Omit<ITask, 'id'>),
id: doc.id,
}));
Expand Down

0 comments on commit 02012af

Please sign in to comment.