Debian Codegraph @

examples/react-spa/frontend/src/socialaccount/ManageProviders.js

raw
1 import { useState, useEffect } from 'react'
2 import * as allauth from '../lib/allauth'
3 import ProviderList from './ProviderList'
4 import FormErrors from '../components/FormErrors'
5 import Button from '../components/Button'
6
7 export default function ManageProviders () {
8 const [accounts, setAccounts] = useState([])
9 const [response, setResponse] = useState({ fetching: false, content: { status: 200, data: [] } })
10
11 useEffect(() => {
12 setResponse((r) => { return { ...r, fetching: true } })
13 allauth.getProviderAccounts().then((resp) => {
14 if (resp.status === 200) {
15 setAccounts(resp.data)
16 }
17 }).then(() => {
18 setResponse((r) => { return { ...r, fetching: false } })
19 })
20 }, [])
21
22 function disconnect (account) {
23 setResponse({ ...response, fetching: true })
24 allauth.disconnectProviderAccount(account.provider.id, account.uid).then((resp) => {
25 setResponse((r) => { return { ...r, content: resp } })
26 if (resp.status === 200) {
27 setAccounts(resp.data)
28 }
29 }).catch((e) => {
30 console.error(e)
31 window.alert(e)
32 }).then(() => {
33 setResponse((r) => { return { ...r, fetching: false } })
34 })
35 }
36
37 return (
38 <div>
39 <h1>Providers</h1>
40
41 <table>
42 <thead>
43 <tr>
44 <th>UID</th>
45 <th>Account</th>
46 <th>Provider</th>
47 <th>Actions</th>
48 </tr>
49 </thead>
50 <tbody>
51 {accounts.map(account => {
52 return (
53 <tr key={account.uid}>
54 <td>{account.uid}</td>
55 <td>{account.display}</td>
56 <td>{account.provider.name}</td>
57 <td>
58 <Button onClick={() => disconnect(account)} disabled={response.fetching}>Disconnect</Button>
59 </td>
60 </tr>
61 )
62 })}
63 </tbody>
64 </table>
65
66 <FormErrors errors={response.content?.errors} />
67
68 <h2>Connect</h2>
69 <ProviderList callbackURL='/account/providers' process={allauth.AuthProcess.CONNECT} />
70 </div>
71 )
72 }