Skip to content

Commit

Permalink
fix: use parameter as filter on task list
Browse files Browse the repository at this point in the history
  • Loading branch information
paolojulian committed Sep 15, 2024
1 parent ba38481 commit 1427dc5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
12 changes: 9 additions & 3 deletions components/home/HomeTaskOverview.tsx
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
15 changes: 11 additions & 4 deletions components/tasks/TaskList/TaskList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaskFilterTypes>('Today');
const router = useRouter();
const { filter } = useLocalSearchParams<{ filter?: string }>();
const selectedFilter = isSupportedTaskFilter(filter) ? filter : 'Today';
const { data, isLoading } = useTasks({ filterType: selectedFilter });
const tasks = data || [];

Expand Down Expand Up @@ -45,6 +48,10 @@ export default function TaskList() {
unCompleteTaskMutation(id);
};

const handleSelectFilter = (filter: TaskFilterTypes): void => {
router.setParams({ defaultFilter: filter });
};

return (
<FlatList
data={tasks}
Expand All @@ -54,7 +61,7 @@ export default function TaskList() {
ListHeaderComponent={
<>
<MainHeader subtitle="Tasks" color={colors.v2.yellow} />
<TaskFilters onSelectFilter={setSelectedFilter} selectedItem={selectedFilter} />
<TaskFilters onSelectFilter={handleSelectFilter} selectedItem={selectedFilter} />
</>
}
ListFooterComponent={<View style={{ paddingBottom: 24 }} />}
Expand Down
11 changes: 11 additions & 0 deletions components/tasks/TaskList/TaskList.utils.ts
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 14 additions & 4 deletions providers/NotificationsProvider/NotificationsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
},
});
}
});

Expand Down

0 comments on commit 1427dc5

Please sign in to comment.