diff options
Diffstat (limited to 'components/form-data-plant/publish-dialog.tsx')
| -rw-r--r-- | components/form-data-plant/publish-dialog.tsx | 226 |
1 files changed, 90 insertions, 136 deletions
diff --git a/components/form-data-plant/publish-dialog.tsx b/components/form-data-plant/publish-dialog.tsx index f63c2db8..a3a2ef0b 100644 --- a/components/form-data-plant/publish-dialog.tsx +++ b/components/form-data-plant/publish-dialog.tsx @@ -37,21 +37,19 @@ import { Loader2, Check, ChevronsUpDown } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { - createSubmissionAction, // 새로운 액션 이름 - fetchDocumentsByProjectAndPackage, // 업데이트된 액션 - fetchStagesByDocumentIdPlant, - fetchSubmissionsByStageParams, // revisions 대신 submissions + createRevisionAction, + fetchDocumentsByPackageId, + fetchStagesByDocumentId, + fetchRevisionsByStageParams, + Document, + IssueStage, + Revision } from "@/lib/vendor-document/service"; -import type { - StageDocument, - StageIssueStage, -} from "@/db/schema/vendorDocu"; interface PublishDialogProps { open: boolean; onOpenChange: (open: boolean) => void; - projectCode: string; - packageCode: string; + packageId: number; formCode: string; fileBlob?: Blob; } @@ -59,8 +57,7 @@ interface PublishDialogProps { export const PublishDialog: React.FC<PublishDialogProps> = ({ open, onOpenChange, - projectCode, - packageCode, + packageId, formCode, fileBlob, }) => { @@ -68,10 +65,9 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ const { data: session } = useSession(); // State for form data - const [documents, setDocuments] = useState<StageDocument[]>([]); - const [stages, setStages] = useState<StageIssueStage[]>([]); - const [latestRevisionCode, setLatestRevisionCode] = useState<string>(""); - const [latestRevisionNumber, setLatestRevisionNumber] = useState<number>(0); + const [documents, setDocuments] = useState<Document[]>([]); + const [stages, setStages] = useState<IssueStage[]>([]); + const [latestRevision, setLatestRevision] = useState<string>(""); // State for document search const [openDocumentCombobox, setOpenDocumentCombobox] = useState(false); @@ -81,10 +77,9 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ const [selectedDocId, setSelectedDocId] = useState<string>(""); const [selectedDocumentDisplay, setSelectedDocumentDisplay] = useState<string>(""); const [selectedStage, setSelectedStage] = useState<string>(""); - const [revisionCodeInput, setRevisionCodeInput] = useState<string>(""); - const [submitterName, setSubmitterName] = useState<string>(""); - const [submissionTitle, setSubmissionTitle] = useState<string>(""); - const [submissionDescription, setSubmissionDescription] = useState<string>(""); + const [revisionInput, setRevisionInput] = useState<string>(""); + const [uploaderName, setUploaderName] = useState<string>(""); + const [comment, setComment] = useState<string>(""); const [customFileName, setCustomFileName] = useState<string>(`${formCode}_document.docx`); // Loading states @@ -99,10 +94,10 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ ) : documents; - // Set submitter name from session when dialog opens + // Set uploader name from session when dialog opens useEffect(() => { if (open && session?.user?.name) { - setSubmitterName(session.user.name); + setUploaderName(session.user.name); } }, [open, session]); @@ -112,26 +107,24 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ setSelectedDocId(""); setSelectedDocumentDisplay(""); setSelectedStage(""); - setRevisionCodeInput(""); - setSubmissionTitle(""); - setSubmissionDescription(""); - // Only set submitterName if not already set from session - if (!session?.user?.name) setSubmitterName(""); - setLatestRevisionCode(""); - setLatestRevisionNumber(0); + setRevisionInput(""); + // Only set uploaderName if not already set from session + if (!session?.user?.name) setUploaderName(""); + setComment(""); + setLatestRevision(""); setCustomFileName(`${formCode}_document.docx`); setDocumentSearchValue(""); } }, [open, formCode, session]); - // Fetch documents based on projectCode and packageCode + // Fetch documents based on packageId useEffect(() => { async function loadDocuments() { - if (projectCode && packageCode && open) { + if (packageId && open) { setIsLoading(true); try { - const docs = await fetchDocumentsByProjectAndPackage(projectCode, packageCode); + const docs = await fetchDocumentsByPackageId(packageId); setDocuments(docs); } catch (error) { console.error("Error fetching documents:", error); @@ -143,7 +136,7 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ } loadDocuments(); - }, [projectCode, packageCode, open]); + }, [packageId, open]); // Fetch stages when document is selected useEffect(() => { @@ -153,12 +146,11 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ // Reset dependent fields setSelectedStage(""); - setRevisionCodeInput(""); - setLatestRevisionCode(""); - setLatestRevisionNumber(0); + setRevisionInput(""); + setLatestRevision(""); try { - const stagesList = await fetchStagesByDocumentIdPlant(parseInt(selectedDocId, 10)); + const stagesList = await fetchStagesByDocumentId(parseInt(selectedDocId, 10)); setStages(stagesList); } catch (error) { console.error("Error fetching stages:", error); @@ -174,78 +166,65 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ loadStages(); }, [selectedDocId]); - // Fetch latest submission (revision) when stage is selected + // Fetch latest revision when stage is selected (for reference) useEffect(() => { - async function loadLatestSubmission() { + async function loadLatestRevision() { if (selectedDocId && selectedStage) { setIsLoading(true); try { - const submissionsList = await fetchSubmissionsByStageParams( + const revsList = await fetchRevisionsByStageParams( parseInt(selectedDocId, 10), selectedStage ); - // Find the latest submission (assuming sorted by revision number) - if (submissionsList.length > 0) { - // Sort submissions by revision number descending - const sortedSubmissions = [...submissionsList].sort((a, b) => - b.revisionNumber - a.revisionNumber - ); + // Find the latest revision (assuming revisions are sorted by revision number) + if (revsList.length > 0) { + // Sort revisions if needed + const sortedRevisions = [...revsList].sort((a, b) => { + return b.revision.localeCompare(a.revision, undefined, { numeric: true }); + }); - const latestSubmission = sortedSubmissions[0]; - setLatestRevisionCode(latestSubmission.revisionCode); - setLatestRevisionNumber(latestSubmission.revisionNumber); + setLatestRevision(sortedRevisions[0].revision); - // Auto-increment revision code - if (latestSubmission.revisionCode.match(/^\d+$/)) { + // Pre-fill the revision input with an incremented value if possible + if (sortedRevisions[0].revision.match(/^\d+$/)) { // If it's a number, increment it - const nextRevision = String(parseInt(latestSubmission.revisionCode, 10) + 1); - setRevisionCodeInput(nextRevision); - } else if (latestSubmission.revisionCode.match(/^[A-Za-z]$/)) { + const nextRevision = String(parseInt(sortedRevisions[0].revision, 10) + 1); + setRevisionInput(nextRevision); + } else if (sortedRevisions[0].revision.match(/^[A-Za-z]$/)) { // If it's a single letter, get the next letter - const currentChar = latestSubmission.revisionCode.charCodeAt(0); + const currentChar = sortedRevisions[0].revision.charCodeAt(0); const nextChar = String.fromCharCode(currentChar + 1); - setRevisionCodeInput(nextChar); - } else if (latestSubmission.revisionCode.toLowerCase().startsWith("rev")) { - // Handle "Rev0", "Rev1" format - const numMatch = latestSubmission.revisionCode.match(/\d+$/); - if (numMatch) { - const nextNum = parseInt(numMatch[0], 10) + 1; - setRevisionCodeInput(`Rev${nextNum}`); - } else { - setRevisionCodeInput(""); - } + setRevisionInput(nextChar); } else { // For other formats, just show the latest as reference - setRevisionCodeInput(""); + setRevisionInput(""); } } else { - // If no submissions exist, set default values - setLatestRevisionCode(""); - setLatestRevisionNumber(0); - setRevisionCodeInput("Rev0"); // Start with Rev0 + // If no revisions exist, set default values + setLatestRevision(""); + setRevisionInput("0"); } } catch (error) { - console.error("Error fetching submissions:", error); - toast.error("Failed to load submission information"); + console.error("Error fetching revisions:", error); + toast.error("Failed to load revision information"); } finally { setIsLoading(false); } } else { - setLatestRevisionCode(""); - setLatestRevisionNumber(0); - setRevisionCodeInput(""); + setLatestRevision(""); + setRevisionInput(""); } } - loadLatestSubmission(); + loadLatestRevision(); }, [selectedDocId, selectedStage]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - if (!selectedDocId || !selectedStage || !revisionCodeInput || !fileBlob) { + if (!selectedDocId || !selectedStage || !revisionInput || !fileBlob) { toast.error("Please fill in all required fields"); return; } @@ -256,30 +235,17 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ // Create FormData const formData = new FormData(); formData.append("documentId", selectedDocId); - formData.append("stageName", selectedStage); - formData.append("revisionCode", revisionCodeInput); + formData.append("stage", selectedStage); + formData.append("revision", revisionInput); formData.append("customFileName", customFileName); + formData.append("uploaderType", "vendor"); // Default value - if (submitterName) { - formData.append("submittedBy", submitterName); + if (uploaderName) { + formData.append("uploaderName", uploaderName); } - if (session?.user?.email) { - formData.append("submittedByEmail", session.user.email); - } - - if (submissionTitle) { - formData.append("submissionTitle", submissionTitle); - } - - if (submissionDescription) { - formData.append("submissionDescription", submissionDescription); - } - - // Get vendor info from selected document - const selectedDoc = documents.find(doc => doc.id === parseInt(selectedDocId, 10)); - if (selectedDoc) { - formData.append("vendorId", String(selectedDoc.vendorId)); + if (comment) { + formData.append("comment", comment); } // Append file as attachment @@ -290,14 +256,12 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ formData.append("attachment", file); } - // Call server action - const result = await createSubmissionAction(formData); + // Call server action directly + const result = await createRevisionAction(formData); - if (result.success) { + if (result) { toast.success("Document published successfully!"); onOpenChange(false); - } else { - toast.error(result.error || "Failed to publish document"); } } catch (error) { console.error("Error publishing document:", error); @@ -337,6 +301,7 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ className="w-full justify-between" disabled={isLoading || documents.length === 0} > + {/* Add text-overflow handling for selected document display */} <span className="truncate"> {selectedDocumentDisplay ? selectedDocumentDisplay @@ -373,6 +338,7 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ : "opacity-0" )} /> + {/* Add text-overflow handling for document items */} <span className="truncate">{doc.docNumber} - {doc.title}</span> </CommandItem> ))} @@ -400,6 +366,7 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ <SelectContent> {stages.map((stage) => ( <SelectItem key={stage.id} value={stage.stageName}> + {/* Add text-overflow handling for stage names */} <span className="truncate">{stage.stageName}</span> </SelectItem> ))} @@ -408,42 +375,28 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ </div> </div> - {/* Revision Code Input */} + {/* Revision Input */} <div className="grid grid-cols-4 items-center gap-4"> - <Label htmlFor="revisionCode" className="text-right"> + <Label htmlFor="revision" className="text-right"> Revision </Label> <div className="col-span-3"> <Input - id="revisionCode" - value={revisionCodeInput} - onChange={(e) => setRevisionCodeInput(e.target.value)} - placeholder="Enter revision code (e.g., Rev0, A, 1)" + id="revision" + value={revisionInput} + onChange={(e) => setRevisionInput(e.target.value)} + placeholder="Enter revision" disabled={isLoading || !selectedStage} /> - {latestRevisionCode && ( + {latestRevision && ( <p className="text-xs text-muted-foreground mt-1"> - Latest revision: {latestRevisionCode} (#{latestRevisionNumber}) + Latest revision: {latestRevision} </p> )} </div> </div> <div className="grid grid-cols-4 items-center gap-4"> - <Label htmlFor="submissionTitle" className="text-right"> - Title - </Label> - <div className="col-span-3"> - <Input - id="submissionTitle" - value={submissionTitle} - onChange={(e) => setSubmissionTitle(e.target.value)} - placeholder="Optional submission title" - /> - </div> - </div> - - <div className="grid grid-cols-4 items-center gap-4"> <Label htmlFor="fileName" className="text-right"> File Name </Label> @@ -458,15 +411,16 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ </div> <div className="grid grid-cols-4 items-center gap-4"> - <Label htmlFor="submitterName" className="text-right"> - Submitter + <Label htmlFor="uploaderName" className="text-right"> + Uploader </Label> <div className="col-span-3"> <Input - id="submitterName" - value={submitterName} - onChange={(e) => setSubmitterName(e.target.value)} + id="uploaderName" + value={uploaderName} + onChange={(e) => setUploaderName(e.target.value)} placeholder="Your name" + // Disable input but show a filled style className={session?.user?.name ? "opacity-70" : ""} readOnly={!!session?.user?.name} /> @@ -479,15 +433,15 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ </div> <div className="grid grid-cols-4 items-center gap-4"> - <Label htmlFor="description" className="text-right"> - Description + <Label htmlFor="comment" className="text-right"> + Comment </Label> <div className="col-span-3"> <Textarea - id="description" - value={submissionDescription} - onChange={(e) => setSubmissionDescription(e.target.value)} - placeholder="Optional submission description" + id="comment" + value={comment} + onChange={(e) => setComment(e.target.value)} + placeholder="Optional comment" className="resize-none" /> </div> @@ -497,7 +451,7 @@ export const PublishDialog: React.FC<PublishDialogProps> = ({ <DialogFooter> <Button type="submit" - disabled={isSubmitting || !selectedDocId || !selectedStage || !revisionCodeInput} + disabled={isSubmitting || !selectedDocId || !selectedStage || !revisionInput} > {isSubmitting ? ( <> |
