Skip to content

Commit ca82522

Browse files
Merge pull request #115 from JumboCode/37-will
37 will
2 parents 4dc25af + 91b0504 commit ca82522

File tree

2 files changed

+46
-80
lines changed

2 files changed

+46
-80
lines changed

frontend/src/app/components/ReturnPopup.jsx

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -28,85 +28,6 @@ const ReturnPopup = ({ units = [], onSuccess, onClose }) => {
2828
}, [units, currentPage]);
2929

3030
const handleReturn = async () => {
31-
//try {
32-
33-
//EMAIL FOR RETURN ITEMS BELOW
34-
35-
//const itemNames = units.map(item => item.name);
36-
37-
// // Fetch the borrower's ID based on the first selected item
38-
// const borrowerID = await fetch (`/api/fetchBorrowerId?id=${units[0].id}`);
39-
// if (!borrowerId.ok) {
40-
// throw new Error(`Failed to fetch borrower ID: ${borrowerId.status}`);
41-
// }
42-
// const { borrower_id: borrowerId } = await borrowerID.json();
43-
44-
// // Fetch the borrower's email using the first item's borrower_id
45-
// console.log("Borrower id:" + borrower_id);
46-
// const borrowerEmailResponse = await fetch(`/api/fetchBorrowerEmail?id=${borrower_id}`);
47-
// if (!borrowerEmailResponse.ok) {
48-
// throw new Error(`Failed to fetch borrower email: ${borrowerEmailResponse.status}`);
49-
// }
50-
// const { borrower_email: borrowerEmail } = await borrowerEmailResponse.json();
51-
52-
// const borrowerNameResponse = await fetch(`/api/fetchBorrowerName?id=${borrowerId}`);
53-
// if (!borrowerNameResponse.ok) {
54-
// throw new Error(`Failed to fetch borrower name: ${borrowerNameResponse.status}`);
55-
// }
56-
// const { borrower_first_name: borrowerFirstName, borrower_last_name: borrowerLastName } = await borrowerNameResponse.json();
57-
58-
// Step 2: Call the groupReturnsByBorrowerHandler API
59-
60-
////////////////////////////////////////////////////
61-
62-
// try {
63-
// const response = await fetch('/api/borrowManagement?action=groupReturnsByBorrower', {
64-
// method: 'POST',
65-
// headers: { 'Content-Type': 'application/json' },
66-
// body: JSON.stringify({ returnedItems: units.map(item => item.id) }),
67-
// });
68-
69-
// if (!response.ok) {
70-
// throw new Error(`Group API error: ${response.status} - ${response.statusText}`);
71-
// }
72-
73-
// const result = await response.json();
74-
// console.log(result.message); // Should log: Emails sent to all borrowers.
75-
76-
// if (onSuccess) onSuccess();
77-
78-
// location.reload();
79-
80-
// } catch (error) {
81-
// console.error("Error returning data:", error);
82-
// }
83-
84-
// try {
85-
// const response = await fetch('../../api/borrowManagement?action=return', {
86-
// method: 'PUT',
87-
// headers: { 'Content-Type': 'application/json' },
88-
// body: JSON.stringify({
89-
// selectedItems: units.map(item => item.id),
90-
// notes_id: Object.keys(notes),
91-
// notes_content: Object.values(notes)
92-
// })
93-
// });
94-
95-
// if (!response.ok) {
96-
// throw new Error(`Fetch error: ${response.status} - ${response.statusText}`);
97-
// }
98-
99-
// const result = await response.json();
100-
// if (result.message) alert(result.message);
101-
// if (onSuccess) onSuccess();
102-
103-
// location.reload();
104-
// } catch (error) {
105-
// console.error("Error returning data:", error);
106-
// }
107-
// if (selectedUnits == 0) {
108-
// alert('No items selected.');
109-
// } else {
11031

11132
try {
11233
// 1. Send Emails first

frontend/src/app/inventory/15Tablecomp/PrePopup.jsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,53 @@ export default function PrePopup({ unit, onClose, onOptionSelect, position, stat
1212
onOptionSelect("expand");
1313
};
1414

15-
const handleFoundClick = () => {
15+
const handleFoundClick = async() => {
1616
onOptionSelect("Available");
17+
18+
try {
19+
// 1. Send Emails first
20+
const emailResponse = await fetch('/api/borrowManagement?action=groupReturnsByBorrower', {
21+
method: 'POST',
22+
headers: { 'Content-Type': 'application/json' },
23+
body: JSON.stringify({ returnedItems: [unit.id] }),
24+
});
25+
26+
if (!emailResponse.ok) {
27+
throw new Error(`Group API error: ${emailResponse.status} - ${emailResponse.statusText}`);
28+
}
29+
30+
const emailResult = await emailResponse.json();
31+
console.log(emailResult.message); // Should log: Emails sent to all borrowers.
32+
33+
// 2. THEN update DB
34+
const returnResponse = await fetch('/api/borrowManagement?action=return', {
35+
method: 'PUT',
36+
headers: { 'Content-Type': 'application/json' },
37+
body: JSON.stringify({
38+
selectedItems: [unit.id],
39+
notes_id: [],
40+
notes_content: [],
41+
}),
42+
});
43+
44+
if (!returnResponse.ok) {
45+
throw new Error(`Fetch error: ${returnResponse.status} - ${returnResponse.statusText}`);
46+
}
47+
48+
const returnResult = await returnResponse.json();
49+
50+
// Show success popup instead of alert
51+
setIsSuccessPopupVisible(true);
52+
53+
// Don't immediately reload - this will happen when user confirms in popup
54+
55+
} catch (error) {
56+
if (error.message.includes("404") || error.message.includes("No borrower")) {
57+
console.info("No borrower to notify — skipping email step.");
58+
} else {
59+
console.error("Unexpected error:", error);
60+
}
61+
}
1762
};
1863

1964
const handleMissingClick = () => {

0 commit comments

Comments
 (0)