# Conflicts: # _reference/reportFiltersAndSorters.md # client/src/components/bill-delete-button/bill-delete-button.component.jsx # client/src/components/bills-list-table/bills-list-table.component.jsx # client/src/components/courtesy-cars-list/courtesy-cars-list.component.jsx # client/src/components/jobs-close-export-button/jobs-close-export-button.component.jsx # client/src/components/jobs-detail-header-actions/jobs-detail-header-actions.component.jsx # client/src/components/jobs-export-all-button/jobs-export-all-button.component.jsx # client/src/components/report-center-modal/report-center-modal-filters-sorters-component.jsx # client/src/components/scoreboard-day-stats/scoreboard-day-stats.component.jsx # client/src/components/scoreboard-targets-table/scoreboard-targets-table.component.jsx # client/src/pages/dms/dms.container.jsx # client/src/redux/application/application.sagas.js # client/src/translations/en_us/common.json # client/src/translations/es/common.json # client/src/translations/fr/common.json # client/src/utils/AuditTrailMappings.js # client/src/utils/graphQLmodifier.js # package-lock.json # package.json
97 lines
3.5 KiB
JavaScript
97 lines
3.5 KiB
JavaScript
import {DeleteFilled} from "@ant-design/icons";
|
|
import {useMutation} from "@apollo/client";
|
|
import {Button, notification, Popconfirm} from "antd";
|
|
import React, {useState} from "react";
|
|
import {useTranslation} from "react-i18next";
|
|
import {DELETE_BILL} from "../../graphql/bills.queries";
|
|
import RbacWrapper from "../rbac-wrapper/rbac-wrapper.component";
|
|
import {insertAuditTrail} from "../../redux/application/application.actions";
|
|
import AuditTrailMapping from "../../utils/AuditTrailMappings";
|
|
import { connect } from "react-redux";
|
|
import { createStructuredSelector } from "reselect";
|
|
|
|
const mapStateToProps = createStructuredSelector({});
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
insertAuditTrail: ({ jobid, operation }) =>
|
|
dispatch(insertAuditTrail({ jobid, operation })),
|
|
});
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(BillDeleteButton);
|
|
|
|
export function BillDeleteButton({ bill, jobid, callback, insertAuditTrail }) {
|
|
const [loading, setLoading] = useState(false);
|
|
const { t } = useTranslation();
|
|
const [deleteBill] = useMutation(DELETE_BILL);
|
|
|
|
const handleDelete = async () => {
|
|
setLoading(true);
|
|
const result = await deleteBill({
|
|
variables: {billId: bill.id},
|
|
update(cache, {errors}) {
|
|
if (errors) return;
|
|
cache.modify({
|
|
fields: {
|
|
bills(existingBills, {readField}) {
|
|
return existingBills.filter(
|
|
(billref) => bill.id !== readField("id", billref)
|
|
);
|
|
},
|
|
search_bills(existingBills, {readField}) {
|
|
return existingBills.filter(
|
|
(billref) => bill.id !== readField("id", billref)
|
|
);
|
|
},
|
|
},
|
|
});
|
|
},
|
|
});
|
|
|
|
if (!!!result.errors) {
|
|
notification["success"]({ message: t("bills.successes.deleted") });
|
|
insertAuditTrail({
|
|
jobid: jobid,
|
|
operation: AuditTrailMapping.billdeleted(bill.invoice_number),
|
|
});
|
|
|
|
if (callback && typeof callback === "function") callback(bill.id);
|
|
} else {
|
|
//Check if it's an fkey violation.
|
|
const error = JSON.stringify(result.errors);
|
|
|
|
if (error.toLowerCase().includes("inventory_billid_fkey")) {
|
|
notification["error"]({
|
|
message: t("bills.errors.deleting", {
|
|
error: t("bills.errors.existinginventoryline"),
|
|
}),
|
|
});
|
|
} else {
|
|
notification["error"]({
|
|
message: t("bills.errors.deleting", {
|
|
error: JSON.stringify(result.errors),
|
|
}),
|
|
});
|
|
}
|
|
}
|
|
|
|
setLoading(false);
|
|
};
|
|
|
|
return (
|
|
<RbacWrapper action="bills:delete" noauth={<></>}>
|
|
<Popconfirm
|
|
disabled={bill.exported}
|
|
onConfirm={handleDelete}
|
|
title={t("bills.labels.deleteconfirm")}
|
|
>
|
|
<Button
|
|
disabled={bill.exported}
|
|
// onClick={handleDelete}
|
|
loading={loading}
|
|
>
|
|
<DeleteFilled/>
|
|
</Button>
|
|
</Popconfirm>
|
|
</RbacWrapper>
|
|
);
|
|
}
|