@tagadapay/plugin-sdk 2.4.12 → 2.4.15
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,97 @@
|
|
|
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
|
+
* Pay for a club offer
|
|
94
|
+
*/
|
|
95
|
+
payOffer: (offerId: string, returnUrl?: string) => Promise<Response>;
|
|
96
|
+
}
|
|
97
|
+
export declare function useClubOffers(options?: UseClubOffersOptions): UseClubOffersResult;
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
const payOffer = useCallback(async (offerId, returnUrl) => {
|
|
59
|
+
const url = returnUrl || (typeof window !== 'undefined' ? window.location.href : '');
|
|
60
|
+
return await apiService.fetch(`/api/v1/offers/${offerId}/pay`, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
returnUrl: url,
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
}, [apiService]);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
void fetchClubOffers();
|
|
69
|
+
}, [fetchClubOffers]);
|
|
70
|
+
return {
|
|
71
|
+
offers,
|
|
72
|
+
isLoading,
|
|
73
|
+
error,
|
|
74
|
+
refetch: fetchClubOffers,
|
|
75
|
+
getOffer,
|
|
76
|
+
getTotalValue,
|
|
77
|
+
getTotalSavings,
|
|
78
|
+
clearError,
|
|
79
|
+
payOffer,
|
|
80
|
+
};
|
|
81
|
+
}
|