Added settings menu with working currency editing

This commit is contained in:
Donkie
2024-01-07 11:11:57 +01:00
parent 9362d04dad
commit 77e781f1ea
7 changed files with 248 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
interface SettingResponseValue {
value: string;
is_set: boolean;
type: string;
}
interface SettingsResponse {
[key: string]: SettingResponseValue;
}
export function useGetSettings() {
const apiEndpoint = import.meta.env.VITE_APIURL;
return useQuery<SettingsResponse>({
queryKey: ["settings"],
queryFn: async () => {
const response = await fetch(`${apiEndpoint}/setting`);
return response.json();
},
});
}
export function useGetSetting(key: string) {
const apiEndpoint = import.meta.env.VITE_APIURL;
return useQuery<SettingResponseValue>({
queryKey: ["settings", key],
queryFn: async () => {
const response = await fetch(`${apiEndpoint}/setting/${key}`);
return response.json();
},
});
}
export function useSetSetting() {
const queryClient = useQueryClient();
const apiEndpoint = import.meta.env.VITE_APIURL;
return useMutation<SettingResponseValue, unknown, { key: string; value: unknown }>({
mutationFn: async ({ key, value }) => {
const response = await fetch(`${apiEndpoint}/setting/${key}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(value),
});
return response.json();
},
onSuccess: (_data, { key }) => {
// Invalidate and refetch
queryClient.invalidateQueries(["settings", key]);
},
});
}