ConfirmRemoveAccountModal.jsx 1.91 KB
Newer Older
1
2
3
import React, { useEffect, useState } from 'react'
import { useOktaAuth } from '@okta/okta-react';
import "./ConfirmRemoveAccountModal.css"
4
import config from '../../../config';
5
6
7
8

const ConfirmRemoveAccountModal = ({ removeModal, headerMessage, bodyMessage }) => {
  const { authState, oktaAuth } = useOktaAuth();
  const [userInfo, setUserInfo] = useState(null);
9
  const accessToken = oktaAuth.getAccessToken();
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

  const stopProp = e => {
    e.stopPropagation()
  }

  useEffect(() => {
    if (!authState.isAuthenticated) {
      // When user isn't authenticated, forget any user info
      setUserInfo(null);
    } else {
      oktaAuth.getUser().then(info => {
        setUserInfo(info);
      });
    }
  }, [authState, oktaAuth]); // Update if authState changes

  const removeAccountRequestOptions = {
    method: 'DELETE',
28
29
30
    headers: {
      Authorization: `Bearer ${accessToken}`,
    },
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
    body: JSON.stringify(
      {
        email: `${userInfo && userInfo.email}`,
      }
    )
  }

  const handleRemoveAccount = () => {
    console.log(removeAccountRequestOptions.body)
    fetch(config.serviceAPI.removeAccountURL, removeAccountRequestOptions)
      .then((res) => {
        if (!res.ok) {
          console.log(res)
          window.alert("Error")
          Promise.reject("")
          return
        }
        if (res.ok) {
          oktaAuth.signOut()
        }
      })
  }

  return (
    <div className="modal-content">
      <div className="confirmRemoveAccountModal">
        <h2>Delete account</h2>
        <label>
          Are you sure you want to delete your account?
        </label>
        <div className="buttonContainer">
          <button className="noButton" onClick={e => removeModal()}>
            No
          </button>
          <button className="yesButton" onClick={handleRemoveAccount}>
            Yes
          </button>
        </div>
      </div>
    </div>
  )
}

export default ConfirmRemoveAccountModal