-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (123 loc) · 6 KB
/
app.js
File metadata and controls
140 lines (123 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
document.addEventListener('DOMContentLoaded', () => {
// --- Element Selectors ---
const surplusEstimateDisplay = document.querySelector('#surplus-estimate span');
const calculateSurplusBtn = document.getElementById('calculate-surplus');
const surplusForm = document.getElementById('surplus-form');
const marketplaceList = document.getElementById('marketplace-list');
const downloadReportBtn = document.getElementById('download-report');
// --- Initial State ---
let surplusItems = JSON.parse(localStorage.getItem('surplusItems')) || [];
// --- Functions ---
/**
* Renders the surplus items in the marketplace view.
*/
const renderMarketplace = () => {
marketplaceList.innerHTML = '';
if (surplusItems.length === 0) {
marketplaceList.innerHTML = '<p>No surplus items available at the moment.</p>';
return;
}
surplusItems.forEach(item => {
const itemElement = document.createElement('div');
itemElement.classList.add('item');
itemElement.setAttribute('data-id', item.id);
itemElement.innerHTML = `
<div class="info">
<p><strong>${item.itemType}</strong></p>
<p>Quantity: ${item.quantity}</p>
<p>Expires: ${item.expiryDate}</p>
<p>Status: <span class="status">${item.status}</span></p>
</div>
<div class="actions">
${item.status === 'Available' ?
`<button class="accept-btn" aria-label="Accept ${item.itemType}">Accept</button>
<button class="reject-btn" aria-label="Reject ${item.itemType}">Reject</button>` :
''}
</div>
`;
marketplaceList.appendChild(itemElement);
});
};
/**
* Updates the status of a surplus item.
* @param {string} id - The ID of the item to update.
* @param {string} status - The new status of the item.
*/
const updateItemStatus = (id, status) => {
surplusItems = surplusItems.map(item =>
item.id === id ? { ...item, status } : item
);
localStorage.setItem('surplusItems', JSON.stringify(surplusItems));
renderMarketplace();
// --- MCP Integration (HubSpot) ---
// Here, you would make an MCP call to HubSpot to update the CRM with the new item status.
// console.log(`MCP_CALL: hubspot.update_deal(${id}, { status: '${status}' })`);
};
/**
* Downloads the impact report as a CSV file.
*/
const downloadImpactReport = () => {
let csvContent = "data:text/csv;charset=utf-8,Item Type,Quantity,Status,Waste Diverted (kg),Carbon Saved (kgCO2e)\n";
surplusItems.forEach(item => {
const wasteDiverted = (item.quantity * 0.5).toFixed(2); // Assuming average weight of 0.5kg per item
const carbonSaved = (wasteDiverted * 1.5).toFixed(2); // Assuming 1.5 kgCO2e saved per kg of waste
csvContent += `${item.itemType},${item.quantity},${item.status},${wasteDiverted},${carbonSaved}\n`;
});
// --- MCP Integration (Excel) ---
// Here, an MCP call could be made to generate a more sophisticated Excel report.
// console.log('MCP_CALL: excel.create_report(data)');
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "impact_report.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
// --- Event Listeners ---
// Calculate Surplus
calculateSurplusBtn.addEventListener('click', () => {
const plannedPax = document.getElementById('planned-pax').value;
const actualAttendance = document.getElementById('actual-attendance').value;
const surplus = plannedPax - actualAttendance;
surplusEstimateDisplay.textContent = surplus > 0 ? surplus : 0;
});
// List Surplus Item
surplusForm.addEventListener('submit', (e) => {
e.preventDefault();
const newItem = {
id: `item-${Date.now()}`,
itemType: document.getElementById('item-type').value,
quantity: document.getElementById('quantity').value,
expiryDate: document.getElementById('expiry-date').value,
pickupAddress: document.getElementById('pickup-address').value,
status: 'Available'
};
surplusItems.push(newItem);
localStorage.setItem('surplusItems', JSON.stringify(surplusItems));
renderMarketplace();
surplusForm.reset();
// --- MCP Integrations ---
// 1. HubSpot: Log the new donor and item.
// console.log(`MCP_CALL: hubspot.create_contact({ email: 'donor@example.com', address: '${newItem.pickupAddress}' })`);
// console.log(`MCP_CALL: hubspot.create_deal({ title: '${newItem.itemType}', amount: ${newItem.quantity} })`);
// 2. Google Maps: Geocode the address to get coordinates.
// console.log(`MCP_CALL: maps.geocode({ address: '${newItem.pickupAddress}' })`);
});
// Accept/Reject Buttons
marketplaceList.addEventListener('click', (e) => {
const id = e.target.closest('.item').dataset.id;
if (e.target.classList.contains('accept-btn')) {
updateItemStatus(id, 'Claimed');
// --- MCP Integration (Google Calendar) ---
// Here, you would make an MCP call to Google Calendar to schedule a pickup.
// console.log('MCP_CALL: calendar.create_event({ title: `Pickup for ${id}`, time: ... })');
} else if (e.target.classList.contains('reject-btn')) {
updateItemStatus(id, 'Available'); // Or perhaps 'Rejected' and then remove from view
}
});
// Download Report
downloadReportBtn.addEventListener('click', downloadImpactReport);
// --- Initial Render ---
renderMarketplace();
});