give
/
src
/
DonorDashboards
/
resources
/
js
/
app
/
components
/
subscription-manager
/
hooks
/
pause-subscription.ts
give
/
src
/
DonorDashboards
/
resources
/
js
/
app
/
components
/
subscription-manager
/
hooks
Last commit date
pause-subscription.ts
1 year ago
pause-subscription.ts
47 lines
| 1 | import {useState} from 'react'; |
| 2 | import {managePausingSubscriptionWithAPI} from '../utils'; |
| 3 | |
| 4 | export type pauseDuration = number; |
| 5 | type id = string; |
| 6 | |
| 7 | const usePauseSubscription = (id: id) => { |
| 8 | const [loading, setLoading] = useState<boolean>(false); |
| 9 | |
| 10 | const handlePause = async (pauseDuration: pauseDuration) => { |
| 11 | setLoading(true); |
| 12 | try { |
| 13 | await managePausingSubscriptionWithAPI({ |
| 14 | id, |
| 15 | intervalInMonths: pauseDuration, |
| 16 | }); |
| 17 | } catch (error) { |
| 18 | console.error('Error pausing subscription:', error); |
| 19 | } finally { |
| 20 | setLoading(false); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | const handleResume = async () => { |
| 25 | setLoading(true); |
| 26 | try { |
| 27 | await managePausingSubscriptionWithAPI({ |
| 28 | id, |
| 29 | action: 'resume', |
| 30 | }); |
| 31 | } catch (error) { |
| 32 | console.error('Error resuming subscription:', error); |
| 33 | } finally { |
| 34 | setLoading(false); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | return { |
| 39 | loading, |
| 40 | setLoading, |
| 41 | handlePause, |
| 42 | handleResume, |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | export default usePauseSubscription; |
| 47 |