소스 검색

fix bug with addon deletion (#4657)

Feroze Mohideen 2 년 전
부모
커밋
bb2b691f4a

+ 5 - 1
dashboard/src/lib/hooks/useAddon.ts

@@ -1,6 +1,6 @@
 import { useEffect, useRef, useState } from "react";
 import { Addon, AddonWithEnvVars } from "@porter-dev/api-contracts";
-import { useQuery } from "@tanstack/react-query";
+import { useQuery, useQueryClient } from "@tanstack/react-query";
 import Anser, { type AnserJsonEntry } from "anser";
 import { match } from "ts-pattern";
 import { z } from "zod";
@@ -194,6 +194,8 @@ export const useAddon = (): {
     isError: boolean;
   };
 } => {
+  const queryClient = useQueryClient();
+
   const updateAddon = async ({
     projectId,
     deploymentTargetId,
@@ -235,6 +237,8 @@ export const useAddon = (): {
         addonName: addon.name.value,
       }
     );
+
+    await queryClient.invalidateQueries(["listAddons"]);
   };
 
   const getAddon = ({

+ 2 - 20
dashboard/src/main/home/add-on-dashboard/AddonContextProvider.tsx

@@ -1,5 +1,4 @@
-import React, { createContext, useCallback, useContext } from "react";
-import { useQueryClient } from "@tanstack/react-query";
+import React, { createContext, useContext } from "react";
 import styled from "styled-components";
 
 import Loading from "components/Loading";
@@ -26,7 +25,6 @@ type AddonContextType = {
   projectId: number;
   deploymentTarget: DeploymentTarget;
   status: ClientAddonStatus;
-  deleteAddon: () => Promise<void>;
 };
 
 const AddonContext = createContext<AddonContextType | null>(null);
@@ -53,7 +51,7 @@ export const AddonContextProvider: React.FC<AddonContextProviderProps> = ({
   const { currentProject } = useContext(Context);
   const { defaultDeploymentTarget, isDefaultDeploymentTargetLoading } =
     useDefaultDeploymentTarget();
-  const { getAddon, deleteAddon } = useAddon();
+  const { getAddon } = useAddon();
   const {
     addon,
     isLoading: isAddonLoading,
@@ -64,7 +62,6 @@ export const AddonContextProvider: React.FC<AddonContextProviderProps> = ({
     addonName,
     refreshIntervalSeconds: 5,
   });
-  const queryClient = useQueryClient();
 
   const status = useAddonStatus({
     projectId: currentProject?.id,
@@ -78,20 +75,6 @@ export const AddonContextProvider: React.FC<AddonContextProviderProps> = ({
     !!currentProject &&
     currentProject.id !== -1;
 
-  const deleteContextAddon = useCallback(async () => {
-    if (!paramsExist || !addon) {
-      return;
-    }
-
-    await deleteAddon({
-      projectId: currentProject.id,
-      deploymentTargetId: defaultDeploymentTarget.id,
-      addon,
-    });
-
-    await queryClient.invalidateQueries(["listAddons"]);
-  }, [paramsExist]);
-
   if (isDefaultDeploymentTargetLoading || isAddonLoading || !paramsExist) {
     return <Loading />;
   }
@@ -118,7 +101,6 @@ export const AddonContextProvider: React.FC<AddonContextProviderProps> = ({
         projectId: currentProject.id,
         deploymentTarget: defaultDeploymentTarget,
         status,
-        deleteAddon: deleteContextAddon,
       }}
     >
       {children}

+ 8 - 2
dashboard/src/main/home/add-on-dashboard/common/Settings.tsx

@@ -8,6 +8,7 @@ import Icon from "components/porter/Icon";
 import Spacer from "components/porter/Spacer";
 import Text from "components/porter/Text";
 import { Code } from "main/home/managed-addons/tabs/shared";
+import { useAddon } from "lib/hooks/useAddon";
 import { getErrorMessageFromNetworkCall } from "lib/hooks/useCluster";
 import { useIntercom } from "lib/hooks/useIntercom";
 
@@ -18,7 +19,8 @@ import { useAddonContext } from "../AddonContextProvider";
 import { useAddonFormContext } from "../AddonFormContextProvider";
 
 const Settings: React.FC = () => {
-  const { addon, deleteAddon } = useAddonContext();
+  const { deleteAddon } = useAddon();
+  const { addon, projectId, deploymentTarget } = useAddonContext();
   const { updateAddonButtonProps } = useAddonFormContext();
   const history = useHistory();
   const { setCurrentOverlay = () => ({}) } = useContext(Context);
@@ -30,7 +32,11 @@ const Settings: React.FC = () => {
     try {
       setCurrentOverlay(null);
       setIsDeleting(true);
-      await deleteAddon();
+      await deleteAddon({
+        projectId,
+        deploymentTargetId: deploymentTarget.id,
+        addon,
+      });
       history.push("/addons");
     } catch (err) {
       showIntercomWithMessage({