- additional cleanup and validation / fixes

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-04-11 14:25:16 -04:00
parent 7c8f276bb0
commit 77c486b4c9
6 changed files with 66 additions and 28 deletions

View File

@@ -16,6 +16,7 @@ import axios from "axios";
import dayjs from "../../utils/day";
import { insertAuditTrail } from "../../redux/application/application.actions.js";
import AuditTrailMapping from "../../utils/AuditTrailMappings.js";
import { isEqual } from "lodash";
const mapStateToProps = createStructuredSelector({
currentUser: selectCurrentUser,
@@ -33,13 +34,13 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
const history = useNavigate();
const [insertTask] = useMutation(MUTATION_INSERT_NEW_TASK);
const [updateTask] = useMutation(MUTATION_UPDATE_TASK);
const { open, context, actions } = taskUpsert;
const { open, context } = taskUpsert;
const { jobid, joblineid, billid, partsorderid, taskId, existingTask, query } = context;
const { refetch } = actions;
const [form] = Form.useForm();
const [selectedJobId, setSelectedJobId] = useState(null);
const [selectedJobDetails, setSelectedJobDetails] = useState(null);
const [jobIdState, setJobIdState] = useState(null);
const [isTouched, setIsTouched] = useState(false);
const { loading, error, data } = useQuery(QUERY_GET_TASKS_JOB_DETAILS_BY_ID, {
variables: { id: jobIdState },
@@ -91,6 +92,7 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
}
return () => {
setSelectedJobId(null);
setIsTouched(false);
};
}, [jobid, existingTask, form, open, joblineid, billid, partsorderid]);
@@ -164,8 +166,6 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
message: t("tasks.successes.updated")
});
if (refetch) await refetch();
toggleModalVisible();
};
@@ -181,19 +181,6 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
]
},
refetchQueries: ["GET_JOB_BY_PK"]
// update(cache, { data }) {
// cache.modify({
// fields: {
// tasks(cached) {
// const newTasks = data?.insert_tasks?.returning.map(task => cache.writeFragment({
// data: task,
// fragment: PARTIAL_TASK_FIELDS_WRAPPER
// }));
// return [...cached, ...newTasks];
// }
// }
// });
// }
};
if (query && Object.keys(query).length) {
@@ -213,8 +200,6 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
});
}
if (refetch) await refetch();
form.resetFields();
toggleModalVisible();
@@ -253,12 +238,16 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
* @returns {Promise<[{jobid, bodyshopid, created_by},...*]>}
*/
const handleFinish = async (formValues) => {
const { ...values } = formValues;
if (existingTask) {
await handleExistingTask(values);
const dirtyValues = Object.keys(formValues).reduce((acc, key) => {
if (!isEqual(formValues[key], existingTask[key])) {
acc[key] = formValues[key];
}
return acc;
}, {});
await handleExistingTask(dirtyValues);
} else {
await handleNewTask(values);
await handleNewTask(formValues);
}
};
@@ -276,14 +265,23 @@ export function TaskUpsertModalContainer({ bodyshop, currentUser, taskUpsert, to
removeTaskIdFromUrl();
toggleModalVisible();
}}
okButtonProps={{ disabled: !isTouched }}
destroyOnClose
>
<Form form={form} onFinish={handleFinish} layout="vertical">
<Form
form={form}
onFinish={handleFinish}
layout="vertical"
onFieldsChange={() => {
setIsTouched(true);
}}
>
<TaskUpsertModalComponent
form={form}
loading={loading || (taskId && taskLoading)}
error={error}
data={data}
existingTask={existingTask}
selectedJobId={selectedJobId}
setSelectedJobId={setSelectedJobId}
selectedJobDetails={selectedJobDetails}