-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Proxies: rest queries and notifications (#1240)
In continuation of work already done to align, feature by feature, here the proxy configuration to : - Have it's REST function to use axios instead of APIConfig and uUse React Query consitenlty - Use Notification` instead for `Alert`.
- Loading branch information
Showing
3 changed files
with
52 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,34 @@ | ||
import { useState } from "react"; | ||
import { AxiosError } from "axios"; | ||
import { getProxies, updateProxy } from "@app/api/rest"; | ||
import { Proxy } from "@app/api/models"; | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
|
||
export interface IProxyFetchState { | ||
proxies: Proxy[]; | ||
isFetching: boolean; | ||
fetchError: AxiosError; | ||
} | ||
|
||
export const ProxiesTasksQueryKey = "proxies"; | ||
|
||
export const useFetchProxies = ( | ||
defaultIsFetching: boolean = false | ||
): IProxyFetchState => { | ||
const { isLoading, data, error } = useQuery( | ||
[ProxiesTasksQueryKey], | ||
async () => (await getProxies()).data, | ||
{ onError: (error) => console.log("error, ", error) } | ||
); | ||
export const useFetchProxies = () => { | ||
const { isLoading, data, error } = useQuery({ | ||
queryKey: [ProxiesTasksQueryKey], | ||
queryFn: getProxies, | ||
onError: (error) => console.log("error, ", error), | ||
}); | ||
return { | ||
proxies: data || [], | ||
isFetching: isLoading, | ||
fetchError: error as AxiosError, | ||
}; | ||
}; | ||
|
||
export const useUpdateProxyMutation = (onSuccess: any) => { | ||
const [putResult, setPutResult] = useState<any>(null); | ||
export const useUpdateProxyMutation = ( | ||
onSuccess: () => void, | ||
onError: (err: AxiosError) => void | ||
) => { | ||
const queryClient = useQueryClient(); | ||
|
||
const { isLoading, mutate, error } = useMutation(updateProxy, { | ||
onSuccess: (res) => { | ||
return useMutation({ | ||
mutationFn: updateProxy, | ||
onSuccess: () => { | ||
onSuccess(); | ||
setPutResult(res); | ||
queryClient.invalidateQueries([ProxiesTasksQueryKey]); | ||
}, | ||
onError: (err) => { | ||
setPutResult(err); | ||
queryClient.invalidateQueries([ProxiesTasksQueryKey]); | ||
}, | ||
onError: onError, | ||
}); | ||
return { | ||
mutate, | ||
putResult, | ||
isLoading, | ||
error, | ||
}; | ||
}; |