Add working call for clock in to timeticketbrowser

This commit is contained in:
jfrye122
2023-05-16 13:49:28 -04:00
parent 9132c1bb9b
commit e950fd9fd7
5 changed files with 124 additions and 146 deletions

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import React, { useState } from "react";
import { View, Text } from "react-native";
import axios from "axios";
import { connect } from "react-redux";
@@ -26,6 +26,10 @@ import moment from "moment";
import { EmployeeClockedInList } from "../time-ticket-lists/employee-clockedin-list.component";
import { useNavigation } from "@react-navigation/native";
import { INSERT_NEW_TIME_TICKET } from "../../graphql/timetickets.queries";
import { useMutation } from "@apollo/client";
import { logImEXEvent } from "../../firebase/firebase.analytics";
const mapStateToProps = createStructuredSelector({
currentEmployee: selectCurrentEmployee,
loaderGettingRates: selectGettingRates,
@@ -54,9 +58,12 @@ export function ScreenTimeTicketBrowser({
const navigation = useNavigation();
//const employeeId = currentEmployee.technician.id;
const [currentSCC, setCurrentSCC] = useState(null);
// const [currentSJob, setCurrentSJob] = useState(null);
const [currentSJobId, setCurrentSJobId] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [insertTimeTicket] = useMutation(INSERT_NEW_TIME_TICKET, {
refetchQueries: ["QUERY_ACTIVE_TIME_TICKETS"],
});
// const { error, data } = useQuery(QUERY_EMPLOYEE_BY_ID, {
// variables: { id: currentEmployee.technician.id },
// });
@@ -69,78 +76,81 @@ export function ScreenTimeTicketBrowser({
// },
// [setCurrentSJobId]
// );
const handleFinish = async (values) => {
console.log("handleFinish called in ScreenTimeTicketBrowser");
setLoading(true);
setError(null);
//do stuff...
const getRates = (currentEmployee) => {
employeeGetRatesStart(currentEmployee.technician.id);
};
const createTheTimeTicketOBJ = async (
currentEmployee,
currentBodyshop,
currentSCC,
currentSJobId
) => {
const theTime = (await axios.post("/utils/time")).data;
if (currentBodyshop) console.log("bodyshopid", currentBodyshop?.id);
if (currentEmployee) console.log("employeeid", currentEmployee?.technician.id);
if (theTime) console.log("date", moment(theTime).format("YYYY-MM-DD"));
// if (currentSJob) console.log("currentSJob", currentSJob?.value);
if (currentSJobId) console.log("jobid or currentSJobId", currentSJobId?.value);
if (currentSCC) console.log("cost_center or currentSCC", currentSCC?.value);
//if(currentBodyshop)console.log(currentBodyshop);
if (currentBodyshop)
console.log(
"ciecacode",
currentBodyshop?.cdk_dealerid || currentBodyshop?.pbs_serialnumber
? currentSCC?.value
: Object.keys(
currentBodyshop.md_responsibility_centers.defaults.costs
).find((key) => {
return (
currentBodyshop.md_responsibility_centers.defaults.costs[
key
] === currentSCC?.value
);
})
);
if (currentEmployee)
console.log("flat_rate", currentEmployee?.technician?.flat_rate);
if (currentSCC) console.log("rate or currentSCC", currentSCC?.rate);
// const temp = {
// timeTicketInput: [
// {
//have bodyshopid: currentBodyshop?.id,
//have employeeid: currentEmployee?.technician?.id,
//have date: "2023-05-11", //moment(theTime).format("YYYY-MM-DD"),
// //clockon: moment(theTime),
// jobid: "temp",//values.jobid,
//have cost_center: "temp",//values.cost_center,
//have ciecacode: currentBodyshop?.cdk_dealerid || currentBodyshop?.pbs_serialnumber
// ? values.cost_center
// : Object.keys(currentBodyshop.md_responsibility_centers.defaults.costs).find((key) => {
// return (currentBodyshop.md_responsibility_centers.defaults.costs[key] === "temp");//values.cost_center);
// }),
//have flat_rate: currentEmployee.technician.flat_rate,
//have rate: 1,
// },
// ],
// };
// console.log(temp);
//employeeGetRatesStart(currentEmployee.technician.id);
if (!!currentSCC?.value && !!currentSJobId?.value) {
setError(null);
console.log("have all values");
} else {
console.log("missing values!");
setLoading(false);
setError({ message: "Please make sure all fields have a value." });
return;
}
if (currentSJobId)
console.log("jobid or currentSJobId", currentSJobId?.value);
const tempVariablesObj = {
variables: {
timeTicketInput: [
{
bodyshopid: currentBodyshop.id,
employeeid: currentEmployee?.technician?.id,
date: moment(theTime).format("YYYY-MM-DD"),
clockon: moment(theTime),
jobid: currentSJobId?.value,
cost_center: currentSCC?.value,
ciecacode:
currentBodyshop?.cdk_dealerid || currentBodyshop?.pbs_serialnumber
? currentSCC?.value
: Object.keys(
currentBodyshop.md_responsibility_centers.defaults.costs
).find((key) => {
return (
currentBodyshop.md_responsibility_centers.defaults.costs[
key
] === currentSCC?.value
);
}),
},
],
},
};
console.info(
"INSERT_NEW_TIME_TICKET, variables for clockin. : ",
tempVariablesObj?.variables?.timeTicketInput[0]
);
const result = await insertTimeTicket(tempVariablesObj);
console.log("insertTimeTicket, result :", result);
if (!!result.errors) {
console.log("insertTimeTicket, result.error :", result.errors);
setError(JSON.stringify(result.errors));
} else {
console.log("insertTimeTicket, result. :", result.data);
//show success
//clear fields
setCurrentSJobId(null);
setCurrentSCC(null);
}
setLoading(false);
};
return (
<View>
{/* <Button
mode="outlined"
loading={loaderGettingRates}
//onPress={() => getRates(currentEmployee)}
onPress={() =>
createTheTimeTicketOBJ(currentEmployee, currentBodyshop, currentSCC, currentSJobId)
}
>
<Text>createTheTimeTicketOBJTest</Text>
</Button> */}
{/* {signingErrorMsg} */}
<View style={{flex:1}}>
<JobIdSearchSelect
currentValue={currentSJobId}
onJobSelected={setCurrentSJobId}
@@ -152,13 +162,12 @@ export function ScreenTimeTicketBrowser({
currentRatesNCostCenters={currentRatesNCostCenters}
onValueSelected={setCurrentSCC}
/>
<Button
mode="outlined"
//loading={loaderClockIn}
//onPress={() => clockIn()}
>
<Button mode="outlined" loading={loading} onPress={handleFinish}>
<Text>Clock In</Text>
</Button>
{error && error?.message ? (
<ErrorDisplay errorMessage={error.message} />
) : null}
<EmployeeClockedInList technician={currentEmployee} />
</View>