Finished moving job totals calculation to server side BOD-267.

This commit is contained in:
Patrick Fic
2020-08-24 10:17:32 -07:00
parent 4b44fdfde5
commit 2acef8f726
18 changed files with 801 additions and 618 deletions

View File

@@ -0,0 +1,59 @@
import { Button, notification } from "antd";
import Axios from "axios";
import React, { useState } from "react";
import { useMutation } from "react-apollo";
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";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
export function JobCalculateTotals({ bodyshop, job }) {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [updateJob] = useMutation(UPDATE_JOB);
const handleCalculate = async () => {
setLoading(true);
const newTotals = (
await Axios.post("/job/totals", {
job: job,
shoprates: bodyshop.shoprates,
})
).data;
const result = await updateJob({
refetchQueries: ["GET_JOB_BY_PK"],
awaitRefetchQueries: true,
variables: {
jobId: job.id,
job: {
job_totals: newTotals,
},
},
});
if (!!!result.errors) {
notification["success"]({ message: t("jobs.successes.updated") });
} else {
notification["error"]({
message: t("jobs.errors.updating", {
error: JSON.stringify(result.errors),
}),
});
}
setLoading(false);
};
return (
<div>
<Button loading={loading} onClick={handleCalculate}>
{t("jobs.actions.recalculate")}
</Button>
</div>
);
}
export default connect(mapStateToProps, null)(JobCalculateTotals);