Skip to content

Commit

Permalink
fix bug with addon deletion (#4657)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen authored May 17, 2024
1 parent d2e3a13 commit bb2b691
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
6 changes: 5 additions & 1 deletion dashboard/src/lib/hooks/useAddon.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -194,6 +194,8 @@ export const useAddon = (): {
isError: boolean;
};
} => {
const queryClient = useQueryClient();

const updateAddon = async ({
projectId,
deploymentTargetId,
Expand Down Expand Up @@ -235,6 +237,8 @@ export const useAddon = (): {
addonName: addon.name.value,
}
);

await queryClient.invalidateQueries(["listAddons"]);
};

const getAddon = ({
Expand Down
22 changes: 2 additions & 20 deletions dashboard/src/main/home/add-on-dashboard/AddonContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -26,7 +25,6 @@ type AddonContextType = {
projectId: number;
deploymentTarget: DeploymentTarget;
status: ClientAddonStatus;
deleteAddon: () => Promise<void>;
};

const AddonContext = createContext<AddonContextType | null>(null);
Expand All @@ -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,
Expand All @@ -64,7 +62,6 @@ export const AddonContextProvider: React.FC<AddonContextProviderProps> = ({
addonName,
refreshIntervalSeconds: 5,
});
const queryClient = useQueryClient();

const status = useAddonStatus({
projectId: currentProject?.id,
Expand All @@ -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 />;
}
Expand All @@ -118,7 +101,6 @@ export const AddonContextProvider: React.FC<AddonContextProviderProps> = ({
projectId: currentProject.id,
deploymentTarget: defaultDeploymentTarget,
status,
deleteAddon: deleteContextAddon,
}}
>
{children}
Expand Down
10 changes: 8 additions & 2 deletions dashboard/src/main/home/add-on-dashboard/common/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
Expand All @@ -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({
Expand Down

0 comments on commit bb2b691

Please sign in to comment.