forked from adrianhajdin/banking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaidLink.tsx
74 lines (63 loc) · 1.93 KB
/
PlaidLink.tsx
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { useCallback, useEffect, useState } from 'react'
import { Button } from './ui/button'
import { PlaidLinkOnSuccess, PlaidLinkOptions, usePlaidLink } from 'react-plaid-link'
import { useRouter } from 'next/navigation';
import { createLinkToken, exchangePublicToken } from '@/lib/actions/user.actions';
import Image from 'next/image';
const PlaidLink = ({ user, variant }: PlaidLinkProps) => {
const router = useRouter();
const [token, setToken] = useState('');
useEffect(() => {
const getLinkToken = async () => {
const data = await createLinkToken(user);
setToken(data?.linkToken);
}
getLinkToken();
}, [user]);
const onSuccess = useCallback<PlaidLinkOnSuccess>(async (public_token: string) => {
await exchangePublicToken({
publicToken: public_token,
user,
})
router.push('/');
}, [user])
const config: PlaidLinkOptions = {
token,
onSuccess
}
const { open, ready } = usePlaidLink(config);
return (
<>
{variant === 'primary' ? (
<Button
onClick={() => open()}
disabled={!ready}
className="plaidlink-primary"
>
Connect bank
</Button>
): variant === 'ghost' ? (
<Button onClick={() => open()} variant="ghost" className="plaidlink-ghost">
<Image
src="/icons/connect-bank.svg"
alt="connect bank"
width={24}
height={24}
/>
<p className='hiddenl text-[16px] font-semibold text-black-2 xl:block'>Connect bank</p>
</Button>
): (
<Button onClick={() => open()} className="plaidlink-default">
<Image
src="/icons/connect-bank.svg"
alt="connect bank"
width={24}
height={24}
/>
<p className='text-[16px] font-semibold text-black-2'>Connect bank</p>
</Button>
)}
</>
)
}
export default PlaidLink