-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Miawong/sc 67855/refactor dashboard fetch into hooks (#3600)
- Loading branch information
Showing
5 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |