{"size": 1215, "ext": "js", "max_stars_repo_path": "src/components/LogoutButton/LogoutButton.js", "max_stars_repo_name": "vincenttse15/chat", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/LogoutButton/LogoutButton.js", "max_issues_repo_name": "vincenttse15/chat", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/LogoutButton/LogoutButton.js", "max_forks_repo_name": "vincenttse15/chat", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.6136363636, "max_line_length": 65, "alphanum_fraction": 0.6567901235}
import React from 'react';
import * as styles from './logoutbutton.module.scss';
import { logout } from '../../redux/actions/userActions';
import { useDispatch } from 'react-redux';
import Cookies from 'universal-cookie';
import superagent from 'superagent';
import { API_URL } from '../../environment';
import { useHistory } from 'react-router';
import { library } from '@fortawesome/fontawesome-svg-core';
import { fas } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

const LogoutButton = (props) => {
  const dispatch = useDispatch();
  const cookies = new Cookies();
  const history = useHistory();
  library.add(fas);

  const {
    ws, 
    setWs,
  } = props;

  async function handleLogout() {
    const loggedOut = await superagent.post(`${API_URL}/logout`)
      .send({ sessionId: cookies.get('session') });

    if (loggedOut) {
      cookies.remove('session');
      dispatch(logout());
      history.push("/");
      ws.close();
      setWs(null);
    }
  }

  return (
    <button className={styles.button} onClick={handleLogout}>
      <FontAwesomeIcon icon={["fas", "sign-out-alt"]} />
    </button>
  );
}

export default LogoutButton;