forked from iamraphson/react-paystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaystack-consumer.tsx
More file actions
39 lines (35 loc) · 1.23 KB
/
Copy pathpaystack-consumer.tsx
File metadata and controls
39 lines (35 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React, {forwardRef, useContext, FunctionComponentElement} from 'react';
import PaystackProvider from './paystack-provider';
import {PaystackProps} from './types';
import PaystackContext from './paystack-context';
interface PaystacConsumerProps extends PaystackProps {
children: Function;
onSuccess?: Function;
onClose?: Function;
}
const PaystackConsumerChild = ({
children,
ref,
}: {
children: Function;
ref: any;
}): FunctionComponentElement<any> => {
const {initializePayment, onSuccess, onClose} = useContext(PaystackContext);
const completeInitializePayment = (): void => initializePayment(onSuccess, onClose);
return children({initializePayment: completeInitializePayment, ref});
};
const PaystackConsumer = forwardRef(
(
{children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystacConsumerProps,
ref: any,
): JSX.Element => {
const onSuccess = paraSuccess ? paraSuccess : (): any => null;
const onClose = paraClose ? paraClose : (): any => null;
return (
<PaystackProvider {...others} onSuccess={onSuccess} onClose={onClose}>
<PaystackConsumerChild ref={ref}>{children}</PaystackConsumerChild>
</PaystackProvider>
);
},
);
export default PaystackConsumer;