import { DownCircleFilled } from "@ant-design/icons"; import { useApolloClient, useMutation } from "@apollo/client"; import { Button, Dropdown, Menu, notification, Popconfirm } from "antd"; import React, { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { connect } from "react-redux"; import { Link, useHistory } from "react-router-dom"; import { createStructuredSelector } from "reselect"; import { logImEXEvent } from "../../firebase/firebase.utils"; import { DELETE_JOB, UPDATE_JOB, VOID_JOB } from "../../graphql/jobs.queries"; import { selectJobReadOnly } from "../../redux/application/application.selectors"; import { setModalContext } from "../../redux/modals/modals.actions"; import { selectBodyshop, selectCurrentUser, } from "../../redux/user/user.selectors"; import AddToProduction from "./jobs-detail-header-actions.addtoproduction.util"; import JobsDetaiLheaderCsi from "./jobs-detail-header-actions.csi.component"; import DuplicateJob from "./jobs-detail-header-actions.duplicate.util"; const mapStateToProps = createStructuredSelector({ bodyshop: selectBodyshop, jobRO: selectJobReadOnly, currentUser: selectCurrentUser, }); const mapDispatchToProps = (dispatch) => ({ setScheduleContext: (context) => dispatch(setModalContext({ context: context, modal: "schedule" })), setBillEnterContext: (context) => dispatch(setModalContext({ context: context, modal: "billEnter" })), setPaymentContext: (context) => dispatch(setModalContext({ context: context, modal: "payment" })), setJobCostingContext: (context) => dispatch(setModalContext({ context: context, modal: "jobCosting" })), setTimeTicketContext: (context) => dispatch(setModalContext({ context: context, modal: "timeTicket" })), }); export function JobsDetailHeaderActions({ job, bodyshop, currentUser, refetch, setScheduleContext, setBillEnterContext, setPaymentContext, setJobCostingContext, jobRO, setTimeTicketContext, }) { const { t } = useTranslation(); const client = useApolloClient(); const history = useHistory(); const [deleteJob] = useMutation(DELETE_JOB); const [updateJob] = useMutation(UPDATE_JOB); const [voidJob] = useMutation(VOID_JOB); const jobInProduction = useMemo(() => { return bodyshop.md_ro_statuses.production_statuses.includes(job.status); }, [job, bodyshop.md_ro_statuses.production_statuses]); const jobInPreProduction = useMemo(() => { return bodyshop.md_ro_statuses.pre_production_statuses.includes(job.status); }, [job.status, bodyshop.md_ro_statuses.pre_production_statuses]); const jobInPostProduction = useMemo(() => { return bodyshop.md_ro_statuses.post_production_statuses.includes( job.status ); }, [job.status, bodyshop.md_ro_statuses.post_production_statuses]); const handleAlertToggle = (e) => { logImEXEvent("production_toggle_alert"); //e.stopPropagation(); updateJob({ variables: { jobId: job.id, job: { production_vars: { ...job.production_vars, alert: !!job.production_vars && !!job.production_vars.alert ? !job.production_vars.alert : true, }, }, }, }); }; const statusmenu = ( { logImEXEvent("job_header_schedule"); setScheduleContext({ actions: { refetch: refetch }, context: { jobId: job.id, job: job, }, }); }} > {t("jobs.actions.schedule")} {!!job.intakechecklist || !jobInPreProduction || !job.converted || jobRO ? ( t("jobs.actions.intake") ) : ( {t("jobs.actions.intake")} )} {!jobInProduction ? ( t("jobs.actions.deliver") ) : ( {t("jobs.actions.deliver")} )} {t("jobs.actions.viewchecklist")} { logImEXEvent("job_header_enter_time_ticekts"); setTimeTicketContext({ actions: {}, context: { jobId: job.id }, }); }} > {t("timetickets.actions.enter")} { logImEXEvent("job_header_enter_payment"); setPaymentContext({ actions: {}, context: { jobid: job.id }, }); }} > {t("menus.header.enterpayment")} {t("menus.jobsactions.newcccontract")} {job.inproduction ? ( AddToProduction(client, job.id, refetch, true)} > {t("jobs.actions.removefromproduction")} ) : ( AddToProduction(client, job.id, refetch)} > {t("jobs.actions.addtoproduction")} )} {job.production_vars && job.production_vars.alert ? t("production.labels.alertoff") : t("production.labels.alerton")} e.stopPropagation()} onConfirm={() => DuplicateJob( client, job.id, { defaultOpenStatus: bodyshop.md_ro_statuses.default_imported }, (newJobId) => { history.push(`/manage/jobs/${newJobId}`); notification["success"]({ message: t("jobs.successes.duplicated"), }); }, true ) } getPopupContainer={(trigger) => trigger.parentNode} > {t("menus.jobsactions.duplicate")} e.stopPropagation()} onConfirm={() => DuplicateJob( client, job.id, { defaultOpenStatus: bodyshop.md_ro_statuses.default_imported }, (newJobId) => { history.push(`/manage/jobs/${newJobId}`); notification["success"]({ message: t("jobs.successes.duplicated"), }); } ) } getPopupContainer={(trigger) => trigger.parentNode} > {t("menus.jobsactions.duplicatenolines")} { logImEXEvent("job_header_enter_bills"); setBillEnterContext({ actions: { refetch: refetch }, context: { job: job, }, }); }} > {t("jobs.actions.postbills")} { const result = await updateJob({ variables: { jobId: job.id, job: { queued_for_parts: true }, }, }); if (!!!result.errors) { notification["success"]({ message: t("jobs.successes.partsqueue"), }); } else { notification["error"]({ message: t("jobs.errors.saving", { error: JSON.stringify(result.errors), }), }); } }} > {t("jobs.actions.addtopartsqueue")} {!jobInPostProduction ? ( t("menus.jobsactions.closejob") ) : ( {t("menus.jobsactions.closejob")} )} {t("menus.jobsactions.admin")} { logImEXEvent("job_header_job_costing"); setJobCostingContext({ actions: { refetch: refetch }, context: { jobId: job.id, }, }); }} > {t("jobs.labels.jobcosting")} {job && !job.converted && ( e.stopPropagation()} onConfirm={async () => { //delete the job. const result = await deleteJob({ variables: { id: job.id } }); if (!!!result.errors) { notification["success"]({ message: t("jobs.successes.delete"), }); //go back to jobs list. history.push(`/manage/`); } else { notification["error"]({ message: t("jobs.errors.deleted", { error: JSON.stringify(result.errors), }), }); } }} getPopupContainer={(trigger) => trigger.parentNode} > {t("menus.jobsactions.deletejob")} )} {!jobRO && job.converted && ( e.stopPropagation()} onConfirm={async () => { //delete the job. const result = await voidJob({ variables: { jobId: job.id, job: { status: bodyshop.md_ro_statuses.default_void, voided: true, }, note: [ { jobid: job.id, created_by: currentUser.email, audit: true, text: t("jobs.labels.voidnote"), }, ], }, }); if (!!!result.errors) { notification["success"]({ message: t("jobs.successes.voided"), }); //go back to jobs list. history.push(`/manage/`); } else { notification["error"]({ message: t("jobs.errors.voiding", { error: JSON.stringify(result.errors), }), }); } }} getPopupContainer={(trigger) => trigger.parentNode} > {t("menus.jobsactions.void")} )} ); return ( ); } export default connect( mapStateToProps, mapDispatchToProps )(JobsDetailHeaderActions);