- Simplified solution in previous commit
- Additional regression testing fixes Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -4,7 +4,7 @@ import { useMutation, useQuery } from "@apollo/client";
|
||||
import { MUTATION_TOGGLE_TASK_COMPLETED, MUTATION_TOGGLE_TASK_DELETED } from "../../graphql/tasks.queries.js";
|
||||
import { pageLimit } from "../../utils/config.js";
|
||||
import AlertComponent from "../alert/alert.component.jsx";
|
||||
import React, { useContext, useEffect } from "react";
|
||||
import React from "react";
|
||||
import TaskListComponent from "./task-list.component.jsx";
|
||||
import { notification } from "antd";
|
||||
import { useTranslation } from "react-i18next";
|
||||
@@ -14,7 +14,6 @@ import AuditTrailMapping from "../../utils/AuditTrailMappings.js";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
import { selectBodyshop, selectCurrentUser } from "../../redux/user/user.selectors.js";
|
||||
import dayjs from "../../utils/day";
|
||||
import { SharedModalContext } from "../../providers/shared-modal.provider.jsx";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
bodyshop: selectBodyshop,
|
||||
@@ -41,7 +40,6 @@ export function TaskListContainer({
|
||||
const searchParams = queryString.parse(useLocation().search);
|
||||
const { page, sortcolumn, sortorder, deleted, completed, mine } = searchParams;
|
||||
const dispatch = useDispatch();
|
||||
const { updateTaskQuery } = useContext(SharedModalContext); // Use the context
|
||||
|
||||
const { loading, error, data, refetch } = useQuery(query[Object.keys(query)[0]], {
|
||||
fetchPolicy: "network-only",
|
||||
@@ -166,11 +164,6 @@ export function TaskListContainer({
|
||||
}
|
||||
};
|
||||
|
||||
// Update the Last Task Paginated Query
|
||||
useEffect(() => {
|
||||
updateTaskQuery(Object.keys(query)[0]);
|
||||
}, [query]);
|
||||
|
||||
if (error) return <AlertComponent message={error.message} type="error" />;
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useMutation, useQuery } from "@apollo/client";
|
||||
import { Form, Modal, notification } from "antd";
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { connect } from "react-redux";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
@@ -15,7 +15,7 @@ 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";
|
||||
import { SharedModalContext } from "../../providers/shared-modal.provider.jsx";
|
||||
import refetchRouteMappings from "./task-upsert-modal.route.mappings.js";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
currentUser: selectCurrentUser,
|
||||
@@ -46,7 +46,6 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
|
||||
variables: { id: jobIdState },
|
||||
skip: !jobIdState
|
||||
});
|
||||
const { taskQuery } = useContext(SharedModalContext);
|
||||
|
||||
const {
|
||||
loading: taskLoading,
|
||||
@@ -108,25 +107,32 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
|
||||
};
|
||||
|
||||
/**
|
||||
* Task Query Refetch function to determine if we need to refetch the task query
|
||||
* @returns {boolean}
|
||||
* Handle refetch queries
|
||||
* @param taskObject
|
||||
*/
|
||||
const taskQueryRefetch = () =>
|
||||
(!jobid || !joblineid || !billid || !partsorderid || !taskId) &&
|
||||
taskQuery &&
|
||||
location.pathname.includes("/manage/tasks");
|
||||
const handleRefetchQueries = (taskObject) => {
|
||||
if (query && Object.keys(query).length) {
|
||||
taskObject.refetchQueries.push(Object.keys(query)[0]);
|
||||
} else {
|
||||
refetchRouteMappings.forEach((mapping) => {
|
||||
if (location.pathname.includes(mapping.route)) {
|
||||
taskObject.refetchQueries.push(mapping.query);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle existing task
|
||||
* @param id
|
||||
* @param jobId
|
||||
* @param values
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
const handleExistingTask = async (values) => {
|
||||
const isAssignedToDirty = values.assigned_to !== existingTask.assigned_to;
|
||||
|
||||
const handleExistingTask = async (id, jobId, values) => {
|
||||
const taskObject = {
|
||||
variables: {
|
||||
taskId: existingTask.id,
|
||||
taskId: id,
|
||||
task: replaceUndefinedWithNull(values)
|
||||
},
|
||||
refetchQueries: []
|
||||
@@ -136,16 +142,11 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
|
||||
taskObject.refetchQueries.push({
|
||||
query: GET_JOB_BY_PK,
|
||||
variables: {
|
||||
id: existingTask.jobid
|
||||
id: jobId
|
||||
}
|
||||
});
|
||||
|
||||
// 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 (taskQueryRefetch()) {
|
||||
taskObject.refetchQueries.push(taskQuery);
|
||||
}
|
||||
handleRefetchQueries(taskObject);
|
||||
|
||||
const taskData = await updateTask(taskObject);
|
||||
|
||||
@@ -196,7 +197,7 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
const handleNewTask = async (values) => {
|
||||
const newTaskObject = {
|
||||
const taskObject = {
|
||||
variables: {
|
||||
taskInput: [
|
||||
{
|
||||
@@ -210,7 +211,7 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
|
||||
};
|
||||
|
||||
// We need to make sure this is updated everywhere
|
||||
newTaskObject.refetchQueries.push({
|
||||
taskObject.refetchQueries.push({
|
||||
query: GET_JOB_BY_PK,
|
||||
variables: {
|
||||
id: values.jobid
|
||||
@@ -218,15 +219,10 @@ 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 (taskQueryRefetch()) {
|
||||
newTaskObject.refetchQueries.push(taskQuery);
|
||||
}
|
||||
handleRefetchQueries(taskObject);
|
||||
|
||||
const newTaskData = await insertTask({ ...newTaskObject });
|
||||
const newTaskData = await insertTask(taskObject);
|
||||
const newTask = newTaskData?.data?.insert_tasks?.returning[0];
|
||||
const newTaskID = newTask?.id;
|
||||
|
||||
if (!newTaskData.errors) {
|
||||
insertAuditTrail({
|
||||
@@ -274,15 +270,16 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
|
||||
* @returns {Promise<[{jobid, bodyshopid, created_by},...*]>}
|
||||
*/
|
||||
const handleFinish = async (formValues) => {
|
||||
if (existingTask) {
|
||||
if (existingTask || taskData?.tasks_by_pk) {
|
||||
const taskSource = existingTask || taskData?.tasks_by_pk;
|
||||
const dirtyValues = Object.keys(formValues).reduce((acc, key) => {
|
||||
if (!isEqual(formValues[key], existingTask[key])) {
|
||||
if (!isEqual(formValues[key], taskSource[key])) {
|
||||
acc[key] = formValues[key];
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
try {
|
||||
await handleExistingTask(dirtyValues);
|
||||
await handleExistingTask(taskSource.id, taskSource.jobid, dirtyValues);
|
||||
} catch (e) {
|
||||
notification["error"]({
|
||||
message: t("tasks.failures.updated")
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import {
|
||||
QUERY_ALL_TASKS_PAGINATED,
|
||||
QUERY_JOB_TASKS_PAGINATED,
|
||||
QUERY_MY_TASKS_PAGINATED
|
||||
} from "../../graphql/tasks.queries.js";
|
||||
|
||||
const getQueryName = (query) => Object.keys(query)[0];
|
||||
|
||||
const refetchRouteMappings = [
|
||||
{query: getQueryName({QUERY_MY_TASKS_PAGINATED}), route: "/manage/tasks/mytasks"},
|
||||
{query: getQueryName({QUERY_ALL_TASKS_PAGINATED}), route: "/manage/tasks/alltasks"},
|
||||
{query: getQueryName({QUERY_JOB_TASKS_PAGINATED}), route: "/manage/jobs"}
|
||||
];
|
||||
|
||||
export default refetchRouteMappings;
|
||||
Reference in New Issue
Block a user