import { useMutation } from "@apollo/client";
import { Switch } from "antd";
import { antdMessage as message } from "../../../util/antdFeedback";
import React, { useState } from "react";
import { UPDATE_JOB_LINE } from "../../../graphql/joblines.queries";
import ipcTypes from "../../../ipc.types";
const { log, ipcRenderer } = window;

export default function IgnoreJobLineAtom({ ignore, lineId, line_desc }) {
  const [updateJobLine] = useMutation(UPDATE_JOB_LINE);
  const [loading, setLoading] = useState(false);
  const handleChange = async (checked) => {
    console.log("handleChange -> checked", checked);
    setLoading(true);
    ipcRenderer.send(ipcTypes.app.toMain.track, {
      event: "TOGGLE_IGNORE_LINE",
      line_desc: line_desc,
      ignore: !checked,
    });

    const result = await updateJobLine({
      variables: { lineId: lineId, line: { ignore: !checked } },
    });
    if (result.errors) {
      message.error("Error updating line.");
      log.error("Error updating job.", result.errors);
    } else {
    }
    setLoading(false);
  };

  return <Switch checked={!ignore} onChange={handleChange} loading={loading} />;
}
