import { DeleteFilled } from "@ant-design/icons";
import { useMutation } from "@apollo/client";
import { Button, Popconfirm } from "antd";
import { antdMessage as message } from "../../../util/antdFeedback";
import React, { useState } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { DELETE_JOB } from "../../../graphql/jobs.queries";
import ipcTypes from "../../../ipc.types";
import { setSelectedJobId } from "../../../redux/application/application.actions";
const { ipcRenderer } = window;
const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser
});
const mapDispatchToProps = (dispatch) => ({
  setSelectedJobId: (id) => dispatch(setSelectedJobId(id)),
});

export function DeleteJobAtom({ setSelectedJobId, jobId }) {
  const [deleteJob] = useMutation(DELETE_JOB);
  const [loading, setLoading] = useState(false);
  const handleDelete = async () => {
    setLoading(true);
    ipcRenderer.send(ipcTypes.app.toMain.track, {
      event: "DELETE_JOB",
    });
    const result = await deleteJob({
      variables: { jobId: jobId },
      refetchQueries: ["QUERY_ALL_JOBS_PAGINATED"],
      awaitRefetchQueries: true,
    });

    if (result.errors) {
      message.error(result.errors.toString());
    } else {
      message.success("Job deleted.");
      setSelectedJobId(null);
    }
    setLoading(false);
  };

  return (
    <div>
      <Popconfirm
        title="Are you sure you want to delete this job? This cannot be undone."
        onConfirm={handleDelete}
      >
        <Button loading={loading}>
          <DeleteFilled /> <span>Delete</span>
        </Button>
      </Popconfirm>
    </div>
  );
}
export default connect(mapStateToProps, mapDispatchToProps)(DeleteJobAtom);
