-
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.
Extract code duplicates from react queries
- Loading branch information
1 parent
8bf7600
commit 358603f
Showing
79 changed files
with
933 additions
and
1,226 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
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
39 changes: 7 additions & 32 deletions
39
src/Data/Managers/V2/DELETE/DeleteInstance/useDeleteInstance.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 |
---|---|---|
@@ -1,49 +1,24 @@ | ||
import { UseMutationResult, useMutation } from "@tanstack/react-query"; | ||
import { ParsedNumber } from "@/Core"; | ||
import { PrimaryBaseUrlManager } from "@/UI"; | ||
import { useFetchHelpers } from "../../helpers"; | ||
import { useDelete } from "../../helpers/useQueries"; | ||
|
||
/** | ||
* React Query hook for Deleting an instance. | ||
* | ||
* @returns {Mutation} - The mutation object provided by `useMutation` hook. | ||
*/ | ||
export const useDeleteInstance = ( | ||
environment: string, | ||
instance_id: string, | ||
service_entity: string, | ||
version: ParsedNumber, | ||
): UseMutationResult<void, Error, string, unknown> => { | ||
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 instance. | ||
* | ||
* @returns {Promise<void>} - A promise that resolves when the instance is succesfully deleted | ||
*/ | ||
const deleteInstance = async (): Promise<void> => { | ||
const response = await fetch( | ||
baseUrl + | ||
`/lsm/v1/service_inventory/${service_entity}/${instance_id}?current_version=${version}`, | ||
{ | ||
method: "DELETE", | ||
headers: headers, | ||
}, | ||
); | ||
|
||
await handleErrors(response); | ||
}; | ||
): UseMutationResult<void, Error, void, unknown> => { | ||
const deleteFn = useDelete()<void>; | ||
|
||
return useMutation({ | ||
mutationFn: deleteInstance, | ||
mutationFn: () => | ||
deleteFn( | ||
`/lsm/v1/service_inventory/${service_entity}/${instance_id}?current_version=${version}`, | ||
), | ||
mutationKey: ["delete_instance"], | ||
}); | ||
}; |
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
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
Oops, something went wrong.