vorteil/direktiv

View on GitHub
ui/src/api/files/mutate/updateFile.ts

Summary

Maintainability
A
3 hrs
Test Coverage
import { UpdateFileSchemaType } from "../schema";
import { fileKeys } from "..";
import { forceLeadingSlash } from "~/api/files/utils";
import { getMessageFromApiError } from "~/api/errorHandling";
import { patchFile } from "./patchFile";
import { useApiKey } from "~/util/store/apiKey";
import useMutationWithPermissions from "~/api/useMutationWithPermissions";
import { useNamespace } from "~/util/store/namespace";
import { useQueryClient } from "@tanstack/react-query";

export const useUpdateFile = ({
  onSuccess,
  onError,
}: {
  onSuccess?: () => void;
  onError?: (e: string | undefined) => void;
} = {}) => {
  const apiKey = useApiKey();
  const namespace = useNamespace();
  const queryClient = useQueryClient();

  if (!namespace) {
    throw new Error("namespace is undefined");
  }

  return useMutationWithPermissions({
    mutationFn: ({
      path,
      payload,
    }: {
      path: string;
      payload: UpdateFileSchemaType;
    }) =>
      patchFile({
        apiKey: apiKey ?? undefined,
        payload,
        urlParams: {
          path,
          namespace,
        },
      }),
    onSuccess(data) {
      queryClient.invalidateQueries({
        queryKey: fileKeys.file(namespace, {
          apiKey: apiKey ?? undefined,
          path: forceLeadingSlash(data.data.path),
        }),
      });
      onSuccess?.();
    },
    onError: (e) => {
      onError?.(getMessageFromApiError(e));
    },
  });
};