All files / src/hooks/cache useUpdateTaskCache.ts

97.29% Statements 36/37
72.72% Branches 8/11
100% Functions 11/11
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                      359x 2486x     359x     359x 259x 100x   41x 41x               387x 363x 363x 2486x 355x 253x       134x       21323x 21323x 36109x 21323x     387x 247x 247x 5331x   247x 247x         220x         247x     140x 140x 3218x 140x   140x               139x            
import { api, PaginatedTaskListRead, TaskRead } from "@/api/apiStore.gen.ts";
import { useProjectId } from "@/hooks/project.ts";
import { useAppDispatch } from "@/store/hooks.ts";
import { RootState, ApiQueries } from "@/store/store";
import { useSelector } from "react-redux";
 
function updateTaskInList(
  task: TaskRead,
  paginatedTasks: PaginatedTaskListRead,
  updateOnly: boolean,
) {
  const tasks = paginatedTasks?.results || ([] as TaskRead[]);
  const taskIndex = tasks.findIndex((t) => t.id === task.id);
 
  // If results is null, initialize it with the current tasks
  Iif (paginatedTasks.results == null) paginatedTasks.results = tasks;
 
  // Update the task in the list if it exists
  if (taskIndex !== -1) {
    tasks[taskIndex] = task;
  } else if (!updateOnly) {
    // If the task does not exist, add it to the list
    tasks.unshift(task);
    paginatedTasks.count += 1;
  }
}
 
function findTaskPageInList(
  taskId: number,
  tasks: [string, ApiQueries[string]][],
): number {
  for (const [cacheKey, value] of tasks) {
    const paginatedTasks = value?.data as PaginatedTaskListRead | undefined;
    if (paginatedTasks) {
      const foundTask = paginatedTasks.results?.find((t) => t.id === taskId);
      if (foundTask) {
        return Number(value?.originalArgs?.page || 1);
      }
    }
  }
  return 1; // If not on any page, add to page 1
}
 
export function useUpdateTaskCache() {
  const dispatch = useAppDispatch();
  const flowsheetId = useProjectId();
  const queries: ApiQueries = useSelector((state: RootState) => state.api.queries);
  return (updatedTask: TaskRead) => {
 
    // Update the standard task list cache if this task is not a child task
    if (updatedTask.parent === null) {
      const endpointName = 'coreTasksList';
      const allResults = Object.entries(queries)
        .filter(([key]) => key.startsWith(endpointName))
 
      const pageNumber = findTaskPageInList(updatedTask.id, allResults);
      dispatch(
        api.util.updateQueryData(
          "coreTasksList",
          { page: pageNumber, flowsheet: flowsheetId },
          (paginatedTasks) => {
            updateTaskInList(updatedTask, paginatedTasks, false);
          },
        ),
      );
 
      return;
    }
 
    const endpointName = 'coreTasksChildrenList';
    const allResults = Object.entries(queries)
      .filter(([key]) => key.startsWith(endpointName))
    const pageNumber = findTaskPageInList(updatedTask.id, allResults);
    // Update the child task list cache
    dispatch(
      api.util.updateQueryData(
        "coreTasksChildrenList",
        {
          id: updatedTask.parent as number,
          page: pageNumber,
        },
        (paginatedTasks) => {
          updateTaskInList(updatedTask, paginatedTasks, true);
        },
      ),
    );
  };
}