mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
fix: proper cancellation handling
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import type {
|
||||
CancelSubscriptionResponse,
|
||||
CreateCheckoutSessionRequest,
|
||||
CreateCheckoutSessionResponse,
|
||||
CreatePortalSessionResponse,
|
||||
GetSubscriptionResponse,
|
||||
} from "@sprint/shared";
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { queryKeys } from "@/lib/query/keys";
|
||||
import { apiClient } from "@/lib/server";
|
||||
|
||||
@@ -42,3 +43,20 @@ export function useCreatePortalSession() {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useCancelSubscription() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation<CancelSubscriptionResponse, Error>({
|
||||
mutationKey: ["subscription", "cancel"],
|
||||
mutationFn: async () => {
|
||||
const { data, error } = await apiClient.subscriptionCancel({ body: {} });
|
||||
if (error) throw new Error(error);
|
||||
if (!data) throw new Error("failed to cancel subscription");
|
||||
return data as CancelSubscriptionResponse;
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: queryKeys.subscription.current() });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user