|
|
@@ -11,18 +11,29 @@ import {
|
|
|
import api from "shared/api";
|
|
|
import { Context } from "shared/Context";
|
|
|
|
|
|
-type TCreatePaymentMethod = {
|
|
|
- createPaymentMethod: () => Promise<string>;
|
|
|
+type TUsePaymentMethod = {
|
|
|
+ paymentMethodList: PaymentMethodList;
|
|
|
+ refetchPaymentMethods: any;
|
|
|
+ isDeleting: boolean;
|
|
|
+ deletePaymentMethod: (paymentMethodId: string) => Promise<void>;
|
|
|
};
|
|
|
|
|
|
-type TDeletePaymentMethod = {
|
|
|
- deletePaymentMethod: (paymentMethodId: string) => Promise<void>;
|
|
|
- isDeleting: boolean;
|
|
|
+type TCreatePaymentMethod = {
|
|
|
+ createPaymentMethod: () => Promise<string>;
|
|
|
};
|
|
|
|
|
|
-export const usePaymentMethodList = (): PaymentMethodList => {
|
|
|
+export const usePaymentMethods = (): TUsePaymentMethod => {
|
|
|
const { user, currentProject } = useContext(Context);
|
|
|
- const clusterReq = useQuery(
|
|
|
+
|
|
|
+ // State has be shared so that payment methods can be removed
|
|
|
+ // from the Billing page once they are deleted
|
|
|
+ const [paymentMethodList, setPaymentMethodList] = useState<PaymentMethodList>(
|
|
|
+ []
|
|
|
+ );
|
|
|
+ const [isDeleting, setIsDeleting] = useState<boolean>(false);
|
|
|
+
|
|
|
+ // Fetch list of payment methods
|
|
|
+ const paymentMethodReq = useQuery(
|
|
|
["getPaymentMethods", currentProject?.id],
|
|
|
async () => {
|
|
|
if (!currentProject?.id || currentProject.id === -1) {
|
|
|
@@ -38,45 +49,17 @@ export const usePaymentMethodList = (): PaymentMethodList => {
|
|
|
{},
|
|
|
{ project_id: currentProject?.id }
|
|
|
);
|
|
|
- const paymentMethodList = await z
|
|
|
+
|
|
|
+ const data = await z
|
|
|
.array(PaymentMethodValidator)
|
|
|
.parseAsync(listResponse.data);
|
|
|
- return paymentMethodList;
|
|
|
- },
|
|
|
- {
|
|
|
- refetchInterval: 3000,
|
|
|
+ setPaymentMethodList(data);
|
|
|
+
|
|
|
+ return data;
|
|
|
}
|
|
|
);
|
|
|
|
|
|
- return {
|
|
|
- paymentMethods: clusterReq.data ?? [],
|
|
|
- };
|
|
|
-};
|
|
|
-
|
|
|
-export const useCreatePaymentMethod = (): TCreatePaymentMethod => {
|
|
|
- const { currentProject } = useContext(Context);
|
|
|
-
|
|
|
- const createPaymentMethod = async () => {
|
|
|
- const resp = await api.addPaymentMethod(
|
|
|
- "<token>",
|
|
|
- {},
|
|
|
- { project_id: currentProject?.id }
|
|
|
- );
|
|
|
-
|
|
|
- const clientSecret = ClientSecretResponse.parse(resp.data);
|
|
|
-
|
|
|
- return clientSecret;
|
|
|
- };
|
|
|
-
|
|
|
- return {
|
|
|
- createPaymentMethod,
|
|
|
- };
|
|
|
-};
|
|
|
-
|
|
|
-export const useDeletePaymentMethod = (): TDeletePaymentMethod => {
|
|
|
- const { currentProject } = useContext(Context);
|
|
|
- const [isDeleting, setIsDeleting] = useState<boolean>(false);
|
|
|
-
|
|
|
+ // Delete list of payment methods
|
|
|
const deletePaymentMethod = async (paymentMethodId: string) => {
|
|
|
if (!currentProject?.id) {
|
|
|
throw new Error("Project ID is missing");
|
|
|
@@ -95,11 +78,38 @@ export const useDeletePaymentMethod = (): TDeletePaymentMethod => {
|
|
|
throw new Error("Failed to delete payment method");
|
|
|
}
|
|
|
|
|
|
+ setPaymentMethodList(
|
|
|
+ paymentMethodList.filter(
|
|
|
+ (paymentMethod) => paymentMethod.id !== paymentMethodId
|
|
|
+ )
|
|
|
+ );
|
|
|
setIsDeleting(false);
|
|
|
};
|
|
|
|
|
|
return {
|
|
|
- deletePaymentMethod,
|
|
|
+ paymentMethodList,
|
|
|
+ refetchPaymentMethods: paymentMethodReq.refetch,
|
|
|
isDeleting,
|
|
|
+ deletePaymentMethod,
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+export const useCreatePaymentMethod = (): TCreatePaymentMethod => {
|
|
|
+ const { currentProject } = useContext(Context);
|
|
|
+
|
|
|
+ const createPaymentMethod = async () => {
|
|
|
+ const resp = await api.addPaymentMethod(
|
|
|
+ "<token>",
|
|
|
+ {},
|
|
|
+ { project_id: currentProject?.id }
|
|
|
+ );
|
|
|
+
|
|
|
+ const clientSecret = ClientSecretResponse.parse(resp.data);
|
|
|
+
|
|
|
+ return clientSecret;
|
|
|
+ };
|
|
|
+
|
|
|
+ return {
|
|
|
+ createPaymentMethod,
|
|
|
};
|
|
|
};
|