@tagadapay/plugin-sdk 2.4.28 → 2.4.30

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,18 @@
1
+ export interface UseCustomerInfosOptions {
2
+ customerId?: string | null;
3
+ enabled?: boolean;
4
+ }
5
+ export interface UseCustomerInfosResult<TData = {
6
+ customer: any;
7
+ }> {
8
+ data: TData | null;
9
+ isLoading: boolean;
10
+ error: Error | null;
11
+ refetch: () => Promise<void>;
12
+ }
13
+ /**
14
+ * useCustomerInfos - Fetches customer infos from `/api/v1/customers/{customerId}` with `storeId` param
15
+ */
16
+ export declare function useCustomerInfos<TData = {
17
+ customer: any;
18
+ }>(options: UseCustomerInfosOptions): UseCustomerInfosResult<TData>;
@@ -0,0 +1,54 @@
1
+ 'use client';
2
+ import { useCallback, useEffect, useMemo, useState } from 'react';
3
+ import { useTagadaContext } from '../providers/TagadaProvider';
4
+ import { usePluginConfig } from './usePluginConfig';
5
+ /**
6
+ * useCustomerInfos - Fetches customer infos from `/api/v1/customers/{customerId}` with `storeId` param
7
+ */
8
+ export function useCustomerInfos(options) {
9
+ const { apiService } = useTagadaContext();
10
+ const { storeId } = usePluginConfig();
11
+ const stableOptions = useMemo(() => {
12
+ return {
13
+ customerId: options.customerId ?? null,
14
+ enabled: options.enabled ?? true,
15
+ };
16
+ }, [options.customerId, options.enabled]);
17
+ const isEnabled = useMemo(() => {
18
+ return Boolean(stableOptions.enabled && stableOptions.customerId && storeId);
19
+ }, [stableOptions.enabled, stableOptions.customerId, storeId]);
20
+ const [data, setData] = useState(null);
21
+ const [isLoading, setIsLoading] = useState(false);
22
+ const [error, setError] = useState(null);
23
+ const fetchCustomerInfos = useCallback(async () => {
24
+ if (!isEnabled)
25
+ return;
26
+ if (!stableOptions.customerId || !storeId)
27
+ return;
28
+ setIsLoading(true);
29
+ setError(null);
30
+ try {
31
+ const response = await apiService.fetch(`/api/v1/customers/${stableOptions.customerId}`, {
32
+ method: 'GET',
33
+ params: { storeId },
34
+ });
35
+ setData(response ?? null);
36
+ }
37
+ catch (err) {
38
+ const safeError = err instanceof Error ? err : new Error('Failed to fetch customer infos');
39
+ setError(safeError);
40
+ }
41
+ finally {
42
+ setIsLoading(false);
43
+ }
44
+ }, [apiService, isEnabled, stableOptions.customerId, storeId]);
45
+ useEffect(() => {
46
+ void fetchCustomerInfos();
47
+ }, [fetchCustomerInfos]);
48
+ return {
49
+ data,
50
+ isLoading,
51
+ error,
52
+ refetch: fetchCustomerInfos,
53
+ };
54
+ }
@@ -1,7 +1,7 @@
1
1
  import { useCallback, useState } from 'react';
2
2
  import { useTagadaContext } from '../providers/TagadaProvider';
3
- import { usePluginConfig } from './usePluginConfig';
4
3
  import { setClientToken } from '../utils/tokenStorage';
4
+ import { usePluginConfig } from './usePluginConfig';
5
5
  export function useLogin() {
6
6
  const [isLoading, setIsLoading] = useState(false);
7
7
  const [error, setError] = useState(null);
@@ -7,6 +7,7 @@ export { useCheckout } from './hooks/useCheckout';
7
7
  export { useClubOffers } from './hooks/useClubOffers';
8
8
  export { useCurrency } from './hooks/useCurrency';
9
9
  export { useCustomer } from './hooks/useCustomer';
10
+ export { useCustomerInfos } from './hooks/useCustomerInfos';
10
11
  export { useDiscounts } from './hooks/useDiscounts';
11
12
  export { useEnvironment } from './hooks/useEnvironment';
12
13
  export { useGeoLocation } from './hooks/useGeoLocation';
@@ -10,6 +10,7 @@ export { useCheckout } from './hooks/useCheckout';
10
10
  export { useClubOffers } from './hooks/useClubOffers';
11
11
  export { useCurrency } from './hooks/useCurrency';
12
12
  export { useCustomer } from './hooks/useCustomer';
13
+ export { useCustomerInfos } from './hooks/useCustomerInfos';
13
14
  export { useDiscounts } from './hooks/useDiscounts';
14
15
  export { useEnvironment } from './hooks/useEnvironment';
15
16
  export { useGeoLocation } from './hooks/useGeoLocation';
@@ -10,6 +10,7 @@ export function setClientToken(token) {
10
10
  if (typeof window !== 'undefined') {
11
11
  try {
12
12
  localStorage.setItem(TOKEN_KEY, token);
13
+ window.dispatchEvent(new Event('storage'));
13
14
  }
14
15
  catch (error) {
15
16
  console.error('Failed to save token to localStorage:', error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.4.28",
3
+ "version": "2.4.30",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",