@tagadapay/plugin-sdk 2.4.12 → 2.4.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ export interface ClubOfferItem {
2
+ id: string;
3
+ productId: string;
4
+ variantId: string;
5
+ product: {
6
+ name: string;
7
+ description: string;
8
+ };
9
+ variant: {
10
+ name: string;
11
+ description: string;
12
+ imageUrl: string;
13
+ grams: number;
14
+ };
15
+ unitAmount: number;
16
+ quantity: number;
17
+ amount: number;
18
+ adjustedAmount: number;
19
+ }
20
+ export interface ClubOfferSummary {
21
+ currency: string;
22
+ totalAmount: number;
23
+ totalAdjustedAmount: number;
24
+ items: ClubOfferItem[];
25
+ }
26
+ export interface ClubOfferLineItem {
27
+ id: string;
28
+ quantity: number;
29
+ price: {
30
+ variant: {
31
+ id: string;
32
+ name: string;
33
+ description: string | null;
34
+ imageUrl: string;
35
+ grams: number;
36
+ product: {
37
+ id: string;
38
+ name: string;
39
+ description: string;
40
+ };
41
+ };
42
+ };
43
+ }
44
+ export interface ClubOffer {
45
+ id: string;
46
+ titleTrans: {
47
+ en: string;
48
+ };
49
+ summaries: ClubOfferSummary[];
50
+ offerLineItems: ClubOfferLineItem[];
51
+ }
52
+ export interface UseClubOffersOptions {
53
+ /**
54
+ * Whether to fetch club offers automatically on mount
55
+ * @default true
56
+ */
57
+ enabled?: boolean;
58
+ }
59
+ export interface UseClubOffersResult {
60
+ /**
61
+ * Array of fetched club offers
62
+ */
63
+ offers: ClubOffer[];
64
+ /**
65
+ * Loading state
66
+ */
67
+ isLoading: boolean;
68
+ /**
69
+ * Error state
70
+ */
71
+ error: Error | null;
72
+ /**
73
+ * Refetch club offers
74
+ */
75
+ refetch: () => Promise<void>;
76
+ /**
77
+ * Get club offer by ID from the loaded offers
78
+ */
79
+ getOffer: (offerId: string) => ClubOffer | undefined;
80
+ /**
81
+ * Get total value of all club offers
82
+ */
83
+ getTotalValue: () => number;
84
+ /**
85
+ * Get total savings across all club offers
86
+ */
87
+ getTotalSavings: () => number;
88
+ /**
89
+ * Clear error state
90
+ */
91
+ clearError: () => void;
92
+ }
93
+ export declare function useClubOffers(options?: UseClubOffersOptions): UseClubOffersResult;
@@ -0,0 +1,71 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ import { useTagadaContext } from '../providers/TagadaProvider';
3
+ import { usePluginConfig } from './usePluginConfig';
4
+ export function useClubOffers(options = {}) {
5
+ const { apiService } = useTagadaContext();
6
+ const { storeId } = usePluginConfig();
7
+ const [offers, setOffers] = useState([]);
8
+ const [isLoading, setIsLoading] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const { enabled = true } = options;
11
+ const clearError = useCallback(() => {
12
+ setError(null);
13
+ }, []);
14
+ const fetchClubOffers = useCallback(async () => {
15
+ if (!enabled)
16
+ return;
17
+ setIsLoading(true);
18
+ setError(null);
19
+ try {
20
+ if (!storeId) {
21
+ throw new Error('Store ID not found. Make sure the TagadaProvider is properly configured.');
22
+ }
23
+ const responseData = await apiService.fetch(`/api/v1/stores/${storeId}/offers`, {
24
+ method: 'GET',
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ },
28
+ params: {
29
+ type: 'club',
30
+ },
31
+ });
32
+ setOffers(responseData.offers || []);
33
+ }
34
+ catch (err) {
35
+ const error = err instanceof Error ? err : new Error('Failed to fetch club offers');
36
+ setError(error);
37
+ console.error('Error fetching club offers:', error);
38
+ }
39
+ finally {
40
+ setIsLoading(false);
41
+ }
42
+ }, [apiService, storeId, enabled]);
43
+ const getOffer = useCallback((offerId) => {
44
+ return offers.find((offer) => offer.id === offerId);
45
+ }, [offers]);
46
+ const getTotalValue = useCallback(() => {
47
+ return offers.reduce((total, offer) => {
48
+ const firstSummary = offer.summaries[0];
49
+ return total + (firstSummary?.totalAdjustedAmount || 0);
50
+ }, 0);
51
+ }, [offers]);
52
+ const getTotalSavings = useCallback(() => {
53
+ return offers.reduce((total, offer) => {
54
+ const firstSummary = offer.summaries[0];
55
+ return total + (firstSummary?.totalAmount - firstSummary?.totalAdjustedAmount || 0);
56
+ }, 0);
57
+ }, [offers]);
58
+ useEffect(() => {
59
+ void fetchClubOffers();
60
+ }, [fetchClubOffers]);
61
+ return {
62
+ offers,
63
+ isLoading,
64
+ error,
65
+ refetch: fetchClubOffers,
66
+ getOffer,
67
+ getTotalValue,
68
+ getTotalSavings,
69
+ clearError,
70
+ };
71
+ }
@@ -4,6 +4,7 @@
4
4
  export { TagadaProvider } from './providers/TagadaProvider';
5
5
  export { useAuth } from './hooks/useAuth';
6
6
  export { useCheckout } from './hooks/useCheckout';
7
+ export { useClubOffers } from './hooks/useClubOffers';
7
8
  export { useCurrency } from './hooks/useCurrency';
8
9
  export { useCustomer } from './hooks/useCustomer';
9
10
  export { useDiscounts } from './hooks/useDiscounts';
@@ -41,6 +42,7 @@ export type { CheckoutData, CheckoutInitParams, CheckoutLineItem, CheckoutSessio
41
42
  export type { Discount, DiscountCodeValidation, UseDiscountsOptions, UseDiscountsResult } from './hooks/useDiscounts';
42
43
  export type { OrderBumpPreview, UseOrderBumpOptions, UseOrderBumpResult } from './hooks/useOrderBump';
43
44
  export type { UseVipOffersOptions, UseVipOffersResult, VipOffer, VipPreviewResponse } from './hooks/useVipOffers';
45
+ export type { ClubOffer, ClubOfferItem, ClubOfferLineItem, ClubOfferSummary, UseClubOffersOptions, UseClubOffersResult } from './hooks/useClubOffers';
44
46
  export type { PostPurchaseOffer, PostPurchaseOfferItem, PostPurchaseOfferLineItem, PostPurchaseOfferSummary, UsePostPurchasesOptions, UsePostPurchasesResult } from './hooks/usePostPurchases';
45
47
  export type { Payment, PaymentPollingHook, PollingOptions } from './hooks/usePaymentPolling';
46
48
  export type { PaymentInstrument, ThreedsChallenge, ThreedsHook, ThreedsOptions, ThreedsProvider, ThreedsSession } from './hooks/useThreeds';
@@ -7,6 +7,7 @@ export { TagadaProvider } from './providers/TagadaProvider';
7
7
  // Hook exports
8
8
  export { useAuth } from './hooks/useAuth';
9
9
  export { useCheckout } from './hooks/useCheckout';
10
+ export { useClubOffers } from './hooks/useClubOffers';
10
11
  export { useCurrency } from './hooks/useCurrency';
11
12
  export { useCustomer } from './hooks/useCustomer';
12
13
  export { useDiscounts } from './hooks/useDiscounts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.4.12",
3
+ "version": "2.4.14",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",