-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-deets.js
More file actions
455 lines (404 loc) · 21.7 KB
/
Copy pathproject-deets.js
File metadata and controls
455 lines (404 loc) · 21.7 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
let deleteConfirmTimer = null;
document.addEventListener('click', async (e) => {
// 1. Check for card clicks to open overlay
// 2. Check for close button or backdrop clicks to close overlay
// 3. Check for the "Connect to Hackatime" button
// Find the closest parent with the class 'project-card'
const card = e.target.closest('.project-card');
const overlay = document.getElementById('overlay');
if (card && overlay) {
const projectName = card.id;
const email = card.dataset.email; // Correctly access data-email via dataset
const hours = card.dataset.hours; // Correctly access data-hours via dataset
const id = card.dataset.id;
const htConnected = card.dataset.htconnected;
// Instead of reading the stringified "[object Object]" from the DOM,
// find the actual record object in the local storage array using the ID.
const airtableProjects = JSON.parse(localStorage.getItem('airtableProjects') || '[]');
const record = airtableProjects.find(r => r.id === id);
localStorage.setItem('selectedProjectId', id);
/*
try {
// Fetch the external HTML file
const response = await fetch('project-details-template.html');
const template = await response.text();
// Chain replacements for all placeholders
const finalHtml = template
.replace(/{{PROJECT_NAME}}/g, projectName)
.replace(/{{EMAIL}}/g, email)
.replace(/{{HOURS}}/g, hours);
overlay.innerHTML = finalHtml;
overlay.style.display = 'block';
} catch (error) {
console.error('Error loading project details template:', error);
}*/
updateOverlay(false);
} else if (overlay && (e.target === overlay || e.target.classList.contains('close-btn'))) {
localStorage.removeItem('selectedProjectId');
localStorage.removeItem('card');
overlay.style.display = 'none';
} else if (e.target && e.target.id === 'connect-project') {
e.preventDefault();
} else if (e.target.closest('#ship-btn')) {
const btn = e.target.closest('#ship-btn');
const currentShipped = btn.dataset.shipped === 'true';
const newShipped = !currentShipped;
const recordId = localStorage.getItem('selectedProjectId');
if (!recordId) return;
try {
const calls = [
fetch('/api/patch-project-details', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ recordId, field: 'Shipped', value: newShipped })
})
];
// Re-shipping clears the Returned flag so it doesn't still show as returned
if (newShipped) {
calls.push(fetch('/api/patch-project-details', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ recordId, field: 'Returned', value: null })
}));
}
const [shipRes] = await Promise.all(calls);
const data = await shipRes.json();
if (data.success) {
const airtableProjects = JSON.parse(localStorage.getItem('airtableProjects') || '[]');
const record = airtableProjects.find(r => r.id === recordId);
if (record) {
record.fields['Shipped'] = newShipped;
if (newShipped) record.fields['Returned'] = null;
localStorage.setItem('airtableProjects', JSON.stringify(airtableProjects));
}
updateOverlay(false);
}
} catch (err) {
console.error('Error toggling shipped:', err);
}
} else if (e.target.closest('#delete-btn')) {
const btn = document.getElementById('delete-btn');
if (!btn) return;
if (btn.dataset.confirming !== 'true') {
// First click: arm the button
btn.dataset.confirming = 'true';
btn.textContent = 'Confirm Delete?';
btn.style.borderColor = 'red';
btn.style.color = 'red';
deleteConfirmTimer = setTimeout(() => {
if (btn.dataset.confirming === 'true') {
btn.dataset.confirming = 'false';
btn.textContent = 'Delete Project';
btn.style.borderColor = '#5a0000';
btn.style.color = '#a00000';
}
}, 5000);
return;
}
// Second click: confirmed
clearTimeout(deleteConfirmTimer);
const recordId = localStorage.getItem('selectedProjectId');
const email = localStorage.getItem('email');
if (!recordId || !email) return;
btn.disabled = true;
btn.textContent = 'Deleting...';
try {
const res = await fetch('/api/delete-project', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ recordId, email })
});
const data = await res.json();
if (data.success) {
const airtableProjects = JSON.parse(localStorage.getItem('airtableProjects') || '[]');
localStorage.setItem('airtableProjects', JSON.stringify(airtableProjects.filter(r => r.id !== recordId)));
localStorage.removeItem('selectedProjectId');
const overlay = document.getElementById('overlay');
if (overlay) overlay.style.display = 'none';
if (typeof fetchProjects === 'function') fetchProjects(email);
} else {
btn.disabled = false;
btn.textContent = 'Error — try again';
btn.style.borderColor = 'red';
btn.style.color = 'red';
}
} catch {
btn.disabled = false;
btn.textContent = 'Error — try again';
}
} else if (e.target.closest('#save-override-response-btn')) {
const recordId = localStorage.getItem('selectedProjectId');
if (!recordId) return;
const input = document.getElementById('override-response-input');
const statusEl = document.getElementById('override-response-status');
const btn = document.getElementById('save-override-response-btn');
if (!input || !btn) return;
const value = input.value.trim() || null;
btn.disabled = true;
if (statusEl) statusEl.textContent = 'Saving...';
try {
const res = await fetch('/api/patch-project-details', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ recordId, field: 'User Override Response', value })
});
const data = await res.json();
if (data.success) {
const airtableProjects = JSON.parse(localStorage.getItem('airtableProjects') || '[]');
const record = airtableProjects.find(r => r.id === recordId);
if (record) {
record.fields['User Override Response'] = value;
localStorage.setItem('airtableProjects', JSON.stringify(airtableProjects));
}
if (statusEl) { statusEl.textContent = 'Saved!'; setTimeout(() => { statusEl.textContent = ''; }, 2000); }
} else {
if (statusEl) statusEl.textContent = 'Error saving';
}
} catch { if (statusEl) statusEl.textContent = 'Error saving'; }
btn.disabled = false;
} else if (e.target.closest('.edit-field-btn')) {
const field = e.target.closest('.edit-field-btn').dataset.field;
document.querySelectorAll('.edit-field-input').forEach(row => {
if (row.dataset.field === field) row.style.display = 'flex';
});
document.querySelectorAll('.edit-field-btn').forEach(btn => {
if (btn.dataset.field === field) btn.style.display = 'none';
});
const fieldMap = { 'Description': 'desc-display', 'Code URL': 'codeurl-display', 'Demo URL': 'demourl-display' };
const displaySpan = document.getElementById(fieldMap[field]);
if (displaySpan) displaySpan.style.display = 'none';
} else if (e.target.closest('.cancel-field-btn')) {
const field = e.target.closest('.cancel-field-btn').dataset.field;
document.querySelectorAll('.edit-field-input').forEach(row => {
if (row.dataset.field === field) row.style.display = 'none';
});
document.querySelectorAll('.edit-field-btn').forEach(btn => {
if (btn.dataset.field === field) btn.style.display = 'flex';
});
const fieldMap = { 'Description': 'desc-display', 'Code URL': 'codeurl-display', 'Demo URL': 'demourl-display' };
const displaySpan = document.getElementById(fieldMap[field]);
if (displaySpan) displaySpan.style.display = '';
} else if (e.target.closest('.save-field-btn')) {
const btn = e.target.closest('.save-field-btn');
const field = btn.dataset.field;
let input = null;
document.querySelectorAll('.field-input').forEach(el => { if (el.dataset.field === field) input = el; });
const recordId = localStorage.getItem('selectedProjectId');
console.log('Save clicked — field:', field, 'value:', input?.value, 'recordId:', recordId);
if (!input || !recordId) return;
const value = input.value.trim();
try {
const res = await fetch('/api/patch-project-details', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ recordId, field, value })
});
const data = await res.json();
console.log('patch-project-details response:', data);
if (data.success) {
const airtableProjects = JSON.parse(localStorage.getItem('airtableProjects') || '[]');
const record = airtableProjects.find(r => r.id === recordId);
if (record) {
record.fields[field] = value;
localStorage.setItem('airtableProjects', JSON.stringify(airtableProjects));
}
updateOverlay(false);
} else {
console.error('Patch failed:', data);
}
} catch (err) {
console.error('Error saving field:', err);
}
}
});
// Handle changes to the project dropdown using delegation
document.addEventListener('change', async (e) => {
if (e.target && e.target.id === 'project-select') {
const selectedProjectName = e.target.value;
console.log(`Selected project: ${selectedProjectName}`);
// Retrieve the full project list from storage to find the hours (list from hackatime)
const storedProjects = JSON.parse(localStorage.getItem('projects') || '[]');
const projectData = storedProjects.find(p => (typeof p === 'string' ? p : p.name) === selectedProjectName);
const selectedProjectATid = localStorage.getItem('selectedProjectId');
console.log('Debug - projectData:', projectData);
console.log('Debug - selectedProjectATid:', selectedProjectATid);
// Check if projectData exists. If it's a string, we treat hours as 0 or handle accordingly
if (projectData) {
const totalSeconds = projectData.total_seconds || 0;
const hours = (totalSeconds / 3600).toFixed(2);
localStorage.setItem('selectedProjectHours', hours);
console.log(`Stored ${hours} hours for project: ${selectedProjectName}`);
if (selectedProjectATid && projectData && selectedProjectATid != 'N/A') {
try {
const ATPatchresponse = await fetch('api/project-scripts?action=patchHTProjectName', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
,
body: JSON.stringify({
selectedProjectName: selectedProjectName,
selectedProjectATid: selectedProjectATid,
})
})
console.log('Fetch response status:', ATPatchresponse.status);
const ATPatchData = await ATPatchresponse.json();
console.log("ATPatchData", ATPatchData);
if (ATPatchresponse.ok && ATPatchData.success) {
localStorage.setItem('updateHtName', ATPatchData.data.fields['Hackatime Project Name']);
console.log("UpdateHackatimeName",localStorage.getItem('updateHtName'));
}
updateOverlay(true);
} catch (error) {
console.error('Error patching project name:', error);
}
}
}
// Refresh the project list display
if (typeof fetchProjects === 'function') {
await fetchProjects(localStorage.getItem('email'));
}
}
});
async function updateOverlay(change) {
const overlay = document.getElementById('overlay');
// Get the current record from the stored Airtable data
const id = localStorage.getItem('selectedProjectId');
const airtableProjects = JSON.parse(localStorage.getItem('airtableProjects') || '[]');
const record = airtableProjects.find(r => r.id === id);
const htConnected = (record&&record.fields["Hackatime Project Name"])? true:false;
if (overlay && record) {
const email = record.fields["Email"] || 'N/A';
const projectName = record.fields["Project Name"] || 'Unnamed Project';
const htName = (change === true) ? localStorage.getItem('updateHtName') : record.fields["Hackatime Project Name"];
const storedHT = JSON.parse(localStorage.getItem('projects') || '[]');
const htProj = storedHT.find(p => (typeof p === 'string' ? p : p.name) === htName);
const rawSecs = htProj ? (htProj.total_seconds || 0) : 0;
let hours;
if (change === true) {
hours = localStorage.getItem('selectedProjectHours') || '0.00';
} else {
hours = (rawSecs / 3600).toFixed(2);
}
// Fetch MetroShip hours from project creation date
let metroshipHours = '0.00';
const createdDate = record.createdTime ? record.createdTime.split('T')[0] : null;
const accessToken = localStorage.getItem('htaccessToken') || '';
if (htName && createdDate && accessToken) {
try {
const statsRes = await fetch(`/api/my-hackatime-hours?accessToken=${encodeURIComponent(accessToken)}&projectName=${encodeURIComponent(htName)}&startDate=${createdDate}`);
const statsData = await statsRes.json();
if (statsData.success && statsData.projectTotal != null) {
metroshipHours = (statsData.projectTotal / 3600).toFixed(2);
}
} catch {}
}
try {
// Fetch the external HTML file
const response = await fetch('project-details-template.html');
const template = await response.text();
const desc = record.fields["Description"] || '';
const codeUrl = record.fields["Code URL"] || '';
const demoUrl = record.fields["Demo URL"] || '';
const shipped = record.fields["Shipped"] ? 'true' : 'false';
const returned = !!record.fields["Returned"];
const overrideHours = record.fields['Optional - Override Hours Spent'];
const overrideReason = record.fields['Optional - Override Hours Spent Justification'] || '';
const userResponse = (record.fields['User Override Response'] || '').replace(/</g, '<').replace(/>/g, '>');
let overrideSectionHtml = '';
if (overrideHours != null || overrideReason || returned) {
const returnedBanner = returned
? `<div style="background:#2a0000;border:1px solid red;border-radius:4px;padding:8px 12px;margin-bottom:10px;color:red;font-weight:bold;">⚠ This project was returned by a reviewer. Please address the feedback and re-ship.</div>`
: '';
const overrideBlock = (overrideHours != null || overrideReason)
? `<div style="margin-bottom:8px;">
${overrideHours != null ? `<p style="margin:4px 0;">Hours adjusted to: <span style="color:limegreen;font-weight:bold;">${overrideHours}</span></p>` : ''}
${overrideReason ? `<p style="margin:4px 0;">Reviewer's note: <span style="color:aqua;">${overrideReason.replace(/</g, '<').replace(/>/g, '>')}</span></p>` : ''}
</div>`
: '';
overrideSectionHtml = `
<div style="margin-top:16px;padding:12px;border:1px solid orange;border-radius:6px;background:#1a1000;">
<p style="color:orange;font-weight:bold;margin:0 0 10px 0;font-size:1.1vw;">Reviewer Feedback</p>
${returnedBanner}
${overrideBlock}
<div>
<p style="margin:0 0 4px 0;color:#aaa;font-size:0.95vw;">Your response:</p>
<textarea id="override-response-input" rows="3"
style="background:#111;color:aqua;border:1px solid #444;border-radius:4px;padding:6px;font-size:0.9vw;width:100%;box-sizing:border-box;resize:vertical;font-family:inherit;"
placeholder="Respond to the reviewer...">${userResponse}</textarea>
<div style="margin-top:6px;display:flex;gap:8px;align-items:center;">
<button id="save-override-response-btn"
style="cursor:pointer;padding:4px 14px;border:1px solid aqua;background:#111;color:aqua;border-radius:4px;font-family:inherit;">Save Response</button>
<span id="override-response-status" style="color:limegreen;font-size:0.85vw;"></span>
</div>
</div>
</div>`;
}
// Chain replacements for all placeholders
const finalHtml = template
.replace(/{{PROJECT_NAME}}/g, projectName)
.replace(/{{EMAIL}}/g, email)
.replace(/{{HOURS}}/g, hours)
.replace(/{{METROSHIP_HOURS}}/g, metroshipHours)
.replace(/{{DESC}}/g, desc)
.replace(/{{CODEURL}}/g, codeUrl)
.replace(/{{DEMOURL}}/g, demoUrl)
.replace(/{{SHIPPED}}/g, shipped)
.replace(/{{OVERRIDE_SECTION}}/g, overrideSectionHtml);
overlay.innerHTML = finalHtml;
overlay.style.display = 'block';
// Style the ship button based on shipped state
const shipBtn = document.getElementById('ship-btn');
if (shipBtn) {
if (shipped === 'true') {
shipBtn.textContent = '✓ Shipped';
shipBtn.style.borderColor = 'limegreen';
shipBtn.style.color = 'limegreen';
} else {
shipBtn.textContent = 'Ship Project';
shipBtn.style.borderColor = 'aqua';
shipBtn.style.color = 'aqua';
}
}
} catch (error) {
console.error('Error loading project details template:', error);
}
//Dropdown menu code
try {
const response = await fetch('api/project-scripts?action=getProjects', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ accessToken: localStorage.getItem('htaccessToken') }),
});
if (!response.ok) throw new Error('Failed to fetch projects from Hackatime');
const result = await response.json();
localStorage.setItem('projects', JSON.stringify(result.data.projects)); //Store project JSON list from HT
const select = document.getElementById('project-select');
const htProjectName = (change === true)? localStorage.getItem('updateHtName'):record.fields["Hackatime Project Name"];
//
if (select && result.success && result.data && Array.isArray(result.data.projects)) {
if (htConnected===true){
const defaultOption = document.createElement('option');
defaultOption.value = htProjectName;
defaultOption.textContent = htProjectName;
select.appendChild(defaultOption);
}else{
select.innerHTML = '<option value="">Select a project...</option>';
}
result.data.projects.forEach(proj => {
console.log("Appending project to select")
console.log({proj: proj});
const projectName = typeof proj === 'string' ? proj : proj.name;
const option = document.createElement('option');
option.value = projectName;
option.textContent = projectName;
select.appendChild(option);
});
localStorage.setItem('projects', JSON.stringify(result.data.projects)); //Store project JSON list from HT
fetchProjects(email);
}
} catch (error) {
console.error('Error connecting project:', error);
}
}
}