From 1427dc5a88481220ec7ae4b52c167df50bd614bf Mon Sep 17 00:00:00 2001 From: Paolo Vincent Julian Date: Sun, 15 Sep 2024 15:43:40 +0800 Subject: [PATCH] fix: use parameter as filter on task list --- components/home/HomeTaskOverview.tsx | 12 +++++++++--- components/tasks/TaskList/TaskList.tsx | 15 +++++++++++---- components/tasks/TaskList/TaskList.utils.ts | 11 +++++++++++ .../NotificationsProvider.tsx | 18 ++++++++++++++---- 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 components/tasks/TaskList/TaskList.utils.ts diff --git a/components/home/HomeTaskOverview.tsx b/components/home/HomeTaskOverview.tsx index 100d837..f4dc78e 100644 --- a/components/home/HomeTaskOverview.tsx +++ b/components/home/HomeTaskOverview.tsx @@ -1,18 +1,24 @@ import { colors } from '@/constants/Colors'; import useTaskOverview from '@/hooks/services/task/useTaskOverview'; -import { router } from 'expo-router'; +import { TabName } from '@/utils/constants'; +import { useRouter } from 'expo-router'; import React from 'react'; import { TouchableOpacity, View } from 'react-native'; import AppCard from '../common/AppCard'; import Stack from '../common/Stack'; import ThemedText from '../common/ThemedText'; -import { TabName } from '@/utils/constants'; function HomeTaskOverview() { const { data } = useTaskOverview(); + const router = useRouter(); const handlePress = () => { - router.push(TabName.Todo as never); + router.push({ + pathname: TabName.Todo as never, + params: { + filter: 'Today', + }, + }); }; return ( diff --git a/components/tasks/TaskList/TaskList.tsx b/components/tasks/TaskList/TaskList.tsx index d6b67b1..1ae14e0 100644 --- a/components/tasks/TaskList/TaskList.tsx +++ b/components/tasks/TaskList/TaskList.tsx @@ -3,18 +3,21 @@ import MainHeader from '@/components/common/MainHeader'; import ThemedText from '@/components/common/ThemedText'; import TaskFilters from '@/components/tasks/TaskFilters'; import { colors } from '@/constants/Colors'; -import { type TaskFilterTypes } from '@/hooks/services/task/task.types'; import { useCompleteTask } from '@/hooks/services/task/useCompleteTask'; import useTasks from '@/hooks/services/task/useTasks'; import { useUncompleteTask } from '@/hooks/services/task/useUncompleteTask'; import { GlobalSnackbar } from '@/managers/SnackbarManager'; -import { useState } from 'react'; +import { useLocalSearchParams, useRouter } from 'expo-router'; import { FlatList, TouchableOpacity, View } from 'react-native'; import EmptyTaskList from './EmptyTaskList'; import TaskItem, { TaskItemProps } from './TaskItem/TaskItem'; +import { isSupportedTaskFilter } from './TaskList.utils'; +import { TaskFilterTypes } from '@/hooks/services/task/task.types'; export default function TaskList() { - const [selectedFilter, setSelectedFilter] = useState('Today'); + const router = useRouter(); + const { filter } = useLocalSearchParams<{ filter?: string }>(); + const selectedFilter = isSupportedTaskFilter(filter) ? filter : 'Today'; const { data, isLoading } = useTasks({ filterType: selectedFilter }); const tasks = data || []; @@ -45,6 +48,10 @@ export default function TaskList() { unCompleteTaskMutation(id); }; + const handleSelectFilter = (filter: TaskFilterTypes): void => { + router.setParams({ defaultFilter: filter }); + }; + return ( - + } ListFooterComponent={} diff --git a/components/tasks/TaskList/TaskList.utils.ts b/components/tasks/TaskList/TaskList.utils.ts new file mode 100644 index 0000000..67b118d --- /dev/null +++ b/components/tasks/TaskList/TaskList.utils.ts @@ -0,0 +1,11 @@ +import { TaskFilterTypes } from '@/hooks/services/task/task.types'; + +export function isSupportedTaskFilter(type?: string): type is TaskFilterTypes { + if (!type) { + return false; + } + + const types: TaskFilterTypes[] = ['Today', 'Tomorrow', 'All', 'Scheduled', 'Completed']; + + return types.includes(type as TaskFilterTypes); +} diff --git a/providers/NotificationsProvider/NotificationsProvider.tsx b/providers/NotificationsProvider/NotificationsProvider.tsx index 9d0edac..4093c6c 100644 --- a/providers/NotificationsProvider/NotificationsProvider.tsx +++ b/providers/NotificationsProvider/NotificationsProvider.tsx @@ -2,7 +2,7 @@ import { useNotification } from '@/hooks/useNotification'; import { NotificationIdentifier } from '@/hooks/useNotification/useNotification.utils'; import { TabName } from '@/utils/constants'; import * as Notifications from 'expo-notifications'; -import { useNavigation } from 'expo-router'; +import { useRouter } from 'expo-router'; import { ReactNode, createContext, useEffect } from 'react'; const NotificationsContext = createContext(null); @@ -13,16 +13,26 @@ type NotificationsProviderProps = { export default function NotificationsProvider({ children }: NotificationsProviderProps) { const { scheduleNotificationsForTasksToday, scheduleNotificationsForTasksTomorrow } = useNotification(); - const navigation = useNavigation(); + const router = useRouter(); useEffect(() => { // Listener for handling when the notification is clicked const subscription = Notifications.addNotificationResponseReceivedListener(response => { if (response.notification.request.identifier === NotificationIdentifier.TASKS_TODAY) { - navigation.navigate(TabName.Todo as never); + router.push({ + pathname: TabName.Todo as never, + params: { + filter: 'Today', + }, + }); } if (response.notification.request.identifier === NotificationIdentifier.TASKS_TOMORROW) { - navigation.navigate(TabName.Todo as never); + router.push({ + pathname: TabName.Todo as never, + params: { + filter: 'Tomorrow', + }, + }); } });