Last active
January 22, 2021 02:32
-
-
Save qweliant/a0fa87414d97d8cbb3f8845bbba74502 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useCallback, useEffect } from "react"; | |
import { usePlaidLink } from "react-plaid-link"; | |
import axios from "axios"; | |
import qs from "qs"; | |
const tokenURL = `http://localhost:8000/api/create_link_token`; | |
const sendTokenURL = `http://localhost:8000/api/set_access_token`; | |
const Link = () => { | |
const [data, setData] = useState(""); | |
const fetchToken = useCallback(async () => { | |
const config = { | |
method: "post", | |
url: tokenURL, | |
}; | |
const res = await axios(config); | |
console.log(res) | |
setData(res.data.link_token); | |
}, []); | |
useEffect(() => { | |
fetchToken(); | |
}, [fetchToken]); | |
const onSuccess = useCallback(async (token, metadata) => { | |
// send token to server | |
const config = { | |
method: "post", | |
url: sendTokenURL, | |
data: qs.stringify({ public_token: token }), | |
headers: { "content-type": "application/json" }, | |
}; | |
try { | |
const response = await axios(config); | |
console.log(response) | |
} catch (error) { | |
console.error(error); | |
} | |
}, []); | |
const config = { | |
token: data, | |
onSuccess, | |
}; | |
const { open, ready, err } = usePlaidLink(config); | |
if (err) return <p>Error!</p>; | |
return ( | |
<div> | |
<button onClick={() => open()} disabled={!ready}> | |
Connect a bank account | |
</button> | |
</div> | |
); | |
} | |
export default Link; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment