fixed to use hooks

This commit is contained in:
2026-01-28 20:06:39 +00:00
parent 260d0558ef
commit 99987e35bb
16 changed files with 270 additions and 175 deletions

View File

@@ -4,5 +4,6 @@ export * from "@/lib/query/hooks/issues";
export * from "@/lib/query/hooks/organisations";
export * from "@/lib/query/hooks/projects";
export * from "@/lib/query/hooks/sprints";
export * from "@/lib/query/hooks/subscriptions";
export * from "@/lib/query/hooks/timers";
export * from "@/lib/query/hooks/users";

View File

@@ -0,0 +1,44 @@
import type {
CreateCheckoutSessionRequest,
CreateCheckoutSessionResponse,
CreatePortalSessionResponse,
GetSubscriptionResponse,
} from "@sprint/shared";
import { useMutation, useQuery } from "@tanstack/react-query";
import { queryKeys } from "@/lib/query/keys";
import { apiClient } from "@/lib/server";
export function useSubscription() {
return useQuery<GetSubscriptionResponse>({
queryKey: queryKeys.subscription.current(),
queryFn: async () => {
const { data, error } = await apiClient.subscriptionGet();
if (error) throw new Error(error);
return (data ?? { subscription: null }) as GetSubscriptionResponse;
},
});
}
export function useCreateCheckoutSession() {
return useMutation<CreateCheckoutSessionResponse, Error, CreateCheckoutSessionRequest>({
mutationKey: ["subscription", "checkout"],
mutationFn: async (input) => {
const { data, error } = await apiClient.subscriptionCreateCheckoutSession({ body: input });
if (error) throw new Error(error);
if (!data) throw new Error("failed to create checkout session");
return data as CreateCheckoutSessionResponse;
},
});
}
export function useCreatePortalSession() {
return useMutation<CreatePortalSessionResponse, Error>({
mutationKey: ["subscription", "portal"],
mutationFn: async () => {
const { data, error } = await apiClient.subscriptionCreatePortalSession({ body: {} });
if (error) throw new Error(error);
if (!data) throw new Error("failed to create portal session");
return data as CreatePortalSessionResponse;
},
});
}

View File

@@ -39,4 +39,8 @@ export const queryKeys = {
all: ["users"] as const,
byUsername: (username: string) => [...queryKeys.users.all, "by-username", username] as const,
},
subscription: {
all: ["subscription"] as const,
current: () => [...queryKeys.subscription.all, "current"] as const,
},
};

View File

@@ -1,38 +0,0 @@
import { getServerURL } from "@/lib/utils";
interface CreateCheckoutParams {
billingPeriod: "monthly" | "annual";
csrfToken: string;
onSuccess?: (url: string) => void;
onError?: (error: string) => void;
}
export async function createCheckoutSession({
billingPeriod,
csrfToken,
onSuccess,
onError,
}: CreateCheckoutParams) {
try {
const response = await fetch(`${getServerURL()}/subscription/create-checkout-session`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": csrfToken,
},
credentials: "include",
body: JSON.stringify({ billingPeriod }),
});
const data = await response.json();
if (!response.ok) {
onError?.(data.error || "Failed to create checkout session");
return;
}
onSuccess?.(data.url);
} catch (error) {
onError?.("Network error");
}
}

View File

@@ -1,31 +0,0 @@
import { getServerURL } from "@/lib/utils";
interface CreatePortalParams {
csrfToken: string;
onSuccess?: (url: string) => void;
onError?: (error: string) => void;
}
export async function createPortalSession({ csrfToken, onSuccess, onError }: CreatePortalParams) {
try {
const response = await fetch(`${getServerURL()}/subscription/create-portal-session`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": csrfToken,
},
credentials: "include",
});
const data = await response.json();
if (!response.ok) {
onError?.(data.error || "Failed to create portal session");
return;
}
onSuccess?.(data.url);
} catch (error) {
onError?.("Network error");
}
}

View File

@@ -1,27 +0,0 @@
import type { SubscriptionRecord } from "@sprint/shared";
import { getServerURL } from "@/lib/utils";
interface GetSubscriptionParams {
onSuccess?: (subscription: SubscriptionRecord | null) => void;
onError?: (error: string) => void;
}
export async function getSubscription({ onSuccess, onError }: GetSubscriptionParams) {
try {
const response = await fetch(`${getServerURL()}/subscription/get`, {
method: "GET",
credentials: "include",
});
const data = await response.json();
if (!response.ok) {
onError?.(data.error || "Failed to fetch subscription");
return;
}
onSuccess?.(data.subscription);
} catch (error) {
onError?.("Network error");
}
}