import React, { useState, useEffect, useRef } from "react";
import { useParams, useNavigate } from "react-router";
import { toast } from "react-toastify";
import { getWorkOrderById, addComment } from "../api";
import "./WorkOrderViewPage.css";
function formatDateTime(value) {
const d = new Date(value.endsWith("Z") ? value : value + "Z");
if (isNaN(d)) return value;
const date = d.toLocaleDateString("en-CA");
const time = d.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit", hour12: false });
const tz = d.toLocaleTimeString(undefined, { timeZoneName: "short" }).split(" ").pop();
return `${date} ${time} ${tz}`;
}
function CommentTimeline({ comments }) {
const bottomRef = useRef(null);
useEffect(() => {
bottomRef.current?.scrollIntoView();
}, [comments?.length]);
if (!comments || comments.length === 0) {
return
;
}
return (
{comments.map((c) => (
{c.userName || c.commenter || "System"}
{c.createdDate ? formatDateTime(c.createdDate) : ""}
{c.commenttext}
))}
);
}
function AuditTimeline({ entries }) {
if (!entries || entries.length === 0) {
return ;
}
return (
{entries.map((e) => (
{e.userName || "System"}
{e.createdAt ? formatDateTime(e.createdAt) : ""}
{e.fieldName}
{e.oldValue && {e.oldValue}}
→
{e.newValue}
))}
);
}
function CommentInput({ workOrderId, commentType, onCommentAdded }) {
const [text, setText] = useState("");
const [sending, setSending] = useState(false);
const handleSubmit = async (e) => {
e.preventDefault();
if (!text.trim()) return;
setSending(true);
try {
const newComment = await addComment(workOrderId, text.trim(), commentType);
setText("");
if (onCommentAdded) onCommentAdded(newComment);
} catch (err) {
toast.error("Failed to add comment");
} finally {
setSending(false);
}
};
const handleKeyDown = (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit(e);
}
};
return (
);
}
export default function WorkOrderViewPage() {
const { id } = useParams();
const navigate = useNavigate();
const [wo, setWo] = useState(null);
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState("customer");
useEffect(() => {
const load = async () => {
try {
const data = await getWorkOrderById(id);
setWo(data);
} catch (err) {
toast.error("Failed to load work order");
} finally {
setLoading(false);
}
};
load();
}, [id]);
const handleCommentAdded = (newComment) => {
setWo((prev) => ({
...prev,
comments: [
...prev.comments,
{
id: newComment.id,
createdDate: newComment.createdDate,
commenttext: newComment.commenttext,
commentType: newComment.commentType,
userName: newComment.userName,
commenter: null,
recordType: null,
},
],
}));
};
if (loading) return ;
if (!wo) return ;
const statusClass = (wo.status || "").toLowerCase().replace(/\s+/g, "-");
const allComments = wo.comments || [];
const customerComments = allComments.filter((c) => c.commentType === "customer");
const vendorComments = allComments.filter((c) => c.commentType === "vendor");
const internalComments = allComments.filter((c) => c.commentType === "internal");
const auditLog = wo.auditLog || [];
const tabs = [
{ key: "customer", label: "Customer", count: customerComments.length },
{ key: "vendor", label: "Vendor", count: vendorComments.length },
{ key: "internal", label: "Internal", count: internalComments.length },
{ key: "audit", label: "Audit Log", count: auditLog.length },
];
const showInput = activeTab === "internal" || activeTab === "vendor";
return (
-
{ e.preventDefault(); navigate("/workorders"); }}>
Work Orders
- {wo.workOrderNumber}
{wo.title}
{wo.status}
Details
WO #
{wo.workOrderNumber}
Priority
{wo.priority || "—"}
Location
{wo.location || "—"}
Assigned To
{wo.assignedTo || "—"}
Created
{wo.createdDateFull ? formatDateTime(wo.createdDateFull) : "—"}
Due Date
{wo.dueDate || "—"}
PO
{wo.po || "—"}
TT
{wo.tt || "—"}
Description
{wo.description || "No description provided."}
Activity
{tabs.map((tab) => (
))}
{activeTab === "customer" &&
}
{activeTab === "vendor" &&
}
{activeTab === "internal" &&
}
{activeTab === "audit" &&
}
{showInput && (
)}
);
}