-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace v1 queries related to services for React-Query
- Loading branch information
1 parent
b9d2139
commit d7af9ab
Showing
25 changed files
with
598 additions
and
438 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 @@ | ||
export * from "./useDeleteService"; |
56 changes: 56 additions & 0 deletions
56
src/Data/Managers/V2/DELETE/DeleteService/useDeleteService.ts
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,56 @@ | ||
import { | ||
UseMutationResult, | ||
useMutation, | ||
useQueryClient, | ||
} from "@tanstack/react-query"; | ||
import { PrimaryBaseUrlManager } from "@/UI"; | ||
import { useFetchHelpers } from "../../helpers"; | ||
|
||
/** | ||
* React Query hook for Deleting an Service. | ||
* | ||
* @returns {Mutation} - The mutation object provided by `useMutation` hook. | ||
*/ | ||
export const useDeleteService = ( | ||
environment: string, | ||
service_entity: string, | ||
): UseMutationResult<void, Error, void, unknown> => { | ||
const client = useQueryClient(); | ||
const { createHeaders, handleErrors } = useFetchHelpers(); | ||
const headers = createHeaders(environment); | ||
|
||
const baseUrlManager = new PrimaryBaseUrlManager( | ||
globalThis.location.origin, | ||
globalThis.location.pathname, | ||
); | ||
|
||
const baseUrl = baseUrlManager.getBaseUrl(process.env.API_BASEURL); | ||
|
||
/** | ||
* Delete an Service. | ||
* | ||
* @returns {Promise<void>} - A promise that resolves when the Service is successfully deleted | ||
*/ | ||
const deleteService = async (): Promise<void> => { | ||
const response = await fetch( | ||
baseUrl + `/lsm/v1/service_catalog/${service_entity}`, | ||
{ | ||
method: "DELETE", | ||
headers: headers, | ||
}, | ||
); | ||
|
||
await handleErrors(response); | ||
}; | ||
|
||
return useMutation({ | ||
mutationFn: deleteService, | ||
mutationKey: ["delete_service"], | ||
onSuccess: () => { | ||
client.refetchQueries({ queryKey: ["get_service_models-continuous"] }); | ||
client.refetchQueries({ queryKey: ["get_service_models-one_time"] }); | ||
client.refetchQueries({ queryKey: ["get_service_model-one_time"] }); | ||
client.refetchQueries({ queryKey: ["get_service_model-continuous"] }); | ||
}, | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export * from "./useGetServiceConfig"; |
61 changes: 61 additions & 0 deletions
61
src/Data/Managers/V2/GETTERS/GetServiceConfig/useGetServiceConfig.ts
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,61 @@ | ||
import { UseQueryResult, useQuery } from "@tanstack/react-query"; | ||
import { Config } from "@/Core"; | ||
import { PrimaryBaseUrlManager } from "@/UI"; | ||
import { useFetchHelpers } from "../../helpers"; | ||
|
||
/** | ||
* Return Signature of the useGetServiceConfig React Query | ||
*/ | ||
interface GetServiceConfig { | ||
useOneTime: () => UseQueryResult<Config, Error>; | ||
} | ||
|
||
/** | ||
* React Query hook to fetch the service config | ||
* | ||
* @param service {string} - the service entity | ||
* @param environment {string} - the environment in which the instance belongs | ||
* | ||
* @returns {GetServiceConfig} An object containing the different available queries. | ||
* @returns {UseQueryResult<Config, Error>} returns.useOneTime - Fetch the service model with a single query. | ||
* @returns {UseQueryResult<Config, Error>} returns.useContinuous - Fetch the service model with a recursive query with an interval of 5s. | ||
*/ | ||
export const useGetServiceConfig = ( | ||
service: string, | ||
environment: string, | ||
): GetServiceConfig => { | ||
const { createHeaders, handleErrors } = useFetchHelpers(); | ||
const headers = createHeaders(environment); | ||
|
||
const baseUrlManager = new PrimaryBaseUrlManager( | ||
globalThis.location.origin, | ||
globalThis.location.pathname, | ||
); | ||
const baseUrl = baseUrlManager.getBaseUrl(process.env.API_BASEURL); | ||
|
||
const fetchInstance = async (): Promise<{ data: Config }> => { | ||
const response = await fetch( | ||
`${baseUrl}/lsm/v1/service_catalog/${service}/config`, | ||
{ | ||
headers, | ||
}, | ||
); | ||
|
||
await handleErrors( | ||
response, | ||
`Failed to fetch Service Config for: ${service}`, | ||
); | ||
|
||
return response.json(); | ||
}; | ||
|
||
return { | ||
useOneTime: (): UseQueryResult<Config, Error> => | ||
useQuery({ | ||
queryKey: ["get_service_config-one_time", service], | ||
queryFn: fetchInstance, | ||
retry: false, | ||
select: (data) => data.data, | ||
}), | ||
}; | ||
}; |
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./UseGetServiceModel"; | ||
export * from "./useGetServiceModel"; |
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 @@ | ||
export * from "./useGetServiceModels"; |
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
Empty file.
53 changes: 53 additions & 0 deletions
53
src/Data/Managers/V2/POST/UpdateCatalog/useUpdateCatalog.ts
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,53 @@ | ||
import { | ||
UseMutationResult, | ||
useMutation, | ||
useQueryClient, | ||
} from "@tanstack/react-query"; | ||
import { PrimaryBaseUrlManager } from "@/UI"; | ||
import { useFetchHelpers } from "../../helpers"; | ||
|
||
/** | ||
* React Query hook for updating environment catalog. | ||
* | ||
* @param {string} environment - The environment to use for creating headers. | ||
* @returns {UseMutationResult<void, Error, void, unknown>}- The mutation object from `useMutation` hook. | ||
*/ | ||
export const useUpdateCatalog = ( | ||
environment: string, | ||
): UseMutationResult<void, Error, void, unknown> => { | ||
const client = useQueryClient(); | ||
const baseUrlManager = new PrimaryBaseUrlManager( | ||
globalThis.location.origin, | ||
globalThis.location.pathname, | ||
); | ||
const { createHeaders, handleErrors } = useFetchHelpers(); | ||
const headers = createHeaders(environment); | ||
const baseUrl = baseUrlManager.getBaseUrl(process.env.API_BASEURL); | ||
|
||
/** | ||
* Update the environment catalog. | ||
* | ||
* @returns {Promise<void>} - The promise object of the fetch request. | ||
* @throws {Error} If the response is not successful, an error with the error message is thrown. | ||
*/ | ||
const updateCatalog = async (): Promise<void> => { | ||
const response = await fetch( | ||
baseUrl + `/lsm/v1/exporter/export_service_definition`, | ||
{ | ||
method: "POST", | ||
headers, | ||
}, | ||
); | ||
|
||
await handleErrors(response); | ||
}; | ||
|
||
return useMutation({ | ||
mutationFn: updateCatalog, | ||
mutationKey: ["update_catalog"], | ||
onSuccess: () => { | ||
client.invalidateQueries({ queryKey: ["get_service_models-one_time"] }); | ||
client.invalidateQueries({ queryKey: ["get_service_models-continuous"] }); | ||
}, | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,31 +1,44 @@ | ||
import React, { useContext } from "react"; | ||
import { PageContainer, RemoteDataView } from "@/UI/Components"; | ||
import { useGetServiceModel } from "@/Data/Managers/V2/GETTERS/GetServiceModel"; | ||
import { ErrorView, LoadingView, PageContainer } from "@/UI/Components"; | ||
import { DependencyContext } from "@/UI/Dependency"; | ||
import { useRouteParams } from "@/UI/Routing"; | ||
import { words } from "@/UI/words"; | ||
import { CreateInstance } from "./CreateInstance"; | ||
|
||
export const Page: React.FC = () => { | ||
const { service: serviceName } = useRouteParams<"CreateInstance">(); | ||
const { queryResolver } = useContext(DependencyContext); | ||
const { environmentHandler } = useContext(DependencyContext); | ||
const env = environmentHandler.useId(); | ||
|
||
const [data, retry] = queryResolver.useContinuous<"GetService">({ | ||
kind: "GetService", | ||
name: serviceName, | ||
}); | ||
const { data, isError, error, isSuccess, refetch } = useGetServiceModel( | ||
serviceName, | ||
env, | ||
).useContinuous(); | ||
|
||
return ( | ||
if (isError) { | ||
<PageContainer pageTitle={words("inventory.createInstance.title")}> | ||
<RemoteDataView | ||
data={data} | ||
retry={retry} | ||
label="AddInstance" | ||
SuccessView={(service) => ( | ||
<div aria-label="AddInstance-Success"> | ||
<CreateInstance serviceEntity={service} /> | ||
</div> | ||
)} | ||
<ErrorView | ||
message={error.message} | ||
retry={refetch} | ||
ariaLabel="ServicesProvider-Failed" | ||
/> | ||
</PageContainer>; | ||
} | ||
|
||
if (isSuccess) { | ||
return ( | ||
<PageContainer pageTitle={words("inventory.createInstance.title")}> | ||
<div aria-label="AddInstance-Success"> | ||
<CreateInstance serviceEntity={data} /> | ||
</div> | ||
</PageContainer> | ||
); | ||
} | ||
|
||
return ( | ||
<PageContainer pageTitle={words("inventory.createInstance.title")}> | ||
<LoadingView ariaLabel="ServicesProvider-Loading" /> | ||
</PageContainer> | ||
); | ||
}; |
Oops, something went wrong.