Skip to content

Commit

Permalink
Miawong/sc 67855/refactor dashboard fetch into hooks (#3600)
Browse files Browse the repository at this point in the history
  • Loading branch information
miaawong authored Feb 6, 2023
1 parent 509d57d commit 2f64cb2
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 0 deletions.
58 changes: 58 additions & 0 deletions web/src/features/Dashboard/api/createSnapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useQuery } from "react-query";
import { Utilities } from "../../../utilities/utilities";
import { useSelectedApp } from "@features/App";

interface SnapshotResponse {
success: boolean;
error: string;
kotsadmNamespace: string;
kotsadmRequiresVeleroAccess: boolean;
}
interface Snapshot {
startingSnapshot: boolean;
}

export const createSnapshot = async (
option: string,
appSlug: string
): Promise<SnapshotResponse> => {
let url =
option === "full"
? `${process.env.API_ENDPOINT}/snapshot/backup`
: `${process.env.API_ENDPOINT}/app/${appSlug}/snapshot/backup`;

const res = await fetch(url, {
method: "POST",
headers: {
Authorization: Utilities.getToken(),
"Content-Type": "application/json",
},
});

const response = await res.json();
if (!res.ok && res.status !== 200) {
throw new Error(response.error);
}

return response;
};

const createSnapshotResponse = (response: SnapshotResponse): Snapshot => {
return {
startingSnapshot: response.kotsadmRequiresVeleroAccess ? false : true,
};
};

export const useCreateSnapshot = (option: "full" | "partial") => {
const { selectedApp } = useSelectedApp();
return useQuery({
queryFn: () => createSnapshot(option, selectedApp?.slug || ""),
queryKey: ["createSnapshot"],
select: (response: SnapshotResponse) => {
createSnapshotResponse(response);
},
enabled: false,
});
};

export default { useCreateSnapshot };
37 changes: 37 additions & 0 deletions web/src/features/Dashboard/api/getAirgapConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useQuery } from "react-query";
import { Utilities } from "../../../utilities/utilities";
import { useSelectedApp } from "@features/App";

export const getAirgapConfig = async (appSlug: string): Promise<number> => {
const configUrl = `${process.env.API_ENDPOINT}/app/${appSlug}/airgap/config`;

let simultaneousUploads = 3;

let res = await fetch(configUrl, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: Utilities.getToken(),
},
});
const response = await res.json();
if (res.ok) {
simultaneousUploads = response.simultaneousUploads;

return simultaneousUploads;
} else {
throw new Error(response.error);
}
};

export const useAirgapConfig = () => {
const { selectedApp } = useSelectedApp();
return useQuery({
queryFn: () => getAirgapConfig(selectedApp?.slug || ""),
queryKey: ["getAirgapConfig"],
onError: (err: Error) => console.log(err),
enabled: false,
});
};

export default { useAirgapConfig };
41 changes: 41 additions & 0 deletions web/src/features/Dashboard/api/getAppDownstream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { isAwaitingResults } from "@src/utilities/utilities";
import { useQuery } from "react-query";
import { Utilities } from "../../../utilities/utilities";
import { useSelectedApp } from "@features/App";
import { Downstream } from "@types";

export const getAppDownstream = async (
appSlug: string
): Promise<Downstream | null> => {
const res = await fetch(`${process.env.API_ENDPOINT}/app/${appSlug}`, {
headers: {
Authorization: Utilities.getToken(),
"Content-Type": "application/json",
},
method: "GET",
});

if (!res.ok && res.status !== 200) {
throw new Error(
`an error occurred while fetching downstream for ${appSlug}`
);
}

const appResponse = await res.json();
return appResponse.downstream;
};

export const useAppDownstream = () => {
const { selectedApp } = useSelectedApp();
return useQuery({
queryFn: () => getAppDownstream(selectedApp?.slug || ""),
queryKey: ["getAppDownstream"],
onError: (err: Error) => console.log(err),
refetchInterval: (downstream) =>
downstream && !isAwaitingResults(downstream?.pendingVersions)
? false
: 2000,
});
};

export default { useAppDownstream };
60 changes: 60 additions & 0 deletions web/src/features/Dashboard/api/getUpdateDownloadStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useQuery } from "react-query";
import { Utilities } from "../../../utilities/utilities";
import { useSelectedApp } from "@features/App";

interface UpdateStatusResponse {
currentMessage: string;
status: string;
}
interface UpdateStatus {
checkingForUpdateError: boolean;
checkingForUpdates: boolean;
checkingUpdateMessage: string;
status: string;
}

const getUpdateDownloadStatus = async (
appSlug: string
): Promise<UpdateStatusResponse> => {
const res = await fetch(
`${process.env.API_ENDPOINT}/app/${appSlug}/task/updatedownload`,
{
headers: {
Authorization: Utilities.getToken(),
"Content-Type": "application/json",
},
method: "GET",
}
);

if (!res.ok) {
throw new Error("Error getting update status");
}
const appResponse = await res.json();
return appResponse;
};

const makeUpdateStatusResponse = (
response: UpdateStatusResponse
): UpdateStatus => {
return {
checkingForUpdateError: response.status === "failed",
checkingForUpdates: response.status !== "running",
checkingUpdateMessage: response.currentMessage,
status: response.status,
};
};

export const useUpdateDownloadStatus = () => {
const { selectedApp } = useSelectedApp();

return useQuery({
queryFn: () => getUpdateDownloadStatus(selectedApp?.slug || ""),
queryKey: ["getUpdateStatus"],
onError: (err: Error) => console.log(err),
refetchInterval: (data) => (data?.status !== "running" ? false : 1000),
select: makeUpdateStatusResponse,
});
};

export default { useUpdateDownloadStatus };
62 changes: 62 additions & 0 deletions web/src/features/Dashboard/api/getUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useQuery } from "react-query";
import { Utilities } from "../../../utilities/utilities";
import { useSelectedApp } from "@features/App";

interface UpdateResponse {
availableUpdates: number;
currentAppSequence: number;
currentRelease: { sequence: number; version: string };
availableReleases: { sequence: number; version: string };
}
interface Updates {
checkingForUpdates: boolean;
checkingForUpdatesError?: boolean;
checkingUpdateMessage?: string;
noUpdatesAvailable: boolean;
}

// bad name, will fix later
export const getCheckForUpdates = async (
appSlug: string
): Promise<UpdateResponse> => {
let res = await fetch(
`${process.env.API_ENDPOINT}/app/${appSlug}/updatecheck`,
{
headers: {
Authorization: Utilities.getToken(),
"Content-Type": "application/json",
},
method: "POST",
}
);

const response = await res.json();
// on the dashboard page it triggers getAppLicense here
if (res.ok) {
return response;
} else {
throw new Error(response.error);
}
};

const makeUpdatesResponse = (response: UpdateResponse): Updates => {
return {
checkingForUpdates: response.availableUpdates === 0 ? false : true,
noUpdatesAvailable: response.availableUpdates === 0 ? true : false,
};
// sets timeout to 3 seconds and set noUpdatesAvailable to false
};

// update name later
export const useCheckForUpdates = () => {
const { selectedApp } = useSelectedApp();
return useQuery({
queryFn: () => getCheckForUpdates(selectedApp?.slug || ""),
queryKey: ["getCheckForUpdates"],
onError: (err: Error) => console.log(err),
enabled: true,
select: (data) => makeUpdatesResponse(data),
});
};

export default { useCheckForUpdates };

0 comments on commit 2f64cb2

Please sign in to comment.