%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/emergentqj/actioncivile/pre-auth/def/static/js/components/
Upload File :
Create Path :
Current File : /home/emergentqj/actioncivile/pre-auth/def/static/js/components/DropOff.jsx

import { useEffect } from "react";
import { node } from "prop-types";
import configFunc from "config/config";
import { postDataAction } from "common/actions";
import { isLocalhost } from "utils";
import { getUniversalCookies } from "../utils/cookies";

const { apiUrl } = configFunc();
let formData = {};

const isThereAnyUsableData = ({
  email,
  organisation,
  organizationnumber,
  phone,
}) => (
  email || organisation || organizationnumber || phone
);

const handleDropOff = () => {
  if (isLocalhost()) return;

  if (isThereAnyUsableData(formData)) {
    const country = (getUniversalCookies().get("epCountry") || "SE").toLowerCase();
    formData.countryCode = country;
    if (!formData.crossSellOption) {
      formData.crossSellOption = "BUSINESSACCOUNT";
    }
    postDataAction(
      `${apiUrl}lead/dropoff`,
      { ...formData },
      true,
    )();
    formData = {};
  }
};

let timeout = null;
const ABANDONED = 30 * 60 * 1000; // After half an hour

export const setDropOffables = (nextFormData) => {
  // Report dropOff after half an hour of the last change,
  // if the tab is still open and nothing happened
  timeout = setTimeout(() => {
    handleDropOff();
  }, ABANDONED);
  formData = nextFormData;
};

export const clearDropOffables = () => {
  clearTimeout(timeout);
  formData = {};
};

function DropOff({ children }) {
  // On window leave
  useEffect(() => {
    window.addEventListener("beforeunload", handleDropOff);
    return () => window.removeEventListener("beforeunload", handleDropOff);
  }, []);

  // On unmount ie close dialog
  useEffect(() => { /* eslint-disable-line arrow-body-style */
    return handleDropOff;
  }, []);

  return children;
}

DropOff.propTypes = {
  children: node.isRequired,
};

export default DropOff;

Zerion Mini Shell 1.0