101 lines
3.4 KiB
JavaScript
101 lines
3.4 KiB
JavaScript
import { QuestionCircleOutlined } from "@ant-design/icons";
|
|
import { useMutation } from "@apollo/client";
|
|
import { Button, notification, Popconfirm } from "antd";
|
|
import axios from "axios";
|
|
import React, { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { logImEXEvent } from "../../firebase/firebase.utils";
|
|
import { DELETE_DOCUMENT } from "../../graphql/documents.queries";
|
|
import cleanAxios from "../../utils/CleanAxios";
|
|
import { DetermineFileType } from "../documents-upload/documents-upload.utility";
|
|
//Context: currentUserEmail, bodyshop, jobid, invoiceid
|
|
|
|
export default function JobsDocumentsDeleteButton({
|
|
galleryImages,
|
|
deletionCallback,
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const [deleteDocument] = useMutation(DELETE_DOCUMENT);
|
|
const imagesToDelete = [
|
|
...galleryImages.images.filter((image) => image.isSelected),
|
|
...galleryImages.other.filter((image) => image.isSelected),
|
|
];
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const handleDelete = () => {
|
|
logImEXEvent("job_documents_delete", { count: imagesToDelete.length });
|
|
setLoading(true);
|
|
imagesToDelete.forEach((image) => {
|
|
let timestamp = Math.floor(Date.now() / 1000);
|
|
let public_id = image.key;
|
|
|
|
axios
|
|
.post("/media/sign", {
|
|
public_id: public_id,
|
|
timestamp: timestamp,
|
|
})
|
|
.then((response) => {
|
|
var signature = response.data;
|
|
var options = {
|
|
headers: { "X-Requested-With": "XMLHttpRequest" },
|
|
};
|
|
const formData = new FormData();
|
|
formData.append("api_key", process.env.REACT_APP_CLOUDINARY_API_KEY);
|
|
formData.append("public_id", public_id);
|
|
formData.append("timestamp", timestamp);
|
|
formData.append("signature", signature);
|
|
|
|
cleanAxios
|
|
.post(
|
|
`${
|
|
process.env.REACT_APP_CLOUDINARY_ENDPOINT_API
|
|
}/${DetermineFileType(image.type)}/destroy`,
|
|
formData,
|
|
options
|
|
)
|
|
.then((response) => {
|
|
deleteDocument({ variables: { id: image.id } })
|
|
.then((r) => {
|
|
notification["success"]({
|
|
message: t("documents.successes.delete"),
|
|
});
|
|
if (deletionCallback) deletionCallback();
|
|
})
|
|
.catch((error) => {
|
|
notification["error"]({
|
|
message: t("documents.errors.deleting", {
|
|
message: JSON.stringify(error),
|
|
}),
|
|
});
|
|
});
|
|
//Delete it from our database.
|
|
})
|
|
.catch((error) => {
|
|
notification["error"]({
|
|
message: t("documents.errors.deleting_cloudinary", {
|
|
message: JSON.stringify(error),
|
|
}),
|
|
});
|
|
});
|
|
});
|
|
});
|
|
setLoading(false);
|
|
};
|
|
|
|
return (
|
|
<Popconfirm
|
|
disabled={imagesToDelete.length < 1}
|
|
icon={<QuestionCircleOutlined style={{ color: "red" }} />}
|
|
onConfirm={handleDelete}
|
|
title={t("documents.labels.confirmdelete")}
|
|
okText={t("general.actions.delete")}
|
|
okButtonProps={{ type: "danger" }}
|
|
cancelText={t("general.actions.cancel")}
|
|
>
|
|
<Button disabled={imagesToDelete.length < 1} loading={loading}>
|
|
{t("documents.actions.delete")}
|
|
</Button>
|
|
</Popconfirm>
|
|
);
|
|
}
|