- Fix console warn, add two missing try catch blocks

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-04-11 22:15:48 -04:00
parent d7f946ec2a
commit de486d2e73
2 changed files with 79 additions and 55 deletions

View File

@@ -176,7 +176,7 @@ export function TaskUpsertModalComponent({
label={t("tasks.fields.assigned_to")}
name="assigned_to"
initialValue={
bodyshop.employees.find((employee) => employee.user_email === currentUser.email && employee.active)
bodyshop.employees.find((employee) => employee?.user_email === currentUser.email && employee.active)
? currentUser.email
: undefined
}
@@ -189,7 +189,7 @@ export function TaskUpsertModalComponent({
<Select
placeholder={t("tasks.placeholders.assigned_to")}
options={bodyshop.employees
.filter((x) => x.active)
.filter((x) => x.active && x.user_email)
.map((employee) => ({
key: employee.id,
value: employee.user_email,

View File

@@ -11,9 +11,7 @@ import { selectTaskUpsert } from "../../redux/modals/modals.selectors";
import { selectBodyshop, selectCurrentUser } from "../../redux/user/user.selectors";
import TaskUpsertModalComponent from "./task-upsert-modal.component";
import { replaceUndefinedWithNull } from "../../utils/undefinedtonull.js";
import { useNavigate } from "react-router-dom";
import axios from "axios";
import dayjs from "../../utils/day";
import { useLocation, useNavigate } from "react-router-dom";
import { insertAuditTrail } from "../../redux/application/application.actions.js";
import AuditTrailMapping from "../../utils/AuditTrailMappings.js";
import { isEqual } from "lodash";
@@ -42,6 +40,7 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
const [selectedJobDetails, setSelectedJobDetails] = useState(null);
const [jobIdState, setJobIdState] = useState(null);
const [isTouched, setIsTouched] = useState(false);
const location = useLocation();
const { loading, error, data } = useQuery(QUERY_GET_TASKS_JOB_DETAILS_BY_ID, {
variables: { id: jobIdState },
@@ -108,6 +107,15 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
history(`${window.location.pathname}?${urlParams}`);
};
/**
* Task Query Refetch function to determine if we need to refetch the task query
* @returns {boolean}
*/
const taskQueryRefetch = () =>
(!jobid || !joblineid || !billid || !partsorderid || !taskId) &&
taskQuery &&
location.pathname.includes("/manage/tasks");
/**
* Handle existing task
* @param values
@@ -135,7 +143,7 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
// We have a relationship query, so we need to refetch the query
if (query && Object.keys(query).length) {
taskObject.refetchQueries.push(Object.keys(query)[0]);
} else if (taskQuery) {
} else if (taskQueryRefetch()) {
taskObject.refetchQueries.push(taskQuery);
}
@@ -151,29 +159,29 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
});
}
if (isAssignedToDirty) {
// TODO This is being moved serverside
axios
.post("/sendemail", {
from: {
name: bodyshop.shopname,
address: bodyshop.email
},
ReplyTo: {
Email: "noreply@imex.online"
},
to: values.assigned_to,
subject: `A Task has been re-assigned to you on ${bodyshop.shopname} - ${values.title}`,
templateStrings: {
header: values.title,
subHeader: `Assigned by ${currentUser.email} ${values.due_at ? `| Due on ${dayjs(values.due_at).format("MM/DD/YYYY")}` : ""}`,
body: `<a href="${window.location.protocol}//${window.location.host}/manage/tasks/alltasks?taskid=${existingTask.id}">Please sign in to your account to view the task details.</a>`
}
})
.catch((e) =>
console.error(`Something went wrong sending email to Assigned party on Task creation. ${e.message || ""}`)
);
}
// if (isAssignedToDirty) {
// // TODO This is being moved serverside
// axios
// .post("/sendemail", {
// from: {
// name: bodyshop.shopname,
// address: bodyshop.email
// },
// ReplyTo: {
// Email: "noreply@imex.online"
// },
// to: values.assigned_to,
// subject: `A Task has been re-assigned to you on ${bodyshop.shopname} - ${values.title}`,
// templateStrings: {
// header: values.title,
// subHeader: `Assigned by ${currentUser.email} ${values.due_at ? `| Due on ${dayjs(values.due_at).format("MM/DD/YYYY")}` : ""}`,
// body: `<a href="${window.location.protocol}//${window.location.host}/manage/tasks/alltasks?taskid=${existingTask.id}">Please sign in to your account to view the task details.</a>`
// }
// })
// .catch((e) =>
// console.error(`Something went wrong sending email to Assigned party on Task creation. ${e.message || ""}`)
// );
// }
notification["success"]({
message: t("tasks.successes.updated")
@@ -182,6 +190,11 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
toggleModalVisible();
};
/**
* Handle new task
* @param values
* @returns {Promise<void>}
*/
const handleNewTask = async (values) => {
const newTaskObject = {
variables: {
@@ -207,12 +220,11 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
// We have a relationship query, so we need to refetch the query
if (query && Object.keys(query).length) {
newTaskObject.refetchQueries.push(Object.keys(query)[0]);
} else if (taskQuery) {
} else if (taskQueryRefetch()) {
newTaskObject.refetchQueries.push(taskQuery);
}
const newTaskData = await insertTask(newTaskObject);
const newTaskData = await insertTask({ ...newTaskObject });
const newTask = newTaskData?.data?.insert_tasks?.returning[0];
const newTaskID = newTask?.id;
@@ -229,27 +241,27 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
// send notification to the assigned user
// TODO: This is being moved serverside
axios
.post("/sendemail", {
from: {
name: bodyshop.shopname,
address: bodyshop.email
},
replyTo: {
Email: "noreply@imex.online"
},
to: values.assigned_to,
subject: `A new Task has been assigned to you on ${bodyshop.shopname} - ${values.title}`,
templateName: "taskAssigned",
templateStrings: {
header: values.title,
subHeader: `Assigned by ${currentUser.email} ${values.due_at ? `| Due on ${dayjs(values.due_at).format("MM/DD/YYYY")}` : ""}`,
body: `<a href="${window.location.protocol}//${window.location.host}/manage/tasks/alltasks?taskid=${newTaskID}">Please sign to your account to view the task details.</a>`
}
})
.catch((e) =>
console.error(`Something went wrong sending email to Assigned party on Task edit. ${e.message || ""}`)
);
// axios
// .post("/sendemail", {
// from: {
// name: bodyshop.shopname,
// address: bodyshop.email
// },
// replyTo: {
// Email: "noreply@imex.online"
// },
// to: values.assigned_to,
// subject: `A new Task has been assigned to you on ${bodyshop.shopname} - ${values.title}`,
// templateName: "taskAssigned",
// templateStrings: {
// header: values.title,
// subHeader: `Assigned by ${currentUser.email} ${values.due_at ? `| Due on ${dayjs(values.due_at).format("MM/DD/YYYY")}` : ""}`,
// body: `<a href="${window.location.protocol}//${window.location.host}/manage/tasks/alltasks?taskid=${newTaskID}">Please sign to your account to view the task details.</a>`
// }
// })
// .catch((e) =>
// console.error(`Something went wrong sending email to Assigned party on Task edit. ${e.message || ""}`)
// );
notification["success"]({
message: t("tasks.successes.created")
@@ -269,9 +281,21 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
}
return acc;
}, {});
await handleExistingTask(dirtyValues);
try {
await handleExistingTask(dirtyValues);
} catch (e) {
notification["error"]({
message: t("tasks.failures.updated")
});
}
} else {
await handleNewTask(formValues);
try {
await handleNewTask(formValues);
} catch (e) {
notification["error"]({
message: t("tasks.failures.created")
});
}
}
};