mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-11-04 17:35:15 +00:00
Proxy host modal basis, other improvements
This commit is contained in:
@@ -7,6 +7,7 @@ export * from "./useDeadHosts";
|
||||
export * from "./useDnsProviders";
|
||||
export * from "./useHealth";
|
||||
export * from "./useHostReport";
|
||||
export * from "./useProxyHost";
|
||||
export * from "./useProxyHosts";
|
||||
export * from "./useRedirectionHost";
|
||||
export * from "./useRedirectionHosts";
|
||||
|
||||
65
frontend/src/hooks/useProxyHost.ts
Normal file
65
frontend/src/hooks/useProxyHost.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { createProxyHost, getProxyHost, type ProxyHost, updateProxyHost } from "src/api/backend";
|
||||
|
||||
const fetchProxyHost = (id: number | "new") => {
|
||||
if (id === "new") {
|
||||
return Promise.resolve({
|
||||
id: 0,
|
||||
createdOn: "",
|
||||
modifiedOn: "",
|
||||
ownerUserId: 0,
|
||||
domainNames: [],
|
||||
forwardHost: "",
|
||||
forwardPort: 0,
|
||||
accessListId: 0,
|
||||
certificateId: 0,
|
||||
sslForced: false,
|
||||
cachingEnabled: false,
|
||||
blockExploits: false,
|
||||
advancedConfig: "",
|
||||
meta: {},
|
||||
allowWebsocketUpgrade: false,
|
||||
http2Support: false,
|
||||
forwardScheme: "",
|
||||
enabled: true,
|
||||
hstsEnabled: false,
|
||||
hstsSubdomains: false,
|
||||
} as ProxyHost);
|
||||
}
|
||||
return getProxyHost(id, ["owner"]);
|
||||
};
|
||||
|
||||
const useProxyHost = (id: number | "new", options = {}) => {
|
||||
return useQuery<ProxyHost, Error>({
|
||||
queryKey: ["proxy-host", id],
|
||||
queryFn: () => fetchProxyHost(id),
|
||||
staleTime: 60 * 1000, // 1 minute
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
const useSetProxyHost = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (values: ProxyHost) => (values.id ? updateProxyHost(values) : createProxyHost(values)),
|
||||
onMutate: (values: ProxyHost) => {
|
||||
if (!values.id) {
|
||||
return;
|
||||
}
|
||||
const previousObject = queryClient.getQueryData(["proxy-host", values.id]);
|
||||
queryClient.setQueryData(["proxy-host", values.id], (old: ProxyHost) => ({
|
||||
...old,
|
||||
...values,
|
||||
}));
|
||||
return () => queryClient.setQueryData(["proxy-host", values.id], previousObject);
|
||||
},
|
||||
onError: (_, __, rollback: any) => rollback(),
|
||||
onSuccess: async ({ id }: ProxyHost) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["proxy-host", id] });
|
||||
queryClient.invalidateQueries({ queryKey: ["proxy-hosts"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["audit-logs"] });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export { useProxyHost, useSetProxyHost };
|
||||
Reference in New Issue
Block a user