import React, { useEffect, useState } from "react";
import { authApi } from "../api/api";
import Loader from "../components/utils/loader";

const Logout = () => {
  const [loader, setLoader] = useState();
  const onLogout = async () => {
    setLoader(true);
    await authApi
      .post(
        "/customer/logout",
        {},
        {
          withCredentials: true,
        }
      )
      .then(() => {
        setLoader(false);
        window.location.href = "/";
      })
      .catch(() => {
        setLoader(false);
        window.location.href = "/";
      });
  };

  useEffect(() => {
    onLogout();
  }, []);

  return loader && <Loader className={`pageLoader`} />;
};

export default Logout;
