diff --git a/components/screen-time-ticket-browser/screen-time-ticket-browser.component.jsx b/components/screen-time-ticket-browser/screen-time-ticket-browser.component.jsx index 97d2bea..c784138 100644 --- a/components/screen-time-ticket-browser/screen-time-ticket-browser.component.jsx +++ b/components/screen-time-ticket-browser/screen-time-ticket-browser.component.jsx @@ -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 ( - - {/* */} - {/* {signingErrorMsg} */} + - + {error && error?.message ? ( + + ) : null} diff --git a/components/time-ticket-lists/employee-clockedin-list.component.jsx b/components/time-ticket-lists/employee-clockedin-list.component.jsx index 2d375a0..9a1b86d 100644 --- a/components/time-ticket-lists/employee-clockedin-list.component.jsx +++ b/components/time-ticket-lists/employee-clockedin-list.component.jsx @@ -23,6 +23,8 @@ const mapStateToProps = createStructuredSelector({ // }); export function EmployeeClockedInList({ technician }) { + console.info("EmployeeClockedInList, QUERY_ACTIVE_TIME_TICKETS called."); + const { t } = useTranslation(); const { loading, error, data, refetch } = useQuery( @@ -39,15 +41,12 @@ export function EmployeeClockedInList({ technician }) { if (loading) return ; if (error) return ; - console.log("EmployeeClockedInList, QUERY_ACTIVE_TIME_TICKETS data:", data); - // if (data) () => {setTmTicketJobId(data)} - const onRefresh = async () => { return refetch(); }; return ( - + {data.timetickets.length > 0 ? ( diff --git a/components/time-ticket/screen-time-ticket-clockoff.component.jsx b/components/time-ticket/screen-time-ticket-clockoff.component.jsx index 4505f11..1487ddd 100644 --- a/components/time-ticket/screen-time-ticket-clockoff.component.jsx +++ b/components/time-ticket/screen-time-ticket-clockoff.component.jsx @@ -22,7 +22,6 @@ import axios from "axios"; import { useNavigation } from "@react-navigation/native"; // import { selectCurrentTimeTicketJobId } from "../../redux/timetickets/timetickets.selectors"; -//TODO add props needed for call const mapStateToProps = createStructuredSelector({ currentEmployee: selectCurrentEmployee, currentRatesNCostCenters: selectRates, diff --git a/components/time-ticket/screen-time-ticket-create.component.jsx b/components/time-ticket/screen-time-ticket-create.component.jsx index a27d3e8..feab9b3 100644 --- a/components/time-ticket/screen-time-ticket-create.component.jsx +++ b/components/time-ticket/screen-time-ticket-create.component.jsx @@ -1,5 +1,5 @@ import { Formik } from "formik"; -import React, { useEffect, useState } from "react"; +import React, { useState } from "react"; import { StyleSheet, Text, View, ScrollView } from "react-native"; import { useTranslation } from "react-i18next"; import { connect } from "react-redux"; @@ -50,7 +50,6 @@ export function TimeTicketCreate({ // const [currentSJob, setCurrentSJob] = useState(null); const [currentSJobId, setCurrentSJobId] = useState(null); - // const wrapperSetCurrentSJobState = useCallback( // (val) => { // setCurrentSJob(val); @@ -70,13 +69,14 @@ export function TimeTicketCreate({ hideDatePicker(); }; - const [insertTimeTicket] = useMutation(INSERT_NEW_TIME_TICKET); + const [insertTicket] = useMutation(INSERT_NEW_TIME_TICKET); const handleFinish = async (values) => { - const theTime = (await axios.post("/utils/time")).data; + logImEXEvent("handleFinish_called_in_TimeTicketCreate"); + console.log("handleFinish called in TimeTicketCreate"); + setError(null); setLoading(true); - logImEXEvent("insertTimeTicket_handleFinish"); // console.log("insertTimeTicket, currentSCC :", currentSCC); // console.log("insertTimeTicket, currentSCC :", currentSJobId); // console.log("insertTimeTicket, values :", values); @@ -85,89 +85,60 @@ export function TimeTicketCreate({ !!currentSCC?.value && !!currentSJobId?.value && !!values.productivehours && - !!values.actualhours && - !!values.productivehours + !!currentBodyshop.id && + !!currentEmployee?.technician?.id && + !!date2 ) { setError(null); - console.log("all have values:"); + 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); - + if (date2) console.log("rate :", currentRatesNCostCenters && currentSCC?.value ? currentRatesNCostCenters.filter((r) => r.cost_center === currentSCC?.value)[0].rate : null);//2023-05-16T16:45:27.154Z + if (date2) console.log("bodyshopid : ", currentBodyshop.id); + if (date2) console.log("moment(date2).format(YYYY-MM-DD)", moment(date2).format("YYYY-MM-DD")); const tempVariablesObj = { variables: { timeTicketInput: [ { + actualhrs: values?.actualhours ? values?.actualhours : null, bodyshopid: currentBodyshop.id, - employeeid: currentEmployee?.technician?.id, - date: moment(theTime).format("YYYY-MM-DD"), - clockon: moment(theTime), - jobid: currentSJobId?.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);}), 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); - }), - } + date: moment(date2).format("YYYY-MM-DD"), + employeeid: currentEmployee?.technician?.id, + flat_rate: currentEmployee && currentEmployee.technician && currentEmployee.technician?.flat_rate, + jobid: currentSJobId?.value, + productivehrs: values?.productivehours, + rate: currentRatesNCostCenters && currentSCC?.value ? currentRatesNCostCenters.filter((r) => r.cost_center === currentSCC?.value)[0].rate : null, + }, ], }, }; - // const tempcallobj = { - // variables: { - // timeticketId: timeTicketId, - // timeticket: { - // actualhrs: values?.actualhours, - // 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 - // ); - // }), - // clockoff: (await axios.post("/utils/time")).data, - // cost_center: currentSCC?.value, - // flat_rate: - // currentEmployee && - // currentEmployee.technician && - // currentEmployee.technician?.flat_rate, - // productivehrs: values?.productivehours, - // rate: - // currentRatesNCostCenters && - // currentSCC?.value && - // currentRatesNCostCenters.filter( - // (r) => r.cost_center === currentSCC?.value - // )[0]?.rate, - // }, - // }, - // }; - console.log("insertTimeTicket, tempVariablesObj :", tempVariablesObj?.variables?.timeTicketInput[0]); + // clockoff: undefined, + // clockon: undefined, + // memo: undefined, + + console.log("insertTimeTicket, tempVariablesObj. :", tempVariablesObj?.variables?.timeTicketInput[0]); //after obj is good add below to make call - const result = await insertTimeTicket(tempVariablesObj); + const result = await insertTicket(tempVariablesObj); - //after call results are retuning add handling below for cases + // //after call results are retuning add handling below for cases console.log("insertTimeTicket, result :", result); setLoading(false); - - if (!!result.errors) { - console.log("insertTimeTicket, result.error :", result.errors); - setError(SON.stringify(result.errors)); - } else { - console.log("insertTimeTicket, result. :", result.data); - navigation.goBack(); - } + // if (!!result.errors) { + // console.log("insertTimeTicket, result.error :", result.errors); + // setError(JSON.stringify(result.errors)); + // } else { + // console.log("insertTimeTicket, result. :", result.data); + // navigation.goBack(); + // } // if (completedCallback) completedCallback(); }; @@ -248,6 +219,7 @@ export function TimeTicketCreate({