Skip to content

Commit 0b7a98a

Browse files
Merge pull request #153 from oss-slu/140-api-error-messages-extract-sections
Enhancing Section Input Flexibility
2 parents 5a14ea0 + 12dc32e commit 0b7a98a

File tree

2 files changed

+62
-28
lines changed

2 files changed

+62
-28
lines changed

client-app/src/components/OrcaDashboardComponent.js

+51-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState } from "react";
22
import axios from "axios";
3+
import { useEffect } from "react";
34
import { saveAs } from "file-saver";
45
import { FaDownload } from "react-icons/fa6";
56
import "../styles/OrcaDashboardComponent.css";
@@ -19,7 +20,7 @@ const OrcaDashboardComponent = () => {
1920
const isSectionsEmpty = sections.length === 0;
2021
const [sameCriteria, setSameCriteria] = useState(false);
2122
const [previewContent, setPreviewContent] = useState("");
22-
const [showPreviewModal, setShowPreviewModal] = useState(false);
23+
const [showPreviewModal, setShowPreviewModal] = useState(false);
2324
const [selectedFileName, setSelectedFileName] = useState("No file chosen");
2425

2526
const onFileSelected = (event) => {
@@ -143,10 +144,11 @@ const OrcaDashboardComponent = () => {
143144
downloadDocument(blob);
144145
})
145146
.catch((error) => {
146-
if (error.response){
147+
if (error.response) {
147148
if (error.response.status === 404) {
148149
alert("There is no data for the provided search term");
149-
} else {alert(`Error ${error.response.status}: ${error.response.statusText}`);
150+
} else {
151+
alert(`Error ${error.response.status}: ${error.response.statusText}`);
150152
}
151153
}
152154
});
@@ -201,14 +203,29 @@ const OrcaDashboardComponent = () => {
201203
};
202204

203205
const handleNumSectionsBlur = (e) => {
204-
const parsedSections = e.target.value
205-
.split(",")
206-
.map((val) => val.trim())
207-
.filter((val) => val !== "");
206+
const input = e.target.value;
207+
let parsedSections = new Set();
208+
209+
input.split(",").forEach((part) => {
210+
part = part.trim();
211+
if (part.includes("-")) {
212+
const [start, end] = part.split("-").map((num) => parseInt(num.trim(), 10));
213+
if (!isNaN(start) && !isNaN(end) && start <= end) {
214+
for (let i = start; i <= end; i++) {
215+
parsedSections.add(i);
216+
}
217+
}
218+
} else {
219+
const num = parseInt(part, 10);
220+
if (!isNaN(num)) {
221+
parsedSections.add(num);
222+
}
223+
}
224+
});
208225

209-
setSections(parsedSections);
226+
setSections(Array.from(parsedSections).sort((a, b) => a - b));
210227
};
211-
228+
212229
const removeTag = (index, setterFunc) => {
213230
setterFunc((prevTerms) => {
214231
const updatedTerms = [...prevTerms];
@@ -257,14 +274,26 @@ const OrcaDashboardComponent = () => {
257274
});
258275
};
259276

277+
useEffect(() => {
278+
const handleKeyDown = (event) => {
279+
if (event.key === "Escape") {
280+
setShowPreviewModal(false);
281+
}
282+
};
283+
284+
document.addEventListener("keydown", handleKeyDown);
285+
return () => document.removeEventListener("keydown", handleKeyDown);
286+
}, []);
287+
260288
return (
261289
<div className="container py-5 d-flex justify-content-center">
262290
<div className="text-center">
263291
<h2 className="mb-4">Extract data from ORCA files to Word documents</h2>
264292
<div className="mb-3 text-start">
265-
<label htmlFor="fileUpload" className="mb-2">Upload your ORCA data file:</label>
293+
<label htmlFor="fileUpload" className="mb-2">
294+
Upload your ORCA data file:
295+
</label>
266296
<div className="input-group">
267-
268297
<input
269298
className="form-control"
270299
type="file"
@@ -294,7 +323,9 @@ const OrcaDashboardComponent = () => {
294323
</div>
295324

296325
<div className="mb-3 text-start">
297-
<label htmlFor="searchTermInput" className="mb-2">Enter the terms you wish to search for (txt only):</label>
326+
<label htmlFor="searchTermInput" className="mb-2">
327+
Enter the terms you wish to search for (txt only):
328+
</label>
298329
<div>
299330
<input
300331
type="text"
@@ -331,12 +362,16 @@ const OrcaDashboardComponent = () => {
331362
</div>
332363

333364
<div className="mb-3 text-start">
334-
<label htmlFor="specifyLinesSelect" className="mb-2">Enter how you want the lines specified:</label>
365+
<label htmlFor="specifyLinesSelect" className="mb-2">
366+
Enter how you want the lines specified:
367+
</label>
335368
{renderSpecifyLine()}
336369
</div>
337370

338371
<div className="mb-3 text-start">
339-
<label htmlFor="numSectionsInput" className="mb-2">Number of sections?</label>
372+
<label htmlFor="numSectionsInput" className="mb-2">
373+
Number of sections?
374+
</label>
340375
<input
341376
type="text"
342377
className="form-control"
@@ -428,10 +463,7 @@ const OrcaDashboardComponent = () => {
428463
!selectedFile ||
429464
isUploadedFilesEmpty
430465
}>
431-
<FaDownload
432-
size="1.2em"
433-
title="Download Output"
434-
/>
466+
<FaDownload size="1.2em" title="Download Output" />
435467
</button>
436468
<button className="btn btn-secondary" onClick={() => setShowPreviewModal(false)}>
437469
Close
@@ -453,10 +485,7 @@ const OrcaDashboardComponent = () => {
453485
!selectedFile ||
454486
isUploadedFilesEmpty
455487
}>
456-
<FaDownload
457-
size="1.2em"
458-
title="Download Output"
459-
/>
488+
<FaDownload size="1.2em" title="Download Output" />
460489
</button>
461490
</div>
462491
</div>

server/services/file_search_operations.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,20 @@ def is_end_pattern(lines, index):
6565
return ""
6666

6767
for term in search_terms:
68-
term_line_num = term_line_nums[term]
69-
for i in range(len(sections)):
70-
section_lines = specify_lines[i].split()
71-
start_line = term_line_num[i] if i < len(term_line_num) else None
68+
term_line_num = term_line_nums.get(term, [])
69+
70+
for section_number in sections:
71+
if section_number > len(specify_lines):
72+
continue
73+
74+
section_lines = specify_lines[section_number - 1].split()
75+
start_line = term_line_num[section_number - 1] if section_number - 1 < len(term_line_num) else None
76+
7277
if start_line is None:
73-
continue # Skip this section if the term is not found
78+
continue
7479

7580
line_empty = 0
76-
document_content += lines[start_line]
81+
document_content += lines[start_line] + "\n"
7782

7883
if section_lines[0].upper() == 'WHOLE' and not use_total_lines:
7984
while line_empty == 0:

0 commit comments

Comments
 (0)