Skip to content

Commit

Permalink
refactor: generalize query key for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
paolojulian committed Jul 29, 2024
1 parent b376d65 commit 9f60111
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 42 deletions.
1 change: 1 addition & 0 deletions hooks/services/task/task.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Task {
}

export enum TaskQueryKeys {
task = 'task',
list = 'task-list',
overview = 'task-overview',
priority = 'task-priority',
Expand Down
14 changes: 1 addition & 13 deletions hooks/services/task/useCompleteTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,7 @@ export function useCompleteTask() {
},
onSuccess: async response => {
queryClient.invalidateQueries({
predicate: query => {
const firstQueryKey = query.queryKey[0];
if (typeof firstQueryKey !== 'string') {
return false;
}

const invalidateQueries: string[] = [
TaskQueryKeys.list,
TaskQueryKeys.priority,
TaskQueryKeys.overview,
];
return invalidateQueries.includes(firstQueryKey);
},
predicate: query => query.queryKey[0] === TaskQueryKeys.task,
});

return response;
Expand Down
14 changes: 1 addition & 13 deletions hooks/services/task/useCreateTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ export const useCreateTask = () => {
mutationFn: setup,
onSuccess: response => {
queryClient.invalidateQueries({
predicate(query) {
const firstQueryKey = query.queryKey[0];
if (typeof firstQueryKey !== 'string') {
return false;
}

const invalidateQueries: string[] = [
TaskQueryKeys.list,
TaskQueryKeys.overview,
TaskQueryKeys.priority,
];
return invalidateQueries.includes(firstQueryKey);
},
predicate: query => query.queryKey[0] === TaskQueryKeys.task,
});

return response;
Expand Down
2 changes: 1 addition & 1 deletion hooks/services/task/usePriorityTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function usePriorityTasks() {
}

return useQuery({
queryKey: [TaskQueryKeys.priority],
queryKey: [TaskQueryKeys.task, TaskQueryKeys.priority],
queryFn: setup,
});
}
Expand Down
2 changes: 1 addition & 1 deletion hooks/services/task/useTaskOverview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function useTaskOverview() {
};

const { data, isLoading } = useQuery({
queryKey: [TaskQueryKeys.overview],
queryKey: [TaskQueryKeys.task, TaskQueryKeys.overview],
queryFn: setup,
initialData: { totalTasks: 0, dueToday: 0, overdue: 0 },
});
Expand Down
2 changes: 1 addition & 1 deletion hooks/services/task/useTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const useTasks = (filters: TaskQueryFilters) => {
}

return useQuery({
queryKey: [TaskQueryKeys.list, filters.filterType],
queryKey: [TaskQueryKeys.task, TaskQueryKeys.list, filters.filterType],
queryFn: setup,
});
};
Expand Down
14 changes: 1 addition & 13 deletions hooks/services/task/useUncompleteTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,7 @@ export function useUncompleteTask() {
},
onSuccess: async response => {
queryClient.invalidateQueries({
predicate: query => {
const firstQueryKey = query.queryKey[0];
if (typeof firstQueryKey !== 'string') {
return false;
}

const invalidateQueries: string[] = [
TaskQueryKeys.list,
TaskQueryKeys.priority,
TaskQueryKeys.overview,
];
return invalidateQueries.includes(firstQueryKey);
},
predicate: query => query.queryKey[0] === TaskQueryKeys.task,
});

return response;
Expand Down

0 comments on commit 9f60111

Please sign in to comment.