@@ -23,6 +23,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@
2323import { trimInput } from '@/lib/utils/sanitize' ;
2424import { useNotify } from '@/lib/hooks/useNotify' ;
2525import { usePayments , type ApiPayment } from '@/lib/api/hooks' ;
26+ import { apiClient } from '@/lib/api/axios' ;
2627import Link from 'next/link' ;
2728
2829type PaymentLink = ApiPayment ;
@@ -99,7 +100,7 @@ const PaymentLinkCard = memo(function PaymentLinkCard({ link, onEdit, onDelete,
99100
100101export default function PaymentsPage ( ) {
101102 const { data : links , isLoading, error : fetchError , refetch } = usePayments ( ) ;
102- const { success : notifySuccess , info : notifyInfo } = useNotify ( ) ;
103+ const { success : notifySuccess , info : notifyInfo , error : notifyError } = useNotify ( ) ;
103104
104105 // Filter & Search states
105106 const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
@@ -116,6 +117,7 @@ export default function PaymentsPage() {
116117 const [ deletingLink , setDeletingLink ] = useState < PaymentLink | null > ( null ) ;
117118 const [ selectedQrLink , setSelectedQrLink ] = useState < PaymentLink | null > ( null ) ;
118119 const [ linksError , setLinksError ] = useState ( false ) ;
120+ const [ isCreating , setIsCreating ] = useState ( false ) ;
119121
120122 // Form states
121123 const [ labelValue , setLabelValue ] = useState ( '' ) ;
@@ -156,17 +158,50 @@ export default function PaymentsPage() {
156158 return filteredLinks . slice ( start , start + pageSize ) ;
157159 } , [ filteredLinks , currentPage , pageSize ] ) ;
158160
159- const handleCreate = ( e : React . FormEvent ) => {
161+ const handleCreate = async ( e : React . FormEvent ) => {
160162 e . preventDefault ( ) ;
161163 const sanitizedLabel = trimInput ( labelValue ) ;
162164 if ( ! sanitizedLabel ) {
163165 setLabelError ( 'Label is required' ) ;
164166 return ;
165167 }
166168 setLabelError ( '' ) ;
167- notifySuccess ( 'Payment link created successfully' ) ;
168- setIsCreateOpen ( false ) ;
169- resetForm ( ) ;
169+
170+ const payload : Record < string , unknown > = {
171+ label : sanitizedLabel ,
172+ currency : currencyValue ,
173+ mode : currencyMode ,
174+ reference : referenceValue || undefined ,
175+ expiry : expiryValue || undefined ,
176+ redirectUrl : redirectUrlValue || undefined ,
177+ } ;
178+
179+ if ( currencyMode === 'single' ) {
180+ payload . amount = amountValue ? parseFloat ( amountValue ) : undefined ;
181+ } else {
182+ const amounts : Record < string , number > = { } ;
183+ for ( const [ code , val ] of Object . entries ( multiCurrencyAmounts ) ) {
184+ if ( val ) amounts [ code ] = parseFloat ( val ) ;
185+ }
186+ payload . amounts = Object . keys ( amounts ) . length > 0 ? amounts : undefined ;
187+ payload . currencies = selectedCurrencies ;
188+ }
189+
190+ setIsCreating ( true ) ;
191+ try {
192+ await apiClient . post ( '/api/payment-links' , payload ) ;
193+ notifySuccess ( 'Payment link created successfully' ) ;
194+ setIsCreateOpen ( false ) ;
195+ resetForm ( ) ;
196+ refetch ( ) ;
197+ } catch ( err : unknown ) {
198+ const message =
199+ ( err as { response ?: { data ?: { error ?: string } } } ) ?. response ?. data ?. error ??
200+ 'Failed to create payment link' ;
201+ notifyError ( message ) ;
202+ } finally {
203+ setIsCreating ( false ) ;
204+ }
170205 } ;
171206
172207 const { register : registerEdit , handleSubmit : handleEditSubmitForm , reset : resetEditForm , formState : { errors : editErrors } } = useForm < EditPaymentLinkFormValues > ( {
@@ -375,7 +410,9 @@ export default function PaymentsPage() {
375410
376411 < DialogFooter className = "pt-4" >
377412 < Button type = "button" variant = "ghost" onClick = { ( ) => setIsCreateOpen ( false ) } > Cancel</ Button >
378- < Button type = "submit" > Create Link</ Button >
413+ < Button type = "submit" disabled = { isCreating } >
414+ { isCreating ? 'Creating...' : 'Create Link' }
415+ </ Button >
379416 </ DialogFooter >
380417 </ form >
381418 </ DialogContent >
0 commit comments