Files
bodyshop/client/src/components/parts-order-cm-received/parts-order-cm-received.component.jsx

65 lines
1.8 KiB
JavaScript

import { useMutation } from "@apollo/client/react";
import { Checkbox, Space, Spin } from "antd";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { MUTATION_UPDATE_PO_CM_REECEIVED } from "../../graphql/parts-orders.queries";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
export default function PartsOrderCmReceived({ checked, orderLineId, partsOrderId }) {
const [updateLine] = useMutation(MUTATION_UPDATE_PO_CM_REECEIVED);
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const notification = useNotification();
const handleChange = async (e) => {
setLoading(true);
const result = await updateLine({
variables: {
partsLineId: orderLineId,
partsOrder: { cm_received: e.target.checked }
},
update(cache) {
cache.modify({
id: cache.identify({
id: partsOrderId,
__typename: "parts_orders"
}),
fields: {
parts_order_lines(ex, { readField }) {
console.log(ex);
return ex.map((lineref) => {
if (orderLineId.id !== readField("id", lineref)) {
lineref.cm_received = e.target.checked;
}
return lineref;
});
}
}
});
}
});
if (!result.errors) {
notification.success({
title: t("parts_orders.successes.line_updated")
});
} else {
notification.error({
title: t("parts_orders.errors.saving", {
error: JSON.stringify(result.errors)
})
});
}
setLoading(false);
};
return (
<Space>
<Checkbox checked={checked} onChange={handleChange} />
{loading && <Spin size="small" />}
</Space>
);
}