import env from "@/env";
import { GET_JOB_TOMBSTONE } from "@/graphql/jobs.queries";
import { selectBodyshop } from "@/redux/user/user.selectors";
import { useQuery } from "@apollo/client";
import { useLocalSearchParams, useRouter } from "expo-router";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import {
Platform,
RefreshControl,
ScrollView,
StyleSheet,
TouchableOpacity,
View,
} from "react-native";
import {
ActivityIndicator,
Button,
Card,
Chip,
Text,
} from "react-native-paper";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { firstNames, getRandomIndex, lastNames } from "../../util/demodata";
import DataLabelComponent from "../data-label/data-label";
import ErrorDisplay from "../error/error-display";
import { JobStatusSelector } from "../job-status-selector/job-status-selector";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
});
const mapDispatchToProps = (dispatch) => ({});
export default connect(mapStateToProps, mapDispatchToProps)(JobTombstone);
function JobTombstone({ bodyshop }) {
const { jobId } = useLocalSearchParams();
const router = useRouter();
const { loading, error, data, refetch } = useQuery(GET_JOB_TOMBSTONE, {
variables: {
id: jobId,
},
skip: !jobId,
});
const { t } = useTranslation();
const onRefresh = async () => {
return refetch();
};
const [availableStatuses, setAvailableStatuses] = useState([]);
const job = data?.jobs_by_pk;
useEffect(() => {
if (!job || !bodyshop) return;
const { md_ro_statuses } = bodyshop;
const {
pre_production_statuses,
production_statuses,
post_production_statuses,
statuses,
default_invoiced,
default_exported,
} = md_ro_statuses;
// Handle non-parts entry scenarios based on job status
if (pre_production_statuses.includes(job.status)) {
setAvailableStatuses(pre_production_statuses);
} else if (production_statuses.includes(job.status)) {
setAvailableStatuses(production_statuses);
} else if (post_production_statuses.includes(job.status)) {
// Filter out invoiced and exported statuses for post-production
setAvailableStatuses(
post_production_statuses.filter(
(status) => status !== default_invoiced && status !== default_exported
)
);
} else {
// Default to all statuses if no specific restrictions apply
console.log(
"Status didn't match any restrictions. Allowing all status changes."
);
setAvailableStatuses(statuses);
}
}, [job, bodyshop, setAvailableStatuses]);
if (loading) {
return ;
}
if (error) {
return ;
}
if (!data.jobs_by_pk) {
return (
Job is not defined.
);
}
return (
}
>
}
/>
{job.inproduction && (
{t("objects.jobs.labels.inproduction")}
}
/>
)}
{job.inproduction &&
job.production_vars &&
!!job.production_vars.note && (
{job.production_vars.note}}
/>
)}
{job.vehicle?.jobs
?.filter((ro) => ro.id !== job.id)
.map((ro) => (
{
router.navigate({
pathname: `/jobs/${ro.id}`,
params: {
title: ro.ro_number || t("general.labels.na"),
},
});
}}
key={ro.id}
>
))}
}
/>
{env.DEMO_MODE
? `${job.v_model_yr || ""} ${job.v_make_desc || ""} ${
job.v_model_desc || ""
} - 1GNDX33L46D168902`
: `${job.v_model_yr || ""} ${job.v_make_desc || ""} ${
job.v_model_desc || ""
} - ${job.v_vin || ""}`}
{(() => {
const paintCodes = Object.keys(
job.vehicle?.v_paint_codes || {}
)
.filter(
(key) =>
job.vehicle.v_paint_codes[key] !== "" &&
job.vehicle.v_paint_codes[key] !== null &&
job.vehicle.v_paint_codes[key] !== undefined
)
.map((key) => job.vehicle.v_paint_codes[key]);
if (paintCodes.length > 0) {
return `${job.v_color || ""} (${paintCodes.join(
", "
)})`.trim();
}
return job.v_color || "";
})()}
}
/>
);
}
const localStyles = StyleSheet.create({
twoColumnCard: { display: "flex", flexDirection: "row" },
twoColumnCardColumn: { flex: 1 },
status: {
textAlign: "center",
flexDirection: "row",
justifyContent: "center",
},
inproduction: {
textAlign: "center",
flexDirection: "row",
justifyContent: "center",
},
});