mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-01 06:13:12 +00:00
- New Classification section: Problem, Trade, SubTrade dropdowns (SubTrade filters by Trade) - New Schedule fields: Scheduled, Completed dates - New Financial section: Vendor NTE, Customer NTE placeholder - All sections below Details default collapsed with toggle arrows - Assigned To on view page is now a clickable dropdown like status - TT moved to Details section - Dropdown options settings page at /settings/dropdowns with add/edit/delete - Settings link in sidebar and user dropdown
521 lines
18 KiB
JavaScript
521 lines
18 KiB
JavaScript
import React, { useState, useEffect, useRef } from "react";
|
|
import ReactDOM from "react-dom";
|
|
import { useParams, useNavigate } from "react-router";
|
|
import { toast } from "react-toastify";
|
|
import { getWorkOrderById, addComment, changeWorkOrderStatus, fetchUsers, changeWorkOrderAssignment } from "../api";
|
|
import "./WorkOrderViewPage.css";
|
|
|
|
function AssignDropdown({ value, onAssign }) {
|
|
const [open, setOpen] = useState(false);
|
|
const [users, setUsers] = useState([]);
|
|
const [loaded, setLoaded] = useState(false);
|
|
const btnRef = useRef(null);
|
|
const menuRef = useRef(null);
|
|
const [menuPos, setMenuPos] = useState({ top: 0, left: 0 });
|
|
|
|
useEffect(() => {
|
|
const handleClick = (e) => {
|
|
if (btnRef.current?.contains(e.target)) return;
|
|
if (menuRef.current?.contains(e.target)) return;
|
|
setOpen(false);
|
|
};
|
|
document.addEventListener("mousedown", handleClick);
|
|
return () => document.removeEventListener("mousedown", handleClick);
|
|
}, []);
|
|
|
|
const handleOpen = async () => {
|
|
if (!loaded) {
|
|
try {
|
|
const data = await fetchUsers();
|
|
setUsers(data);
|
|
setLoaded(true);
|
|
} catch { return; }
|
|
}
|
|
if (btnRef.current) {
|
|
const rect = btnRef.current.getBoundingClientRect();
|
|
setMenuPos({ top: rect.bottom + 4, left: rect.left });
|
|
}
|
|
setOpen(!open);
|
|
};
|
|
|
|
const handleSelect = async (userId, userName) => {
|
|
setOpen(false);
|
|
onAssign(userId, userName);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<button ref={btnRef} className="woAssignBtn" onClick={handleOpen}>
|
|
{value || "Unassigned"} <span className="woStatusCaret">▾</span>
|
|
</button>
|
|
{open && ReactDOM.createPortal(
|
|
<div ref={menuRef} className="woAssignMenu" style={{ top: menuPos.top, left: menuPos.left }}>
|
|
<button className={`woAssignOption ${!value ? "active" : ""}`} onClick={() => handleSelect("", "")}>
|
|
Unassigned
|
|
</button>
|
|
{users.map((u) => (
|
|
<button key={u.id} className={`woAssignOption ${value === u.name ? "active" : ""}`} onClick={() => handleSelect(u.id, u.name)}>
|
|
{u.name}
|
|
</button>
|
|
))}
|
|
</div>,
|
|
document.body
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
function CollapsibleSection({ title, defaultOpen = false, children }) {
|
|
const [open, setOpen] = useState(defaultOpen);
|
|
return (
|
|
<div className="woViewSection">
|
|
<h4 className="woSectionToggle" onClick={() => setOpen(!open)}>
|
|
<span className={`woSectionArrow ${open ? "open" : ""}`}>▸</span>
|
|
{title}
|
|
</h4>
|
|
{open && children}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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);
|
|
const sorted = [...(comments || [])].sort((a, b) =>
|
|
new Date(a.createdDate) - new Date(b.createdDate)
|
|
);
|
|
|
|
useEffect(() => {
|
|
bottomRef.current?.scrollIntoView();
|
|
}, [sorted.length]);
|
|
|
|
if (!sorted.length) {
|
|
return <div className="woTabScroll"><p className="woNoComments">No comments yet.</p></div>;
|
|
}
|
|
|
|
return (
|
|
<div className="woTabScroll">
|
|
<div className="woTimeline">
|
|
{sorted.map((c) => (
|
|
<div key={c.id} className="woTimelineItem">
|
|
<div className="woTimelineHeader">
|
|
<span className="woTimelineAuthor">{c.userName || c.commenter || "System"}</span>
|
|
<span className="woTimelineDate">
|
|
{c.createdDate ? formatDateTime(c.createdDate) : ""}
|
|
</span>
|
|
</div>
|
|
<div className="woTimelineBody">{c.commenttext}</div>
|
|
</div>
|
|
))}
|
|
<div ref={bottomRef} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function AuditTimeline({ entries }) {
|
|
const bottomRef = useRef(null);
|
|
const sorted = [...(entries || [])].sort((a, b) =>
|
|
new Date(a.createdAt) - new Date(b.createdAt)
|
|
);
|
|
|
|
useEffect(() => {
|
|
bottomRef.current?.scrollIntoView();
|
|
}, [sorted.length]);
|
|
|
|
if (!sorted.length) {
|
|
return <div className="woTabScroll"><p className="woNoComments">No changes recorded.</p></div>;
|
|
}
|
|
|
|
return (
|
|
<div className="woTabScroll">
|
|
<div className="woTimeline">
|
|
{sorted.map((e) => (
|
|
<div key={e.id} className="woTimelineItem woAuditItem">
|
|
<div className="woTimelineHeader">
|
|
<span className="woTimelineAuthor">{e.userName || "System"}</span>
|
|
<span className="woTimelineDate">
|
|
{e.createdAt ? formatDateTime(e.createdAt) : ""}
|
|
</span>
|
|
</div>
|
|
<div className="woTimelineBody">
|
|
<span className="woAuditField">{e.fieldName}</span>
|
|
{e.oldValue && <span className="woAuditOld">{e.oldValue}</span>}
|
|
<span className="woAuditArrow">→</span>
|
|
<span className="woAuditNew">{e.newValue}</span>
|
|
</div>
|
|
</div>
|
|
))}
|
|
<div ref={bottomRef} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<form className="woCommentInput" onSubmit={handleSubmit}>
|
|
<textarea
|
|
className="woCommentTextarea"
|
|
value={text}
|
|
onChange={(e) => setText(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder="Type a comment... (Enter to send, Shift+Enter for new line)"
|
|
rows={2}
|
|
disabled={sending}
|
|
/>
|
|
<button className="btn btn-primary woCommentSend" type="submit" disabled={sending || !text.trim()}>
|
|
{sending ? "Sending..." : "Send"}
|
|
</button>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
const STATUS_OPTIONS = ["Open", "In Progress", "On Hold", "Done", "Cancelled"];
|
|
|
|
function StatusDropdown({ status, onStatusChange }) {
|
|
const [open, setOpen] = useState(false);
|
|
const btnRef = useRef(null);
|
|
const menuRef = useRef(null);
|
|
const [menuPos, setMenuPos] = useState({ top: 0, left: 0 });
|
|
const statusClass = (status || "").toLowerCase().replace(/\s+/g, "-");
|
|
|
|
useEffect(() => {
|
|
const handleClick = (e) => {
|
|
if (btnRef.current?.contains(e.target)) return;
|
|
if (menuRef.current?.contains(e.target)) return;
|
|
setOpen(false);
|
|
};
|
|
document.addEventListener("mousedown", handleClick);
|
|
return () => document.removeEventListener("mousedown", handleClick);
|
|
}, []);
|
|
|
|
const handleOpen = () => {
|
|
if (btnRef.current) {
|
|
const rect = btnRef.current.getBoundingClientRect();
|
|
setMenuPos({ top: rect.bottom + 4, left: rect.left });
|
|
}
|
|
setOpen(!open);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
ref={btnRef}
|
|
className={`woStatusBadge ${statusClass} woStatusClickable`}
|
|
onClick={handleOpen}
|
|
>
|
|
{status} <span className="woStatusCaret">▾</span>
|
|
</button>
|
|
{open && ReactDOM.createPortal(
|
|
<div
|
|
ref={menuRef}
|
|
className="woStatusMenu"
|
|
style={{ top: menuPos.top, left: menuPos.left }}
|
|
>
|
|
{STATUS_OPTIONS.map((s) => (
|
|
<button
|
|
key={s}
|
|
className={`woStatusOption ${s === status ? "active" : ""}`}
|
|
onClick={() => { onStatusChange(s); setOpen(false); }}
|
|
>
|
|
<span className={`woStatusDot ${s.toLowerCase().replace(/\s+/g, "-")}`} />
|
|
{s}
|
|
</button>
|
|
))}
|
|
</div>,
|
|
document.body
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
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");
|
|
const [statusUpdating, setStatusUpdating] = useState(false);
|
|
|
|
const handleStatusChange = async (newStatus) => {
|
|
if (newStatus === wo?.status) return;
|
|
setStatusUpdating(true);
|
|
try {
|
|
await changeWorkOrderStatus(id, newStatus);
|
|
const oldStatus = wo.status;
|
|
setWo((prev) => ({
|
|
...prev,
|
|
status: newStatus,
|
|
auditLog: [
|
|
{
|
|
id: Date.now(),
|
|
fieldName: "Status",
|
|
oldValue: oldStatus,
|
|
newValue: newStatus,
|
|
action: "status_change",
|
|
createdAt: new Date().toISOString(),
|
|
userName: "You",
|
|
},
|
|
...(prev.auditLog || []),
|
|
],
|
|
}));
|
|
toast.success(`Status changed to ${newStatus}`);
|
|
} catch (err) {
|
|
toast.error("Failed to update status");
|
|
} finally {
|
|
setStatusUpdating(false);
|
|
}
|
|
};
|
|
|
|
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 <div className="woViewPage"><div className="loading">Loading...</div></div>;
|
|
if (!wo) return <div className="woViewPage"><div className="loading">Work order not found.</div></div>;
|
|
|
|
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 (
|
|
<div className="woViewPage">
|
|
<div className="panel panel-default panel-connected">
|
|
<div className="panel-body">
|
|
<ol className="breadcrumb breadcrumb-alt">
|
|
<li>
|
|
<a href="#" onClick={(e) => { e.preventDefault(); navigate("/workorders"); }}>
|
|
Work Orders
|
|
</a>
|
|
</li>
|
|
<li className="active">{wo.internalWONumber}</li>
|
|
</ol>
|
|
|
|
<div className="woViewHeader">
|
|
<div className="woViewTitle">
|
|
<h2>{wo.title}</h2>
|
|
<StatusDropdown status={wo.status} onStatusChange={handleStatusChange} />
|
|
</div>
|
|
<div className="woViewActions">
|
|
<button className="btn btn-primary" onClick={() => navigate(`/workorders/edit/${id}`)}>
|
|
Edit
|
|
</button>
|
|
<button className="btn btn-default" onClick={() => navigate("/workorders")}>
|
|
Back to List
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="woViewGrid">
|
|
<div className="woViewSection">
|
|
<h4>Details</h4>
|
|
<div className="woFieldGrid">
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Internal WO</span>
|
|
<span className="woFieldValue">{wo.internalWONumber}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Customer WO</span>
|
|
<span className="woFieldValue">{wo.workOrderNumber || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Priority</span>
|
|
<span className="woFieldValue">{wo.priority || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Location</span>
|
|
<span className="woFieldValue">{wo.location || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Assigned To</span>
|
|
<span className="woFieldValue">
|
|
<AssignDropdown
|
|
value={wo.assignedTo}
|
|
onAssign={async (userId, userName) => {
|
|
try {
|
|
await changeWorkOrderAssignment(wo.id, userId);
|
|
setWo((prev) => ({ ...prev, assignedTo: userName }));
|
|
toast.success(`Assigned to ${userName || "Unassigned"}`);
|
|
} catch { toast.error("Failed to update assignment"); }
|
|
}}
|
|
/>
|
|
</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Source</span>
|
|
<span className="woFieldValue">{wo.source || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">TT</span>
|
|
<span className="woFieldValue">{wo.tt || "—"}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<CollapsibleSection title="Classification">
|
|
<div className="woFieldGrid">
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Problem</span>
|
|
<span className="woFieldValue">{wo.problem || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Trade</span>
|
|
<span className="woFieldValue">{wo.trade || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Sub-Trade</span>
|
|
<span className="woFieldValue">{wo.subTrade || "—"}</span>
|
|
</div>
|
|
</div>
|
|
</CollapsibleSection>
|
|
|
|
<CollapsibleSection title="Schedule">
|
|
<div className="woFieldGrid">
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Created</span>
|
|
<span className="woFieldValue">
|
|
{wo.createdDateFull ? formatDateTime(wo.createdDateFull) : "—"}
|
|
</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Due Date</span>
|
|
<span className="woFieldValue">{wo.dueDate || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Scheduled</span>
|
|
<span className="woFieldValue">{wo.scheduledDate || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Completed</span>
|
|
<span className="woFieldValue">{wo.completedDate || "—"}</span>
|
|
</div>
|
|
</div>
|
|
</CollapsibleSection>
|
|
|
|
<CollapsibleSection title="Financial">
|
|
<div className="woFieldGrid">
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Vendor NTE</span>
|
|
<span className="woFieldValue">{wo.vendorNTE ? `$${parseFloat(wo.vendorNTE).toFixed(2)}` : "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Customer PO</span>
|
|
<span className="woFieldValue">{wo.po || "—"}</span>
|
|
</div>
|
|
<div className="woField">
|
|
<span className="woFieldLabel">Customer NTE</span>
|
|
<span className="woFieldValue woFieldPlaceholder">Not connected</span>
|
|
</div>
|
|
</div>
|
|
</CollapsibleSection>
|
|
|
|
<CollapsibleSection title="Description">
|
|
<p className="woDescription">{wo.description || "No description provided."}</p>
|
|
</CollapsibleSection>
|
|
|
|
<div className="woViewSection">
|
|
<h4>Activity</h4>
|
|
<div className="woCommentTabs">
|
|
{tabs.map((tab) => (
|
|
<button
|
|
key={tab.key}
|
|
className={`woCommentTab ${activeTab === tab.key ? "active" : ""}`}
|
|
onClick={() => setActiveTab(tab.key)}
|
|
>
|
|
{tab.label} ({tab.count})
|
|
</button>
|
|
))}
|
|
</div>
|
|
<div className="woTabContainer">
|
|
{activeTab === "customer" && <CommentTimeline comments={customerComments} />}
|
|
{activeTab === "vendor" && <CommentTimeline comments={vendorComments} />}
|
|
{activeTab === "internal" && <CommentTimeline comments={internalComments} />}
|
|
{activeTab === "audit" && <AuditTimeline entries={auditLog} />}
|
|
{showInput && (
|
|
<CommentInput
|
|
workOrderId={parseInt(id)}
|
|
commentType={activeTab}
|
|
onCommentAdded={handleCommentAdded}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|