From 9cce792d72846efdcef21ca7f7f6d23db854b184 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Tue, 16 Jun 2020 11:28:41 -0700 Subject: [PATCH] Implemented rescheduling functionality on event BOD-167 --- .../schedule-event.component.jsx | 37 ++++++++++-- .../schedule-event.container.jsx | 57 +++++++++++++------ .../schedule-job-modal.container.jsx | 28 ++++++++- client/src/translations/en_us/common.json | 2 +- client/src/translations/es/common.json | 2 +- client/src/translations/fr/common.json | 2 +- 6 files changed, 99 insertions(+), 29 deletions(-) diff --git a/client/src/components/schedule-event/schedule-event.component.jsx b/client/src/components/schedule-event/schedule-event.component.jsx index e1c024a8d..e4fcd3abc 100644 --- a/client/src/components/schedule-event/schedule-event.component.jsx +++ b/client/src/components/schedule-event/schedule-event.component.jsx @@ -5,8 +5,21 @@ import { Link } from "react-router-dom"; import CurrencyFormatter from "../../utils/CurrencyFormatter"; import PhoneFormatter from "../../utils/PhoneFormatter"; import DataLabel from "../data-label/data-label.component"; +import { createStructuredSelector } from "reselect"; +import { setModalContext } from "../../redux/modals/modals.actions"; +import { connect } from "react-redux"; -export default function ScheduleEventComponent({ event, handleCancel }) { +const mapDispatchToProps = (dispatch) => ({ + setScheduleContext: (context) => + dispatch(setModalContext({ context: context, modal: "schedule" })), +}); + +export function ScheduleEventComponent({ + event, + refetch, + handleCancel, + setScheduleContext, +}) { const { t } = useTranslation(); const popoverContent = (
@@ -52,7 +65,7 @@ export default function ScheduleEventComponent({ event, handleCancel }) {
) : null} -
+
{event.job ? ( @@ -61,7 +74,19 @@ export default function ScheduleEventComponent({ event, handleCancel }) { - {event.isintake ? ( @@ -69,7 +94,8 @@ export default function ScheduleEventComponent({ event, handleCancel }) { to={{ pathname: `/manage/jobs/${event.job && event.job.id}/intake`, search: `?appointmentId=${event.id}`, - }}> + }} + > @@ -97,8 +123,9 @@ export default function ScheduleEventComponent({ event, handleCancel }) { ); return ( - + {RegularEvent} ); } +export default connect(null, mapDispatchToProps)(ScheduleEventComponent); diff --git a/client/src/components/schedule-event/schedule-event.container.jsx b/client/src/components/schedule-event/schedule-event.container.jsx index c35c03e54..ffb910496 100644 --- a/client/src/components/schedule-event/schedule-event.container.jsx +++ b/client/src/components/schedule-event/schedule-event.container.jsx @@ -10,27 +10,48 @@ export default function ScheduleEventContainer({ event, refetch }) { const [cancelAppointment] = useMutation(CANCEL_APPOINTMENT_BY_ID); const [updateJob] = useMutation(UPDATE_JOB); const handleCancel = async (id) => { - try { - await cancelAppointment({ variables: { appid: event.id } }); - notification["success"]({ - message: t("appointments.successes.canceled"), - }); + const cancelAppt = await cancelAppointment({ + variables: { appid: event.id }, + }); + notification["success"]({ + message: t("appointments.successes.canceled"), + }); - await updateJob({ - variables: { - jobId: event.job.id, - job: { - date_scheduled: null, - scheduled_in: null, - }, - }, + if (!!cancelAppt.errors) { + notification["error"]({ + message: t("appointments.errors.canceling", { + message: JSON.stringify(cancelAppt.errors), + }), }); - - if (refetch) refetch(); - } catch (error) { - notification["error"]({ message: t("appointments.errors.canceling") }); + return; } + + const jobUpdate = await updateJob({ + variables: { + jobId: event.job.id, + job: { + date_scheduled: null, + scheduled_in: null, + }, + }, + }); + if (!!jobUpdate.errors) { + notification["error"]({ + message: t("jobs.errors.updating", { + message: JSON.stringify(jobUpdate.errors), + }), + }); + return; + } + + if (refetch) refetch(); }; - return ; + return ( + + ); } diff --git a/client/src/components/schedule-job-modal/schedule-job-modal.container.jsx b/client/src/components/schedule-job-modal/schedule-job-modal.container.jsx index cab977d4e..fd21c5726 100644 --- a/client/src/components/schedule-job-modal/schedule-job-modal.container.jsx +++ b/client/src/components/schedule-job-modal/schedule-job-modal.container.jsx @@ -3,6 +3,7 @@ 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"; @@ -32,13 +33,13 @@ export function ScheduleJobModalContainer({ setEmailOptions, }) { const { visible, context, actions } = scheduleModal; - const { jobId, job } = context; + const { jobId, job, previousEvent } = context; const { refetch } = actions; const [appData, setAppData] = useState({ start: null, }); - + const [cancelAppointment] = useMutation(CANCEL_APPOINTMENT_BY_ID); const [insertAppointment] = useMutation(INSERT_APPOINTMENT); const [updateJobStatus] = useMutation(UPDATE_JOBS); const [formData, setFormData] = useState({ @@ -52,7 +53,10 @@ export function ScheduleJobModalContainer({ email: (job && job.ownr_ea) || "", start: null, }); - }, [job, setFormData]); + setAppData({ + start: null, + }); + }, [job, setFormData, setAppData]); const { t } = useTranslation(); @@ -64,6 +68,24 @@ export function ScheduleJobModalContainer({ //TODO Customize the amount of minutes it will add. const handleOk = async () => { + 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: { diff --git a/client/src/translations/en_us/common.json b/client/src/translations/en_us/common.json index 92c6b5948..800b6f1e4 100644 --- a/client/src/translations/en_us/common.json +++ b/client/src/translations/en_us/common.json @@ -27,7 +27,7 @@ "viewjob": "View Job" }, "errors": { - "canceling": "Error canceling appointment.", + "canceling": "Error canceling appointment. {{message}}", "saving": "Error scheduling appointment. {{message}}" }, "fields": { diff --git a/client/src/translations/es/common.json b/client/src/translations/es/common.json index 9a49572c2..47b6c191a 100644 --- a/client/src/translations/es/common.json +++ b/client/src/translations/es/common.json @@ -27,7 +27,7 @@ "viewjob": "Ver trabajo" }, "errors": { - "canceling": "Error al cancelar la cita.", + "canceling": "Error al cancelar la cita. {{message}}", "saving": "Error al programar la cita. {{message}}" }, "fields": { diff --git a/client/src/translations/fr/common.json b/client/src/translations/fr/common.json index c98a1917d..9d1fd16ce 100644 --- a/client/src/translations/fr/common.json +++ b/client/src/translations/fr/common.json @@ -27,7 +27,7 @@ "viewjob": "Voir le travail" }, "errors": { - "canceling": "Erreur lors de l'annulation du rendez-vous.", + "canceling": "Erreur lors de l'annulation du rendez-vous. {{message}}", "saving": "Erreur lors de la planification du rendez-vous. {{message}}" }, "fields": {