Files
bodyshop/client/src/components/job-create-iou/job-create-iou.component.jsx
Dave Richer 1718ba5695 - merge
Signed-off-by: Dave Richer <dave@imexsystems.ca>
2024-01-19 12:29:04 -05:00

99 lines
3.4 KiB
JavaScript

import {useApolloClient} from "@apollo/client";
import {useSplitTreatments} from "@splitsoftware/splitio-react";
import {Button, notification, Popconfirm} from "antd";
import React, {useState} from "react";
import {useTranslation} from "react-i18next";
import {connect} from "react-redux";
import {useNavigate} from "react-router-dom";
import {createStructuredSelector} from "reselect";
import {UPDATE_JOB_LINES_IOU} from "../../graphql/jobs-lines.queries";
import {selectBodyshop, selectCurrentUser,} from "../../redux/user/user.selectors";
import {CreateIouForJob} from "../jobs-detail-header-actions/jobs-detail-header-actions.duplicate.util";
import {selectTechnician} from "../../redux/tech/tech.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
currentUser: selectCurrentUser,
technician: selectTechnician,
});
const mapDispatchToProps = (dispatch) => ({
//setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(JobCreateIOU);
export function JobCreateIOU({bodyshop, currentUser, job, selectedJobLines, technician}) {
const {t} = useTranslation();
const [loading, setLoading] = useState(false);
const client = useApolloClient();
const history = useNavigate();
const {treatments: {IOU_Tracking}} = useSplitTreatments({
attributes: {},
names: ["IOU_Tracking"],
splitKey: bodyshop.imexshopid,
});
if (IOU_Tracking.treatment !== "on") return null;
const handleCreateIou = async () => {
setLoading(true);
//Query all of the job details to recreate.
const iouId = await CreateIouForJob(
client,
job.id,
{
status: bodyshop.md_ro_statuses.default_open,
bodyshopid: bodyshop.id,
useremail: currentUser.email,
},
selectedJobLines
);
notification.open({
type: "success",
message: t("jobs.successes.ioucreated"),
onClick: () => history(`/manage/jobs/${iouId}`),
});
const selectedJobLinesIds = selectedJobLines.map((l) => l.id);
await client.mutate({
mutation: UPDATE_JOB_LINES_IOU,
variables: {ids: selectedJobLinesIds},
update(cache) {
cache.modify({
id: cache.identify(job.id),
fields: {
joblines(existingJobLines, {readField}) {
return existingJobLines.map((a) => {
if (!selectedJobLinesIds.includes(a.id)) return a;
return {...a, ioucreated: true};
});
},
},
});
},
});
setLoading(false);
};
return (
<Popconfirm
title={t("jobs.labels.createiouwarning")}
onConfirm={handleCreateIou}
disabled={
!selectedJobLines || selectedJobLines.length === 0 || !job.converted || technician
}
>
<Button
loading={loading}
disabled={
!selectedJobLines || selectedJobLines.length === 0 || !job.converted || technician
}
>
{t("jobs.actions.createiou")}
</Button>
</Popconfirm>
);
}