- Merge client update into test-beta
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -1,151 +1,149 @@
|
||||
import React, { useCallback, useEffect, useState } from "react";
|
||||
import React, {useCallback, useEffect, useState} from "react";
|
||||
|
||||
import { Form, Modal, notification } from "antd";
|
||||
import {Form, Modal, notification} from "antd";
|
||||
import axios from "axios";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { connect } from "react-redux";
|
||||
import { createStructuredSelector } from "reselect";
|
||||
import { toggleModalVisible } from "../../redux/modals/modals.actions";
|
||||
import { selectTimeTicketTasks } from "../../redux/modals/modals.selectors";
|
||||
import {
|
||||
selectBodyshop,
|
||||
selectCurrentUser,
|
||||
} from "../../redux/user/user.selectors";
|
||||
import {useTranslation} from "react-i18next";
|
||||
import {connect} from "react-redux";
|
||||
import {createStructuredSelector} from "reselect";
|
||||
import {toggleModalVisible} from "../../redux/modals/modals.actions";
|
||||
import {selectTimeTicketTasks} from "../../redux/modals/modals.selectors";
|
||||
import {selectBodyshop, selectCurrentUser,} from "../../redux/user/user.selectors";
|
||||
import TimeTicketTaskModalComponent from "./time-ticket-task-modal.component";
|
||||
import { useApolloClient } from "@apollo/client";
|
||||
import { QUERY_COMPLETED_TASKS } from "../../graphql/jobs.queries";
|
||||
import {useApolloClient} from "@apollo/client";
|
||||
import {QUERY_COMPLETED_TASKS} from "../../graphql/jobs.queries";
|
||||
import "./time-ticket-task-modal.styles.scss";
|
||||
import { selectTechnician } from "../../redux/tech/tech.selectors";
|
||||
import {selectTechnician} from "../../redux/tech/tech.selectors";
|
||||
|
||||
const mapStateToProps = createStructuredSelector({
|
||||
timeTicketTasksModal: selectTimeTicketTasks,
|
||||
bodyshop: selectBodyshop,
|
||||
currentUser: selectCurrentUser,
|
||||
technician: selectTechnician,
|
||||
timeTicketTasksModal: selectTimeTicketTasks,
|
||||
bodyshop: selectBodyshop,
|
||||
currentUser: selectCurrentUser,
|
||||
technician: selectTechnician,
|
||||
});
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
toggleModalVisible: () => dispatch(toggleModalVisible("timeTicketTask")),
|
||||
toggleModalVisible: () => dispatch(toggleModalVisible("timeTicketTask")),
|
||||
});
|
||||
export default connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(TimeTickeTaskModalContainer);
|
||||
|
||||
export function TimeTickeTaskModalContainer({
|
||||
bodyshop,
|
||||
currentUser,
|
||||
technician,
|
||||
timeTicketTasksModal,
|
||||
toggleModalVisible,
|
||||
}) {
|
||||
const [form] = Form.useForm();
|
||||
const { context, visible, actions } = timeTicketTasksModal;
|
||||
const [completedTasks, setCompletedTasks] = useState([]);
|
||||
const [unassignedHours, setUnassignedHours] = useState(0);
|
||||
const { t } = useTranslation();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const client = useApolloClient();
|
||||
bodyshop,
|
||||
currentUser,
|
||||
technician,
|
||||
timeTicketTasksModal,
|
||||
toggleModalVisible,
|
||||
}) {
|
||||
const [form] = Form.useForm();
|
||||
const {context, visible, actions} = timeTicketTasksModal;
|
||||
const [completedTasks, setCompletedTasks] = useState([]);
|
||||
const [unassignedHours, setUnassignedHours] = useState(0);
|
||||
const {t} = useTranslation();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const client = useApolloClient();
|
||||
|
||||
async function handleFinish(values) {
|
||||
calculateTickets({ values, handleFinish: true });
|
||||
}
|
||||
const getCompletedTasks = useCallback(
|
||||
async (jobid) => {
|
||||
setLoading(true);
|
||||
|
||||
const { data } = await client.query({
|
||||
query: QUERY_COMPLETED_TASKS,
|
||||
variables: { jobid },
|
||||
});
|
||||
|
||||
setCompletedTasks(data.jobs_by_pk.completed_tasks || []);
|
||||
setLoading(false);
|
||||
},
|
||||
[client]
|
||||
);
|
||||
useEffect(() => {
|
||||
if (visible) {
|
||||
form.setFieldsValue({ ...context, task: null, timetickets: null });
|
||||
if (context.jobid) {
|
||||
getCompletedTasks(context.jobid);
|
||||
}
|
||||
async function handleFinish(values) {
|
||||
calculateTickets({values, handleFinish: true});
|
||||
}
|
||||
}, [context.jobid, visible, getCompletedTasks, form, context]);
|
||||
|
||||
async function handleValueChange(changedValues, allValues) {
|
||||
if (changedValues.jobid) {
|
||||
getCompletedTasks(changedValues.jobid);
|
||||
}
|
||||
if (allValues.jobid && allValues.task) {
|
||||
calculateTickets({ values: allValues, handleFinish: false });
|
||||
}
|
||||
}
|
||||
const getCompletedTasks = useCallback(
|
||||
async (jobid) => {
|
||||
setLoading(true);
|
||||
|
||||
const calculateTickets = async ({ values, handleFinish }) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const { data, ...response } = await axios.post("/payroll/claimtask", {
|
||||
jobid: values.jobid,
|
||||
task: values.task,
|
||||
calculateOnly: !handleFinish,
|
||||
employee: technician
|
||||
? {
|
||||
name: `${technician.first_name} ${technician.last_name}`.trim(),
|
||||
employeeid: technician.id,
|
||||
const {data} = await client.query({
|
||||
query: QUERY_COMPLETED_TASKS,
|
||||
variables: {jobid},
|
||||
});
|
||||
|
||||
setCompletedTasks(data.jobs_by_pk.completed_tasks || []);
|
||||
setLoading(false);
|
||||
},
|
||||
[client]
|
||||
);
|
||||
useEffect(() => {
|
||||
if (visible) {
|
||||
form.setFieldsValue({...context, task: null, timetickets: null});
|
||||
if (context.jobid) {
|
||||
getCompletedTasks(context.jobid);
|
||||
}
|
||||
: { name: currentUser.displayName, email: currentUser.email },
|
||||
});
|
||||
if (response.status === 200 && handleFinish) {
|
||||
//Close the modal
|
||||
if (actions?.refetch) actions.refetch();
|
||||
toggleModalVisible();
|
||||
} else if (handleFinish === false) {
|
||||
form.setFieldsValue({ timetickets: data.ticketsToInsert });
|
||||
setUnassignedHours(data.unassignedHours);
|
||||
} else {
|
||||
notification.open({
|
||||
type: "error",
|
||||
message: t("timetickets.errors.creating", {
|
||||
message: JSON.stringify(data),
|
||||
}),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
notification.open({
|
||||
type: "error",
|
||||
message: t("timetickets.errors.creating", { message: error.message }),
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
}
|
||||
}, [context.jobid, visible, getCompletedTasks, form, context]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
destroyOnClose
|
||||
open={visible}
|
||||
onCancel={() => {
|
||||
toggleModalVisible();
|
||||
form.resetFields();
|
||||
}}
|
||||
width="80%"
|
||||
onOk={() => form.submit()}
|
||||
>
|
||||
<Form
|
||||
autoComplete={"off"}
|
||||
form={form}
|
||||
layout="vertical"
|
||||
onFinish={handleFinish}
|
||||
initialValues={context}
|
||||
onValuesChange={handleValueChange}
|
||||
>
|
||||
<TimeTicketTaskModalComponent
|
||||
form={form}
|
||||
loading={loading}
|
||||
completedTasks={completedTasks}
|
||||
unassignedHours={unassignedHours}
|
||||
/>
|
||||
</Form>
|
||||
</Modal>
|
||||
);
|
||||
async function handleValueChange(changedValues, allValues) {
|
||||
if (changedValues.jobid) {
|
||||
getCompletedTasks(changedValues.jobid);
|
||||
}
|
||||
if (allValues.jobid && allValues.task) {
|
||||
calculateTickets({values: allValues, handleFinish: false});
|
||||
}
|
||||
}
|
||||
|
||||
const calculateTickets = async ({values, handleFinish}) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const {data, ...response} = await axios.post("/payroll/claimtask", {
|
||||
jobid: values.jobid,
|
||||
task: values.task,
|
||||
calculateOnly: !handleFinish,
|
||||
employee: technician
|
||||
? {
|
||||
name: `${technician.first_name} ${technician.last_name}`.trim(),
|
||||
employeeid: technician.id,
|
||||
}
|
||||
: {name: currentUser.displayName, email: currentUser.email},
|
||||
});
|
||||
if (response.status === 200 && handleFinish) {
|
||||
//Close the modal
|
||||
if (actions?.refetch) actions.refetch();
|
||||
toggleModalVisible();
|
||||
} else if (handleFinish === false) {
|
||||
form.setFieldsValue({timetickets: data.ticketsToInsert});
|
||||
setUnassignedHours(data.unassignedHours);
|
||||
} else {
|
||||
notification.open({
|
||||
type: "error",
|
||||
message: t("timetickets.errors.creating", {
|
||||
message: JSON.stringify(data),
|
||||
}),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
notification.open({
|
||||
type: "error",
|
||||
message: t("timetickets.errors.creating", {message: error.message}),
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal
|
||||
destroyOnClose
|
||||
open={visible}
|
||||
onCancel={() => {
|
||||
toggleModalVisible();
|
||||
form.resetFields();
|
||||
}}
|
||||
width="80%"
|
||||
onOk={() => form.submit()}
|
||||
>
|
||||
<Form
|
||||
autoComplete={"off"}
|
||||
form={form}
|
||||
layout="vertical"
|
||||
onFinish={handleFinish}
|
||||
initialValues={context}
|
||||
onValuesChange={handleValueChange}
|
||||
>
|
||||
<TimeTicketTaskModalComponent
|
||||
form={form}
|
||||
loading={loading}
|
||||
completedTasks={completedTasks}
|
||||
unassignedHours={unassignedHours}
|
||||
/>
|
||||
</Form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user