Skip to content

Commit 378540a

Browse files
authored
Merge pull request #164 from Preeti9764/preview-bug
Enhance GitHub Organization Selection in Setting
2 parents b1d37bc + 5ce285f commit 378540a

File tree

4 files changed

+365
-310
lines changed

4 files changed

+365
-310
lines changed

src/popup.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,14 @@ <h4>Organization Name</h4>
140140
</span>
141141
</span>
142142
</div>
143-
<input id="orgInput" type="text"
144-
class="w-full border-2 border-gray-200 bg-gray-200 rounded-xl text-gray-800 p-2 my-2"
145-
placeholder="Enter organization name (default: fossasia)">
143+
<div class="flex items-center mt-4 gap-2">
144+
<input id="orgInput" type="text"
145+
class="w-full border-2 border-gray-200 bg-gray-200 rounded-xl text-gray-800 p-2 my-2"
146+
placeholder="Enter organization name(Default: fossasia)">
147+
<button id="setOrgBtn" type="button"
148+
class="px-5 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-xl text-base my-2 h-[44px] flex-shrink-0"
149+
style="min-width:60px;">Set</button>
150+
</div>
146151
</div>
147152
<div class="">
148153
<div class="flex items-center justify-between">

src/scripts/main.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@ function handleLastWeekContributionChange() {
131131
let value = lastWeekContributionElement.checked;
132132
let labelElement = document.querySelector("label[for='lastWeekContribution']");
133133
if (value) {
134-
startingDateElement.readOnly = true;
135-
endingDateElement.readOnly = true;
136-
endingDateElement.value = getToday();
137-
startingDateElement.value = getLastWeek();
138-
handleEndingDateChange();
139-
handleStartingDateChange();
140-
labelElement.classList.add("selectedLabel");
141-
labelElement.classList.remove("unselectedLabel");
134+
startingDateElement.readOnly = true;
135+
endingDateElement.readOnly = true;
136+
endingDateElement.value = getToday();
137+
startingDateElement.value = getLastWeek();
138+
handleEndingDateChange();
139+
handleStartingDateChange();
140+
labelElement.classList.add("selectedLabel");
141+
labelElement.classList.remove("unselectedLabel");
142142
} else {
143-
startingDateElement.readOnly = false;
144-
endingDateElement.readOnly = false;
145-
labelElement.classList.add("unselectedLabel");
146-
labelElement.classList.remove("selectedLabel");
143+
startingDateElement.readOnly = false;
144+
endingDateElement.readOnly = false;
145+
labelElement.classList.add("unselectedLabel");
146+
labelElement.classList.remove("selectedLabel");
147147
}
148148

149149
chrome.storage.local.set({ lastWeekContribution: value });

src/scripts/popup.js

Lines changed: 93 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,16 @@ document.addEventListener('DOMContentLoaded', function () {
215215
const copyBtn = document.getElementById('copyReport');
216216

217217
generateBtn.addEventListener('click', function () {
218-
this.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Generating...';
219-
this.disabled = true;
220-
window.generateScrumReport();
218+
// Check org input value before generating report
219+
let org = orgInput.value.trim().toLowerCase();
220+
if (!org) {
221+
org = 'fossasia';
222+
}
223+
chrome.storage.local.set({ orgName: org }, () => {
224+
generateBtn.innerHTML = '<i class="fa fa-spinner fa-spin"></i> Generating...';
225+
generateBtn.disabled = true;
226+
window.generateScrumReport();
227+
});
221228
});
222229

223230
copyBtn.addEventListener('click', function () {
@@ -276,11 +283,13 @@ document.addEventListener('DOMContentLoaded', function () {
276283
], (items) => {
277284
console.log('Restoring state:', items);
278285

279-
if(items.startingDate && items.endingDate && !items.lastWeekContribution && !items.yesterdayContribution) {
286+
287+
if (items.startingDate && items.endingDate && !items.lastWeekContribution && !items.yesterdayContribution) {
280288
const startDateInput = document.getElementById('startingDate');
281289
const endDateInput = document.getElementById('endingDate');
282290

283-
if(startDateInput && endDateInput) {
291+
if (startDateInput && endDateInput) {
292+
284293
startDateInput.value = items.startingDate;
285294
endDateInput.value = items.endingDate;
286295
startDateInput.readOnly = false;
@@ -315,7 +324,7 @@ document.addEventListener('DOMContentLoaded', function () {
315324
endDateInput.value = getToday();
316325
}
317326
startDateInput.readOnly = endDateInput.readOnly = true;
318-
327+
319328
chrome.storage.local.set({
320329
startingDate: startDateInput.value,
321330
endingDate: endDateInput.value,
@@ -367,28 +376,31 @@ document.addEventListener('DOMContentLoaded', function () {
367376
orgInput.value = result.orgName || '';
368377
});
369378

370-
// Debounce function
371-
function debounce(func, wait) {
372-
let timeout;
373-
return function (...args) {
374-
clearTimeout(timeout);
375-
timeout = setTimeout(() => func.apply(this, args), wait);
376-
};
377-
}
379+
// Auto-update orgName in storage on input change
380+
orgInput.addEventListener('input', function () {
381+
let org = orgInput.value.trim().toLowerCase();
382+
if (!org) {
383+
org = 'fossasia';
384+
}
385+
chrome.storage.local.set({ orgName: org }, function () {
386+
chrome.storage.local.remove('githubCache'); // Clear cache on org change
387+
});
388+
});
378389

379-
let lastInvalidOrg = '';
380-
// Validate and set org as user types
381-
const handleOrgInput = debounce(function () {
390+
// Add click event for setOrgBtn to set org
391+
setOrgBtn.addEventListener('click', function () {
382392
let org = orgInput.value.trim().toLowerCase();
383393
if (!org) {
384394
org = 'fossasia';
385395
}
386-
console.log('[Org Check] Checking organization:', org);
396+
setOrgBtn.disabled = true;
397+
const originalText = setOrgBtn.innerHTML;
398+
setOrgBtn.innerHTML = '<i class="fa fa-spinner fa-spin"></i>';
387399
fetch(`https://api.github.com/orgs/${org}`)
388400
.then(res => {
389-
console.log('[Org Check] Response status for', org, ':', res.status);
390401
if (res.status === 404) {
391-
console.log('[Org Check] Organization not found on GitHub:', org);
402+
setOrgBtn.disabled = false;
403+
setOrgBtn.innerHTML = originalText;
392404
const oldToast = document.getElementById('invalid-org-toast');
393405
if (oldToast) oldToast.parentNode.removeChild(oldToast);
394406
const toastDiv = document.createElement('div');
@@ -413,14 +425,40 @@ document.addEventListener('DOMContentLoaded', function () {
413425
}
414426
const oldToast = document.getElementById('invalid-org-toast');
415427
if (oldToast) oldToast.parentNode.removeChild(oldToast);
416-
console.log('[Org Check] Organisation exists on GitHub:', org);
417-
console.log('[Org Check] Organization exists on GitHub:', org);
418428
chrome.storage.local.set({ orgName: org }, function () {
419-
if (window.generateScrumReport) window.generateScrumReport();
429+
// Always clear the scrum report and show org changed message
430+
const scrumReport = document.getElementById('scrumReport');
431+
if (scrumReport) {
432+
scrumReport.innerHTML = '<p style="text-align: center; color: #666; padding: 20px;">Organization changed. Click Generate button to fetch the GitHub activities.</p>';
433+
}
434+
// Clear the githubCache for previous org
435+
chrome.storage.local.remove('githubCache');
436+
setOrgBtn.disabled = false;
437+
setOrgBtn.innerHTML = originalText;
438+
// Always show green toast: org is set
439+
const toastDiv = document.createElement('div');
440+
toastDiv.id = 'invalid-org-toast';
441+
toastDiv.className = 'toast';
442+
toastDiv.style.background = '#10b981';
443+
toastDiv.style.color = '#fff';
444+
toastDiv.style.fontWeight = 'bold';
445+
toastDiv.style.padding = '12px 24px';
446+
toastDiv.style.borderRadius = '8px';
447+
toastDiv.style.position = 'fixed';
448+
toastDiv.style.top = '24px';
449+
toastDiv.style.left = '50%';
450+
toastDiv.style.transform = 'translateX(-50%)';
451+
toastDiv.style.zIndex = '9999';
452+
toastDiv.innerText = 'Organization is set.';
453+
document.body.appendChild(toastDiv);
454+
setTimeout(() => {
455+
if (toastDiv.parentNode) toastDiv.parentNode.removeChild(toastDiv);
456+
}, 2500);
420457
});
421458
})
422459
.catch((err) => {
423-
console.log('[Org Check] Error validating organisation:', org, err);
460+
setOrgBtn.disabled = false;
461+
setOrgBtn.innerHTML = originalText;
424462
const oldToast = document.getElementById('invalid-org-toast');
425463
if (oldToast) oldToast.parentNode.removeChild(oldToast);
426464
const toastDiv = document.createElement('div');
@@ -442,9 +480,38 @@ document.addEventListener('DOMContentLoaded', function () {
442480
if (toastDiv.parentNode) toastDiv.parentNode.removeChild(toastDiv);
443481
}, 3000);
444482
});
445-
}, 3000);
483+
});
484+
485+
let cacheInput = document.getElementById('cacheInput');
486+
if (cacheInput) {
487+
chrome.storage.local.get(['cacheInput'], function (result) {
488+
if (result.cacheInput) {
489+
cacheInput.value = result.cacheInput;
490+
} else {
491+
cacheInput.value = 10;
492+
}
493+
});
494+
495+
cacheInput.addEventListener('blur', function () {
496+
let ttlValue = parseInt(this.value);
497+
if (isNaN(ttlValue) || ttlValue <= 0 || this.value.trim() === '') {
498+
ttlValue = 10;
499+
this.value = ttlValue;
500+
this.style.borderColor = '#ef4444';
501+
} else if (ttlValue > 1440) {
502+
ttlValue = 1440;
503+
this.value = ttlValue;
504+
this.style.borderColor = '#f59e0b';
505+
} else {
506+
this.style.borderColor = '#10b981';
507+
}
508+
509+
chrome.storage.local.set({ cacheInput: ttlValue }, function () {
510+
console.log('Cache TTL saved:', ttlValue, 'minutes');
511+
});
512+
});
446513

447-
orgInput.addEventListener('input', handleOrgInput);
514+
}
448515

449516
});
450517

@@ -586,33 +653,3 @@ function toggleRadio(radio) {
586653
});
587654
}
588655

589-
let cacheInput = document.getElementById('cacheInput');
590-
if (cacheInput) {
591-
chrome.storage.local.get(['cacheInput'], function (result) {
592-
if (result.cacheInput) {
593-
cacheInput.value = result.cacheInput;
594-
} else {
595-
cacheInput.value = 10;
596-
}
597-
});
598-
599-
cacheInput.addEventListener('blur', function () {
600-
let ttlValue = parseInt(this.value);
601-
if (isNaN(ttlValue) || ttlValue <= 0 || this.value.trim() === '') {
602-
ttlValue = 10;
603-
this.value = ttlValue;
604-
this.style.borderColor = '#ef4444';
605-
} else if (ttlValue > 1440) {
606-
ttlValue = 1440;
607-
this.value = ttlValue;
608-
this.style.borderColor = '#f59e0b';
609-
} else {
610-
this.style.borderColor = '#10b981';
611-
}
612-
613-
chrome.storage.local.set({ cacheInput: ttlValue }, function () {
614-
console.log('Cache TTL saved:', ttlValue, 'minutes');
615-
});
616-
});
617-
618-
}

0 commit comments

Comments
 (0)