refactor: 💡 types

This commit is contained in:
amianthus
2025-09-16 13:52:35 +01:00
parent d6e9802cdc
commit d529d6adb7
3 changed files with 70 additions and 69 deletions

View File

@@ -1,21 +1,21 @@
import { useAxiosInstance } from "@/services/useAxiosInstance";
import { Feature } from "@autumn/shared";
import type { Feature } from "@autumn/shared";
import { useQuery } from "@tanstack/react-query";
import { useAxiosInstance } from "@/services/useAxiosInstance";
export const useFeaturesQuery = () => {
const axiosInstance = useAxiosInstance();
const axiosInstance = useAxiosInstance();
const fetchFeatures = async () => {
const { data } = await axiosInstance.get("/products/features");
return data;
};
const fetchFeatures = async () => {
const { data } = await axiosInstance.get("/products/features");
return data;
};
const { data, isLoading, error, refetch } = useQuery<{
features: Feature[];
}>({
queryKey: ["features"],
queryFn: fetchFeatures,
});
const { data, isLoading, error, refetch } = useQuery<{
features: Feature[];
}>({
queryKey: ["features"],
queryFn: fetchFeatures,
});
return { features: data?.features || [], isLoading, error, refetch };
return { features: (data?.features || []) as Feature[], isLoading, error, refetch };
};

View File

@@ -1,46 +1,46 @@
import { useAxiosInstance } from "@/services/useAxiosInstance";
import { FullProduct, ProductCounts, ProductV2 } from "@autumn/shared";
import type { FullProduct, ProductCounts, ProductV2 } from "@autumn/shared";
import { useQuery } from "@tanstack/react-query";
import { useAxiosInstance } from "@/services/useAxiosInstance";
export const useProductsQuery = () => {
const axiosInstance = useAxiosInstance();
const axiosInstance = useAxiosInstance();
const fetchProducts = async () => {
const { data } = await axiosInstance.get("/products/products");
return data;
};
const fetchProducts = async () => {
const { data } = await axiosInstance.get("/products/products");
return data;
};
const fetchProductCounts = async () => {
const { data } = await axiosInstance.get("/products/product_counts");
return data;
};
const fetchProductCounts = async () => {
const { data } = await axiosInstance.get("/products/product_counts");
return data;
};
const { data, isLoading, error, refetch } = useQuery<{
products: ProductV2[];
groupToDefaults: Record<string, Record<string, FullProduct>>;
}>({
queryKey: ["products"],
queryFn: fetchProducts,
});
const { data, isLoading, error, refetch } = useQuery<{
products: ProductV2[];
groupToDefaults: Record<string, Record<string, FullProduct>>;
}>({
queryKey: ["products"],
queryFn: fetchProducts,
});
const { data: countsData, refetch: countsRefetch } = useQuery<
Record<string, ProductCounts>
>({
queryKey: ["product_counts"],
queryFn: fetchProductCounts,
});
const { data: countsData, refetch: countsRefetch } = useQuery<
Record<string, ProductCounts>
>({
queryKey: ["product_counts"],
queryFn: fetchProductCounts,
});
return {
products: data?.products || [],
counts: countsData || {},
groupToDefaults: data?.groupToDefaults || {},
isLoading,
error,
refetch: async () => {
await Promise.all([countsRefetch(), refetch()]);
},
// mutate: async () => {
// await Promise.all([countsRefetch(), refetch()]);
// },
};
return {
products: (data?.products || []) as ProductV2[],
counts: countsData || {},
groupToDefaults: data?.groupToDefaults || {},
isLoading,
error,
refetch: async () => {
await Promise.all([countsRefetch(), refetch()]);
},
// mutate: async () => {
// await Promise.all([countsRefetch(), refetch()]);
// },
};
};

View File

@@ -1,24 +1,25 @@
import { useAxiosInstance } from "@/services/useAxiosInstance";
import type { Reward, RewardProgram } from "@autumn/shared";
import { useQuery } from "@tanstack/react-query";
import { useAxiosInstance } from "@/services/useAxiosInstance";
export const useRewardsQuery = () => {
const axiosInstance = useAxiosInstance();
const axiosInstance = useAxiosInstance();
const fetchRewards = async () => {
const { data } = await axiosInstance.get("/products/rewards");
return data;
};
const fetchRewards = async () => {
const { data } = await axiosInstance.get("/products/rewards");
return data;
};
const { data, isLoading, error, refetch } = useQuery({
queryKey: ["rewards"],
queryFn: fetchRewards,
});
const { data, isLoading, error, refetch } = useQuery({
queryKey: ["rewards"],
queryFn: fetchRewards,
});
return {
rewards: data?.rewards || [],
rewardPrograms: data?.rewardPrograms || [],
isLoading,
error,
refetch,
};
};
return {
rewards: (data?.rewards || []) as Reward[],
rewardPrograms: (data?.rewardPrograms || []) as RewardProgram[],
isLoading,
error,
refetch,
};
};