@djangocfg/ext-payments 1.0.0
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.
- package/README.md +206 -0
- package/dist/chunk-5KY6HVXF.js +2593 -0
- package/dist/hooks.cjs +2666 -0
- package/dist/hooks.d.cts +186 -0
- package/dist/hooks.d.ts +186 -0
- package/dist/hooks.js +1 -0
- package/dist/index.cjs +2590 -0
- package/dist/index.d.cts +1287 -0
- package/dist/index.d.ts +1287 -0
- package/dist/index.js +1 -0
- package/package.json +79 -0
- package/src/api/generated/ext_payments/_utils/fetchers/ext_payments__payments.ts +408 -0
- package/src/api/generated/ext_payments/_utils/fetchers/index.ts +28 -0
- package/src/api/generated/ext_payments/_utils/hooks/ext_payments__payments.ts +147 -0
- package/src/api/generated/ext_payments/_utils/hooks/index.ts +28 -0
- package/src/api/generated/ext_payments/_utils/schemas/Balance.schema.ts +23 -0
- package/src/api/generated/ext_payments/_utils/schemas/Currency.schema.ts +28 -0
- package/src/api/generated/ext_payments/_utils/schemas/PaginatedPaymentListList.schema.ts +24 -0
- package/src/api/generated/ext_payments/_utils/schemas/PaymentDetail.schema.ts +44 -0
- package/src/api/generated/ext_payments/_utils/schemas/PaymentList.schema.ts +28 -0
- package/src/api/generated/ext_payments/_utils/schemas/Transaction.schema.ts +28 -0
- package/src/api/generated/ext_payments/_utils/schemas/index.ts +24 -0
- package/src/api/generated/ext_payments/api-instance.ts +131 -0
- package/src/api/generated/ext_payments/client.ts +301 -0
- package/src/api/generated/ext_payments/enums.ts +64 -0
- package/src/api/generated/ext_payments/errors.ts +116 -0
- package/src/api/generated/ext_payments/ext_payments__payments/client.ts +118 -0
- package/src/api/generated/ext_payments/ext_payments__payments/index.ts +2 -0
- package/src/api/generated/ext_payments/ext_payments__payments/models.ts +135 -0
- package/src/api/generated/ext_payments/http.ts +103 -0
- package/src/api/generated/ext_payments/index.ts +273 -0
- package/src/api/generated/ext_payments/logger.ts +259 -0
- package/src/api/generated/ext_payments/retry.ts +175 -0
- package/src/api/generated/ext_payments/schema.json +850 -0
- package/src/api/generated/ext_payments/storage.ts +161 -0
- package/src/api/generated/ext_payments/validation-events.ts +133 -0
- package/src/api/index.ts +9 -0
- package/src/config.ts +20 -0
- package/src/contexts/BalancesContext.tsx +62 -0
- package/src/contexts/CurrenciesContext.tsx +63 -0
- package/src/contexts/OverviewContext.tsx +173 -0
- package/src/contexts/PaymentsContext.tsx +121 -0
- package/src/contexts/PaymentsExtensionProvider.tsx +55 -0
- package/src/contexts/README.md +201 -0
- package/src/contexts/RootPaymentsContext.tsx +65 -0
- package/src/contexts/index.ts +45 -0
- package/src/contexts/types.ts +40 -0
- package/src/hooks/index.ts +20 -0
- package/src/index.ts +36 -0
- package/src/layouts/PaymentsLayout/PaymentsLayout.tsx +92 -0
- package/src/layouts/PaymentsLayout/components/CreatePaymentDialog.tsx +291 -0
- package/src/layouts/PaymentsLayout/components/PaymentDetailsDialog.tsx +290 -0
- package/src/layouts/PaymentsLayout/components/index.ts +2 -0
- package/src/layouts/PaymentsLayout/events.ts +47 -0
- package/src/layouts/PaymentsLayout/index.ts +16 -0
- package/src/layouts/PaymentsLayout/types.ts +6 -0
- package/src/layouts/PaymentsLayout/views/overview/components/BalanceCard.tsx +128 -0
- package/src/layouts/PaymentsLayout/views/overview/components/RecentPayments.tsx +142 -0
- package/src/layouts/PaymentsLayout/views/overview/components/index.ts +2 -0
- package/src/layouts/PaymentsLayout/views/overview/index.tsx +20 -0
- package/src/layouts/PaymentsLayout/views/payments/components/PaymentsList.tsx +277 -0
- package/src/layouts/PaymentsLayout/views/payments/components/index.ts +1 -0
- package/src/layouts/PaymentsLayout/views/payments/index.tsx +17 -0
- package/src/layouts/PaymentsLayout/views/transactions/components/TransactionsList.tsx +273 -0
- package/src/layouts/PaymentsLayout/views/transactions/components/index.ts +1 -0
- package/src/layouts/PaymentsLayout/views/transactions/index.tsx +17 -0
- package/src/utils/logger.ts +9 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage adapters for cross-platform token storage.
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - LocalStorage (browser)
|
|
6
|
+
* - Cookies (SSR/browser)
|
|
7
|
+
* - Memory (Node.js/Electron/testing)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { APILogger } from './logger';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Storage adapter interface for cross-platform token storage.
|
|
14
|
+
*/
|
|
15
|
+
export interface StorageAdapter {
|
|
16
|
+
getItem(key: string): string | null;
|
|
17
|
+
setItem(key: string, value: string): void;
|
|
18
|
+
removeItem(key: string): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* LocalStorage adapter with safe try-catch for browser environments.
|
|
23
|
+
* Works in modern browsers with localStorage support.
|
|
24
|
+
*
|
|
25
|
+
* Note: This adapter uses window.localStorage and should only be used in browser/client environments.
|
|
26
|
+
* For server-side usage, use MemoryStorageAdapter or CookieStorageAdapter instead.
|
|
27
|
+
*/
|
|
28
|
+
export class LocalStorageAdapter implements StorageAdapter {
|
|
29
|
+
private logger?: APILogger;
|
|
30
|
+
|
|
31
|
+
constructor(logger?: APILogger) {
|
|
32
|
+
this.logger = logger;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getItem(key: string): string | null {
|
|
36
|
+
try {
|
|
37
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
38
|
+
const value = localStorage.getItem(key);
|
|
39
|
+
this.logger?.debug(`LocalStorage.getItem("${key}"): ${value ? 'found' : 'not found'}`);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
43
|
+
} catch (error) {
|
|
44
|
+
this.logger?.error('LocalStorage.getItem failed:', error);
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setItem(key: string, value: string): void {
|
|
50
|
+
try {
|
|
51
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
52
|
+
localStorage.setItem(key, value);
|
|
53
|
+
this.logger?.debug(`LocalStorage.setItem("${key}"): success`);
|
|
54
|
+
} else {
|
|
55
|
+
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
this.logger?.error('LocalStorage.setItem failed:', error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
removeItem(key: string): void {
|
|
63
|
+
try {
|
|
64
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
65
|
+
localStorage.removeItem(key);
|
|
66
|
+
this.logger?.debug(`LocalStorage.removeItem("${key}"): success`);
|
|
67
|
+
} else {
|
|
68
|
+
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
69
|
+
}
|
|
70
|
+
} catch (error) {
|
|
71
|
+
this.logger?.error('LocalStorage.removeItem failed:', error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Cookie-based storage adapter for SSR and browser environments.
|
|
78
|
+
* Useful for Next.js, Nuxt.js, and other SSR frameworks.
|
|
79
|
+
*/
|
|
80
|
+
export class CookieStorageAdapter implements StorageAdapter {
|
|
81
|
+
private logger?: APILogger;
|
|
82
|
+
|
|
83
|
+
constructor(logger?: APILogger) {
|
|
84
|
+
this.logger = logger;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
getItem(key: string): string | null {
|
|
88
|
+
try {
|
|
89
|
+
if (typeof document === 'undefined') {
|
|
90
|
+
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const value = `; ${document.cookie}`;
|
|
94
|
+
const parts = value.split(`; ${key}=`);
|
|
95
|
+
if (parts.length === 2) {
|
|
96
|
+
const result = parts.pop()?.split(';').shift() || null;
|
|
97
|
+
this.logger?.debug(`CookieStorage.getItem("${key}"): ${result ? 'found' : 'not found'}`);
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
this.logger?.debug(`CookieStorage.getItem("${key}"): not found`);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
this.logger?.error('CookieStorage.getItem failed:', error);
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
setItem(key: string, value: string): void {
|
|
108
|
+
try {
|
|
109
|
+
if (typeof document !== 'undefined') {
|
|
110
|
+
document.cookie = `${key}=${value}; path=/; max-age=31536000`;
|
|
111
|
+
this.logger?.debug(`CookieStorage.setItem("${key}"): success`);
|
|
112
|
+
} else {
|
|
113
|
+
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
this.logger?.error('CookieStorage.setItem failed:', error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
removeItem(key: string): void {
|
|
121
|
+
try {
|
|
122
|
+
if (typeof document !== 'undefined') {
|
|
123
|
+
document.cookie = `${key}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
124
|
+
this.logger?.debug(`CookieStorage.removeItem("${key}"): success`);
|
|
125
|
+
} else {
|
|
126
|
+
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
this.logger?.error('CookieStorage.removeItem failed:', error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* In-memory storage adapter for Node.js, Electron, and testing environments.
|
|
136
|
+
* Data is stored in RAM and cleared when process exits.
|
|
137
|
+
*/
|
|
138
|
+
export class MemoryStorageAdapter implements StorageAdapter {
|
|
139
|
+
private storage: Map<string, string> = new Map();
|
|
140
|
+
private logger?: APILogger;
|
|
141
|
+
|
|
142
|
+
constructor(logger?: APILogger) {
|
|
143
|
+
this.logger = logger;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getItem(key: string): string | null {
|
|
147
|
+
const value = this.storage.get(key) || null;
|
|
148
|
+
this.logger?.debug(`MemoryStorage.getItem("${key}"): ${value ? 'found' : 'not found'}`);
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
setItem(key: string, value: string): void {
|
|
153
|
+
this.storage.set(key, value);
|
|
154
|
+
this.logger?.debug(`MemoryStorage.setItem("${key}"): success`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
removeItem(key: string): void {
|
|
158
|
+
this.storage.delete(key);
|
|
159
|
+
this.logger?.debug(`MemoryStorage.removeItem("${key}"): success`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod Validation Events - Browser CustomEvent integration
|
|
3
|
+
*
|
|
4
|
+
* Dispatches browser CustomEvents when Zod validation fails, allowing
|
|
5
|
+
* React/frontend apps to listen and handle validation errors globally.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // In your React app
|
|
10
|
+
* window.addEventListener('zod-validation-error', (event) => {
|
|
11
|
+
* const { operation, path, method, error, response } = event.detail;
|
|
12
|
+
* console.error(`Validation failed for ${method} ${path}`, error);
|
|
13
|
+
* // Show toast notification, log to Sentry, etc.
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ZodError } from 'zod'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validation error event detail
|
|
22
|
+
*/
|
|
23
|
+
export interface ValidationErrorDetail {
|
|
24
|
+
/** Operation/function name that failed validation */
|
|
25
|
+
operation: string
|
|
26
|
+
/** API endpoint path */
|
|
27
|
+
path: string
|
|
28
|
+
/** HTTP method */
|
|
29
|
+
method: string
|
|
30
|
+
/** Zod validation error */
|
|
31
|
+
error: ZodError
|
|
32
|
+
/** Raw response data that failed validation */
|
|
33
|
+
response: any
|
|
34
|
+
/** Timestamp of the error */
|
|
35
|
+
timestamp: Date
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Custom event type for Zod validation errors
|
|
40
|
+
*/
|
|
41
|
+
export type ValidationErrorEvent = CustomEvent<ValidationErrorDetail>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Dispatch a Zod validation error event.
|
|
45
|
+
*
|
|
46
|
+
* Only dispatches in browser environment (when window is defined).
|
|
47
|
+
* Safe to call in Node.js/SSR - will be a no-op.
|
|
48
|
+
*
|
|
49
|
+
* @param detail - Validation error details
|
|
50
|
+
*/
|
|
51
|
+
export function dispatchValidationError(detail: ValidationErrorDetail): void {
|
|
52
|
+
// Check if running in browser
|
|
53
|
+
if (typeof window === 'undefined') {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const event = new CustomEvent<ValidationErrorDetail>('zod-validation-error', {
|
|
59
|
+
detail,
|
|
60
|
+
bubbles: true,
|
|
61
|
+
cancelable: false,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
window.dispatchEvent(event)
|
|
65
|
+
} catch (error) {
|
|
66
|
+
// Silently fail - validation event dispatch should never crash the app
|
|
67
|
+
console.warn('Failed to dispatch validation error event:', error)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Add a global listener for Zod validation errors.
|
|
73
|
+
*
|
|
74
|
+
* @param callback - Function to call when validation error occurs
|
|
75
|
+
* @returns Cleanup function to remove the listener
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const cleanup = onValidationError(({ operation, error }) => {
|
|
80
|
+
* toast.error(`Validation failed in ${operation}`);
|
|
81
|
+
* logToSentry(error);
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* // Later, remove listener
|
|
85
|
+
* cleanup();
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export function onValidationError(
|
|
89
|
+
callback: (detail: ValidationErrorDetail) => void
|
|
90
|
+
): () => void {
|
|
91
|
+
if (typeof window === 'undefined') {
|
|
92
|
+
// Return no-op cleanup function for SSR
|
|
93
|
+
return () => {}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const handler = (event: Event) => {
|
|
97
|
+
if (event instanceof CustomEvent) {
|
|
98
|
+
callback(event.detail)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
window.addEventListener('zod-validation-error', handler)
|
|
103
|
+
|
|
104
|
+
// Return cleanup function
|
|
105
|
+
return () => {
|
|
106
|
+
window.removeEventListener('zod-validation-error', handler)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Format Zod error for logging/display.
|
|
112
|
+
*
|
|
113
|
+
* @param error - Zod validation error
|
|
114
|
+
* @returns Formatted error message
|
|
115
|
+
*/
|
|
116
|
+
export function formatZodError(error: ZodError): string {
|
|
117
|
+
const issues = error.issues.map((issue, index) => {
|
|
118
|
+
const path = issue.path.join('.') || 'root'
|
|
119
|
+
const parts = [`${index + 1}. ${path}: ${issue.message}`]
|
|
120
|
+
|
|
121
|
+
if ('expected' in issue && issue.expected) {
|
|
122
|
+
parts.push(` Expected: ${issue.expected}`)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if ('received' in issue && issue.received) {
|
|
126
|
+
parts.push(` Received: ${issue.received}`)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return parts.join('\n')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
return issues.join('\n')
|
|
133
|
+
}
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payments Extension API
|
|
3
|
+
*
|
|
4
|
+
* Pre-configured API instance with shared authentication
|
|
5
|
+
*/
|
|
6
|
+
import { API } from './generated/ext_payments';
|
|
7
|
+
import { createExtensionAPI } from '@djangocfg/ext-base/api';
|
|
8
|
+
|
|
9
|
+
export const apiPayments = createExtensionAPI(API);
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payments extension configuration
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionMetadata } from '@djangocfg/ext-base';
|
|
6
|
+
|
|
7
|
+
export const extensionConfig: ExtensionMetadata = {
|
|
8
|
+
name: 'payments',
|
|
9
|
+
version: '1.0.0',
|
|
10
|
+
author: 'DjangoCFG',
|
|
11
|
+
displayName: 'Payments',
|
|
12
|
+
description: 'Payment processing and subscription management',
|
|
13
|
+
icon: '💳',
|
|
14
|
+
license: 'MIT',
|
|
15
|
+
githubUrl: 'https://github.com/markolofsen/django-cfg',
|
|
16
|
+
homepage: 'https://djangocfg.com',
|
|
17
|
+
keywords: ['payments', 'subscriptions', 'billing', 'checkout', 'stripe'],
|
|
18
|
+
dependencies: [],
|
|
19
|
+
minVersion: '2.0.0',
|
|
20
|
+
} as const;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, { createContext, useContext, type ReactNode } from 'react';
|
|
4
|
+
import { apiPayments } from '../api';
|
|
5
|
+
import { usePaymentsBalanceRetrieve } from '../api/generated/ext_payments/_utils/hooks';
|
|
6
|
+
|
|
7
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
8
|
+
// Context Type
|
|
9
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
export interface BalancesContextValue {
|
|
12
|
+
// Balance data - single endpoint returns balance info
|
|
13
|
+
balance: any | undefined;
|
|
14
|
+
isLoadingBalance: boolean;
|
|
15
|
+
balanceError: Error | undefined;
|
|
16
|
+
refreshBalance: () => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
20
|
+
// Context
|
|
21
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
const BalancesContext = createContext<BalancesContextValue | undefined>(undefined);
|
|
24
|
+
|
|
25
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
26
|
+
// Provider
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
export function BalancesProvider({ children }: { children: ReactNode }) {
|
|
30
|
+
// Get balance from /cfg/payments/balance/
|
|
31
|
+
const {
|
|
32
|
+
data: balance,
|
|
33
|
+
error: balanceError,
|
|
34
|
+
isLoading: isLoadingBalance,
|
|
35
|
+
mutate: mutateBalance,
|
|
36
|
+
} = usePaymentsBalanceRetrieve(apiPayments);
|
|
37
|
+
|
|
38
|
+
const refreshBalance = async () => {
|
|
39
|
+
await mutateBalance();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const value: BalancesContextValue = {
|
|
43
|
+
balance,
|
|
44
|
+
isLoadingBalance,
|
|
45
|
+
balanceError,
|
|
46
|
+
refreshBalance,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return <BalancesContext.Provider value={value}>{children}</BalancesContext.Provider>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
53
|
+
// Hook
|
|
54
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
export function useBalancesContext(): BalancesContextValue {
|
|
57
|
+
const context = useContext(BalancesContext);
|
|
58
|
+
if (!context) {
|
|
59
|
+
throw new Error('useBalancesContext must be used within BalancesProvider');
|
|
60
|
+
}
|
|
61
|
+
return context;
|
|
62
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, { createContext, useContext, type ReactNode } from 'react';
|
|
4
|
+
import { apiPayments } from '../api';
|
|
5
|
+
import { usePaymentsCurrenciesList } from '../api/generated/ext_payments/_utils/hooks';
|
|
6
|
+
|
|
7
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
8
|
+
// Context Type
|
|
9
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
export interface CurrenciesContextValue {
|
|
12
|
+
// Currencies data - returns raw response with currencies array
|
|
13
|
+
currencies: any | undefined;
|
|
14
|
+
isLoadingCurrencies: boolean;
|
|
15
|
+
currenciesError: Error | undefined;
|
|
16
|
+
refreshCurrencies: () => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
20
|
+
// Context
|
|
21
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
const CurrenciesContext = createContext<CurrenciesContextValue | undefined>(undefined);
|
|
24
|
+
|
|
25
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
26
|
+
// Provider
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
export function CurrenciesProvider({ children }: { children: ReactNode }) {
|
|
30
|
+
// Get currencies list from /cfg/payments/currencies/
|
|
31
|
+
const {
|
|
32
|
+
data: currencies,
|
|
33
|
+
error: currenciesError,
|
|
34
|
+
isLoading: isLoadingCurrencies,
|
|
35
|
+
mutate: mutateCurrencies,
|
|
36
|
+
} = usePaymentsCurrenciesList(apiPayments);
|
|
37
|
+
|
|
38
|
+
const refreshCurrencies = async () => {
|
|
39
|
+
await mutateCurrencies();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const value: CurrenciesContextValue = {
|
|
43
|
+
currencies,
|
|
44
|
+
isLoadingCurrencies,
|
|
45
|
+
currenciesError,
|
|
46
|
+
refreshCurrencies,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return <CurrenciesContext.Provider value={value}>{children}</CurrenciesContext.Provider>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
53
|
+
// Hook
|
|
54
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
55
|
+
|
|
56
|
+
export function useCurrenciesContext(): CurrenciesContextValue {
|
|
57
|
+
const context = useContext(CurrenciesContext);
|
|
58
|
+
if (!context) {
|
|
59
|
+
throw new Error('useCurrenciesContext must be used within CurrenciesProvider');
|
|
60
|
+
}
|
|
61
|
+
return context;
|
|
62
|
+
}
|
|
63
|
+
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import React, { createContext, useContext, type ReactNode } from 'react';
|
|
4
|
+
import { SWRConfig } from 'swr';
|
|
5
|
+
import { apiPayments } from '../api';
|
|
6
|
+
import {
|
|
7
|
+
usePaymentsBalanceRetrieve,
|
|
8
|
+
usePaymentsPaymentsList,
|
|
9
|
+
usePaymentsTransactionsList,
|
|
10
|
+
useCreatePaymentsPaymentsCreateCreate,
|
|
11
|
+
} from '../api/generated/ext_payments/_utils/hooks';
|
|
12
|
+
import type {
|
|
13
|
+
PaginatedPaymentListList,
|
|
14
|
+
PaymentList,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
18
|
+
// Context Type
|
|
19
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
export interface OverviewContextValue {
|
|
22
|
+
// Balance data
|
|
23
|
+
balance: any | undefined;
|
|
24
|
+
isLoadingBalance: boolean;
|
|
25
|
+
balanceError: Error | undefined;
|
|
26
|
+
refreshBalance: () => Promise<void>;
|
|
27
|
+
|
|
28
|
+
// Payments data
|
|
29
|
+
payments: PaginatedPaymentListList | undefined;
|
|
30
|
+
isLoadingPayments: boolean;
|
|
31
|
+
paymentsError: Error | undefined;
|
|
32
|
+
refreshPayments: () => Promise<void>;
|
|
33
|
+
|
|
34
|
+
// Transactions data
|
|
35
|
+
transactions: any | undefined;
|
|
36
|
+
isLoadingTransactions: boolean;
|
|
37
|
+
transactionsError: Error | undefined;
|
|
38
|
+
refreshTransactions: () => Promise<void>;
|
|
39
|
+
|
|
40
|
+
// Payment operations
|
|
41
|
+
createPayment: () => Promise<PaymentList>;
|
|
42
|
+
|
|
43
|
+
// Loading states
|
|
44
|
+
isLoadingOverview: boolean;
|
|
45
|
+
overviewError: Error | undefined;
|
|
46
|
+
|
|
47
|
+
// Operations
|
|
48
|
+
refreshOverview: () => Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
52
|
+
// Context
|
|
53
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
const OverviewContext = createContext<OverviewContextValue | undefined>(undefined);
|
|
56
|
+
|
|
57
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
58
|
+
// Provider
|
|
59
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
export function OverviewProvider({ children }: { children: ReactNode }) {
|
|
62
|
+
// SWR config for overview data - disable auto-revalidation
|
|
63
|
+
const swrConfig = {
|
|
64
|
+
revalidateOnFocus: false,
|
|
65
|
+
revalidateOnReconnect: false,
|
|
66
|
+
revalidateIfStale: false,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Balance
|
|
70
|
+
const {
|
|
71
|
+
data: balance,
|
|
72
|
+
error: balanceError,
|
|
73
|
+
isLoading: isLoadingBalance,
|
|
74
|
+
mutate: mutateBalance,
|
|
75
|
+
} = usePaymentsBalanceRetrieve(apiPayments);
|
|
76
|
+
|
|
77
|
+
// Payments list
|
|
78
|
+
const {
|
|
79
|
+
data: payments,
|
|
80
|
+
error: paymentsError,
|
|
81
|
+
isLoading: isLoadingPayments,
|
|
82
|
+
mutate: mutatePayments,
|
|
83
|
+
} = usePaymentsPaymentsList({}, apiPayments);
|
|
84
|
+
|
|
85
|
+
// Transactions
|
|
86
|
+
const {
|
|
87
|
+
data: transactions,
|
|
88
|
+
error: transactionsError,
|
|
89
|
+
isLoading: isLoadingTransactions,
|
|
90
|
+
mutate: mutateTransactions,
|
|
91
|
+
} = usePaymentsTransactionsList({}, apiPayments);
|
|
92
|
+
|
|
93
|
+
// Payment mutations
|
|
94
|
+
const createPaymentMutation = useCreatePaymentsPaymentsCreateCreate();
|
|
95
|
+
|
|
96
|
+
const isLoadingOverview = isLoadingBalance || isLoadingPayments || isLoadingTransactions;
|
|
97
|
+
const overviewError = balanceError || paymentsError || transactionsError;
|
|
98
|
+
|
|
99
|
+
const refreshBalance = async () => {
|
|
100
|
+
await mutateBalance();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const refreshPayments = async () => {
|
|
104
|
+
await mutatePayments();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const refreshTransactions = async () => {
|
|
108
|
+
await mutateTransactions();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const refreshOverview = async () => {
|
|
112
|
+
await Promise.all([
|
|
113
|
+
mutateBalance(),
|
|
114
|
+
mutatePayments(),
|
|
115
|
+
mutateTransactions(),
|
|
116
|
+
]);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Create payment
|
|
120
|
+
const createPayment = async (): Promise<PaymentList> => {
|
|
121
|
+
const result = await createPaymentMutation(apiPayments);
|
|
122
|
+
// Refresh overview data to show new payment
|
|
123
|
+
await refreshOverview();
|
|
124
|
+
return result as PaymentList;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const value: OverviewContextValue = {
|
|
128
|
+
balance,
|
|
129
|
+
isLoadingBalance,
|
|
130
|
+
balanceError,
|
|
131
|
+
refreshBalance,
|
|
132
|
+
payments,
|
|
133
|
+
isLoadingPayments,
|
|
134
|
+
paymentsError,
|
|
135
|
+
refreshPayments,
|
|
136
|
+
transactions,
|
|
137
|
+
isLoadingTransactions,
|
|
138
|
+
transactionsError,
|
|
139
|
+
refreshTransactions,
|
|
140
|
+
createPayment,
|
|
141
|
+
isLoadingOverview,
|
|
142
|
+
overviewError,
|
|
143
|
+
refreshOverview,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<SWRConfig value={swrConfig}>
|
|
148
|
+
<OverviewContext.Provider value={value}>{children}</OverviewContext.Provider>
|
|
149
|
+
</SWRConfig>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
154
|
+
// Hook
|
|
155
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
156
|
+
|
|
157
|
+
export function useOverviewContext(): OverviewContextValue {
|
|
158
|
+
const context = useContext(OverviewContext);
|
|
159
|
+
if (!context) {
|
|
160
|
+
throw new Error('useOverviewContext must be used within OverviewProvider');
|
|
161
|
+
}
|
|
162
|
+
return context;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
166
|
+
// Re-export types
|
|
167
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
168
|
+
|
|
169
|
+
export type {
|
|
170
|
+
PaginatedPaymentListList,
|
|
171
|
+
PaymentList,
|
|
172
|
+
};
|
|
173
|
+
|