All files / src/hooks/cache useUpdateTaskCache.ts

96.87% Statements 31/32
50% Branches 3/6
100% Functions 7/7
100% Lines 28/28

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 90 91 92 93 94 95 96 97 98 99                                333x 1529x     333x       258x     45x 45x                 323x   1518x   243x       205x       33246x 33246x 33246x 53489x     528x       308x 308x 5886x     308x 308x         263x         308x     140x 140x 2940x   140x   140x               70x            
import { useSelector } from "react-redux";
import {
  api,
  PaginatedTaskListRead,
  TaskRead,
  TaskTypeEnum,
} from "@/api/apiStore.gen.ts";
import { useProjectId } from "@/hooks/project.ts";
import { useAppDispatch } from "@/store/hooks.ts";
import { ApiQueries, RootState } from "@/store/store";
 
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 [, 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) => {
    if (updatedTask.task_type === TaskTypeEnum.BuildState) return;
 
    // 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);
        },
      ),
    );
  };
}