Files
bodyshop/client/src/components/schedule-job-modal/schedule-job-modal.container.jsx
2020-06-24 11:20:27 -07:00

176 lines
4.9 KiB
JavaScript

import React, { useState, useEffect } from "react";
import ScheduleJobModalComponent from "./schedule-job-modal.component";
import { useMutation, useQuery } from "@apollo/react-hooks";
import {
INSERT_APPOINTMENT,
CANCEL_APPOINTMENT_BY_ID,
QUERY_APPOINTMENTS_BY_JOBID,
} from "../../graphql/appointments.queries";
import moment from "moment";
import { notification, Modal } from "antd";
import { useTranslation } from "react-i18next";
import { UPDATE_JOBS } from "../../graphql/jobs.queries";
import { setEmailOptions } from "../../redux/email/email.actions";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { selectSchedule } from "../../redux/modals/modals.selectors";
import { toggleModalVisible } from "../../redux/modals/modals.actions";
import { TemplateList } from "../../utils/TemplateConstants";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
scheduleModal: selectSchedule,
});
const mapDispatchToProps = (dispatch) => ({
toggleModalVisible: () => dispatch(toggleModalVisible("schedule")),
setEmailOptions: (e) => dispatch(setEmailOptions(e)),
});
export function ScheduleJobModalContainer({
scheduleModal,
bodyshop,
toggleModalVisible,
setEmailOptions,
}) {
const { visible, context, actions } = scheduleModal;
const { jobId, job, previousEvent } = context;
const { refetch } = actions;
const [loading, setLoading] = useState(false);
const [appData, setAppData] = useState({
notifyCustomer: !!(job && job.ownr_ea),
email: (job && job.ownr_ea) || "",
start: null,
smartDates: [],
});
const [cancelAppointment] = useMutation(CANCEL_APPOINTMENT_BY_ID);
const [insertAppointment] = useMutation(INSERT_APPOINTMENT);
const [updateJobStatus] = useMutation(UPDATE_JOBS);
useEffect(() => {
setAppData({
notifyCustomer: !!(job && job.ownr_ea),
email: (job && job.ownr_ea) || "",
start: null,
smartDates: [],
});
}, [job, setAppData]);
const { t } = useTranslation();
const existingAppointments = useQuery(QUERY_APPOINTMENTS_BY_JOBID, {
variables: { jobid: jobId },
fetchPolicy: "network-only",
skip: !visible || !!!jobId,
});
//TODO Customize the amount of minutes it will add.
const handleOk = async () => {
setLoading(true);
if (!!previousEvent) {
const cancelAppt = await cancelAppointment({
variables: { appid: previousEvent },
});
notification["success"]({
message: t("appointments.successes.canceled"),
});
if (!!cancelAppt.errors) {
notification["error"]({
message: t("appointments.errors.canceling", {
message: JSON.stringify(cancelAppt.errors),
}),
});
return;
}
}
const appt = await insertAppointment({
variables: {
app: {
//...appData,
jobid: jobId,
bodyshopid: bodyshop.id,
start: moment(appData.start),
end: moment(appData.start).add(bodyshop.appt_length || 60, "minutes"),
},
},
});
if (!!appt.errors) {
notification["error"]({
message: t("appointments.errors.saving", {
message: JSON.stringify(appt.errors),
}),
});
return;
}
notification["success"]({
message: t("appointments.successes.created"),
});
if (jobId) {
const jobUpdate = await updateJobStatus({
variables: {
jobIds: [jobId],
fields: {
status: bodyshop.md_ro_statuses.default_scheduled,
date_scheduled: new Date(),
scheduled_in: appData.start,
},
},
});
if (!!jobUpdate.errors) {
notification["error"]({
message: t("appointments.errors.saving", {
message: JSON.stringify(jobUpdate.errors),
}),
});
return;
}
}
setLoading(false);
toggleModalVisible();
if (appData.notifyCustomer) {
setEmailOptions({
messageOptions: {
to: appData.email,
replyTo: bodyshop.email,
},
template: {
name: TemplateList.appointment_confirmation.key,
variables: {
id: appt.data.insert_appointments.returning[0].id,
},
},
});
}
if (refetch) refetch();
};
return (
<Modal
visible={visible}
onCancel={() => toggleModalVisible()}
onOk={handleOk}
width={"90%"}
maskClosable={false}
destroyOnClose
okButtonProps={{
disabled: appData.start ? false : true,
loading: loading,
}}
>
<ScheduleJobModalComponent
existingAppointments={existingAppointments}
appData={appData}
setAppData={setAppData}
/>
</Modal>
);
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(ScheduleJobModalContainer);