Files
bodyshop/client/src/components/jobs-close-save-button/jobs-close-save-button.component.jsx

73 lines
1.8 KiB
JavaScript

import { useMutation } from "@apollo/react-hooks";
import { Button, notification } from "antd";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { UPDATE_JOB } from "../../graphql/jobs.queries";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { logImEXEvent } from "../../firebase/firebase.utils";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
export function JobsCloseSaveButton({
bodyshop,
suspenseAmount,
jobId,
labMatAllocations,
partsAllocations,
setInvoicedState,
disabled,
}) {
const [loading, setLoading] = useState(false);
const { t } = useTranslation();
const [updateJob] = useMutation(UPDATE_JOB);
const handleSave = async () => {
logImEXEvent("jobs_close_save");
setLoading(true);
const result = await updateJob({
variables: {
jobId: jobId,
job: {
date_invoiced: new Date(),
status: bodyshop.md_ro_statuses.default_invoiced || "Invoiced*",
invoice_allocation: {
labMatAllocations,
partsAllocations,
},
},
},
});
if (!!!result.errors) {
notification["success"]({ message: t("jobs.successes.invoiced") });
setInvoicedState(true);
} else {
notification["error"]({
message: t("jobs.errors.invoicing", {
error: JSON.stringify(result.errors),
}),
});
}
setLoading(false);
};
return (
<Button
onClick={handleSave}
type="primary"
disabled={suspenseAmount > 0 || disabled}
loading={loading}
>
{t("general.actions.save")}
</Button>
);
}
export default connect(mapStateToProps, null)(JobsCloseSaveButton);