@@ -25,34 +25,49 @@ const HumanFriendlyDate = ({ date }: { date: string | Date }) => {
2525
2626interface StripeCurrentLinksPanelProps {
2727 getLinks : ( ) => Promise < GetInvoiceLinksResponse > ;
28+ deactivateLink : ( linkId : string ) => Promise < void > ;
2829}
2930
3031export const StripeCurrentLinksPanel : React . FC <
3132 StripeCurrentLinksPanelProps
32- > = ( { getLinks } ) => {
33+ > = ( { getLinks, deactivateLink } ) => {
3334 const [ links , setLinks ] = useState < GetInvoiceLinksResponse | null > ( null ) ;
3435 const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
3536 const [ selectedRows , setSelectedRows ] = useState < string [ ] > ( [ ] ) ;
3637 const { userData } = useAuth ( ) ;
37- useEffect ( ( ) => {
38- const getLinksOnLoad = async ( ) => {
39- try {
40- setIsLoading ( true ) ;
41- const data = await getLinks ( ) ;
42- setLinks ( data ) ;
43- setIsLoading ( false ) ;
44- } catch ( e ) {
45- setIsLoading ( false ) ;
46- notifications . show ( {
47- title : "Error" ,
48- message :
49- "Failed to get payment links. Please try again or contact support." ,
50- color : "red" ,
51- icon : < IconAlertCircle size = { 16 } /> ,
52- } ) ;
53- console . error ( e ) ;
38+ const deleteLinks = async ( linkIds : string [ ] ) => {
39+ const promises = linkIds . map ( ( x ) => deactivateLink ( x ) ) ;
40+ const results = await Promise . allSettled ( promises ) ;
41+ let success = 0 ;
42+ let fail = 0 ;
43+ for ( const item of results ) {
44+ if ( item . status === "rejected" ) {
45+ fail ++ ;
46+ } else {
47+ success ++ ;
5448 }
55- } ;
49+ }
50+ return { fail, success } ;
51+ } ;
52+ const getLinksOnLoad = async ( ) => {
53+ try {
54+ setIsLoading ( true ) ;
55+ const data = await getLinks ( ) ;
56+ setLinks ( data ) ;
57+ setIsLoading ( false ) ;
58+ } catch ( e ) {
59+ setIsLoading ( false ) ;
60+ notifications . show ( {
61+ title : "Error" ,
62+ message :
63+ "Failed to get payment links. Please try again or contact support." ,
64+ color : "red" ,
65+ icon : < IconAlertCircle size = { 16 } /> ,
66+ } ) ;
67+ console . error ( e ) ;
68+ }
69+ } ;
70+ useEffect ( ( ) => {
5671 getLinksOnLoad ( ) ;
5772 } , [ ] ) ;
5873 const createTableRow = ( data : GetInvoiceLinksResponse [ number ] ) => {
@@ -116,13 +131,27 @@ export const StripeCurrentLinksPanel: React.FC<
116131 </ Table . Tr >
117132 ) ;
118133 } ;
119- const deactivateLinks = ( linkIds : string [ ] ) => {
120- notifications . show ( {
121- title : "Feature not available" ,
122- message : "Coming soon!" ,
123- color : "yellow" ,
124- icon : < IconAlertTriangle size = { 16 } /> ,
125- } ) ;
134+ const deactivateLinks = async ( linkIds : string [ ] ) => {
135+ setIsLoading ( true ) ;
136+ try {
137+ const result = await deleteLinks ( linkIds ) ;
138+ if ( result . fail > 0 ) {
139+ notifications . show ( {
140+ title : `Failed to deactivate ${ pluralize ( "link" , result . fail , true ) } .` ,
141+ message : "Please try again later." ,
142+ color : "red" ,
143+ } ) ;
144+ }
145+ if ( result . success > 0 ) {
146+ notifications . show ( {
147+ message : `Deactivated ${ pluralize ( "link" , result . success , true ) } !` ,
148+ color : "green" ,
149+ } ) ;
150+ }
151+ getLinksOnLoad ( ) ;
152+ } finally {
153+ setIsLoading ( false ) ;
154+ }
126155 } ;
127156
128157 return (
0 commit comments