@@ -60,7 +60,8 @@ document.addEventListener('DOMContentLoaded', function () {
60
60
let tokenVisible = false ;
61
61
62
62
const orgInput = document . getElementById ( 'orgInput' ) ;
63
- const setOrgBtn = document . getElementById ( 'setOrgBtn' ) ;
63
+ const validateOrgBtn = document . getElementById ( 'validateOrgBtn' ) ;
64
+ const orgValidationStatus = document . getElementById ( 'orgValidationStatus' ) ;
64
65
65
66
chrome . storage . local . get ( [ 'darkMode' ] , function ( result ) {
66
67
if ( result . darkMode ) {
@@ -295,7 +296,7 @@ document.addEventListener('DOMContentLoaded', function () {
295
296
endDateInput . value = getToday ( ) ;
296
297
}
297
298
startDateInput . readOnly = endDateInput . readOnly = true ;
298
-
299
+
299
300
chrome . storage . local . set ( {
300
301
startingDate : startDateInput . value ,
301
302
endingDate : endDateInput . value ,
@@ -347,89 +348,111 @@ document.addEventListener('DOMContentLoaded', function () {
347
348
orgInput . value = result . orgName || '' ;
348
349
} ) ;
349
350
350
- // Debounce function
351
- function debounce ( func , wait ) {
352
- let timeout ;
353
- return function ( ...args ) {
354
- clearTimeout ( timeout ) ;
355
- timeout = setTimeout ( ( ) => func . apply ( this , args ) , wait ) ;
356
- } ;
357
- }
358
-
359
- let lastInvalidOrg = '' ;
360
- // Validate and set org as user types
361
- const handleOrgInput = debounce ( function ( ) {
351
+ // Organization validation function
352
+ async function validateOrganization ( ) {
362
353
let org = orgInput . value . trim ( ) . toLowerCase ( ) ;
363
354
if ( ! org ) {
364
355
org = 'fossasia' ;
365
356
}
366
- console . log ( '[Org Check] Checking organization:' , org ) ;
367
- fetch ( `https://api.github.com/orgs/${ org } ` )
368
- . then ( res => {
369
- console . log ( '[Org Check] Response status for' , org , ':' , res . status ) ;
370
- if ( res . status === 404 ) {
371
- console . log ( '[Org Check] Organization not found on GitHub:' , org ) ;
372
- const oldToast = document . getElementById ( 'invalid-org-toast' ) ;
373
- if ( oldToast ) oldToast . parentNode . removeChild ( oldToast ) ;
374
- const toastDiv = document . createElement ( 'div' ) ;
375
- toastDiv . id = 'invalid-org-toast' ;
376
- toastDiv . className = 'toast' ;
377
- toastDiv . style . background = '#dc2626' ;
378
- toastDiv . style . color = '#fff' ;
379
- toastDiv . style . fontWeight = 'bold' ;
380
- toastDiv . style . padding = '12px 24px' ;
381
- toastDiv . style . borderRadius = '8px' ;
382
- toastDiv . style . position = 'fixed' ;
383
- toastDiv . style . top = '24px' ;
384
- toastDiv . style . left = '50%' ;
385
- toastDiv . style . transform = 'translateX(-50%)' ;
386
- toastDiv . style . zIndex = '9999' ;
387
- toastDiv . innerText = 'Organization not found on GitHub.' ;
388
- document . body . appendChild ( toastDiv ) ;
389
- setTimeout ( ( ) => {
390
- if ( toastDiv . parentNode ) toastDiv . parentNode . removeChild ( toastDiv ) ;
391
- } , 3000 ) ;
392
- return ;
393
- }
394
- const oldToast = document . getElementById ( 'invalid-org-toast' ) ;
395
- if ( oldToast ) oldToast . parentNode . removeChild ( oldToast ) ;
396
- console . log ( '[Org Check] Organisation exists on GitHub:' , org ) ;
397
357
398
- // Valid org: update storage and fetch data
399
- chrome . storage . local . set ( { orgName : org , githubCache : null } , function ( ) {
400
- const scrumReport = document . getElementById ( 'scrumReport' ) ;
401
- if ( scrumReport ) {
402
- scrumReport . innerHTML = '<p style="text-align: center; color: #666; padding: 20px;">Organisation changed. Click "Generate Report" to fetch new data.</p>' ;
403
- }
358
+ // Show loading state
359
+ validateOrgBtn . disabled = true ;
360
+ validateOrgBtn . innerHTML = '<i class="fa fa-spinner fa-spin"></i><span>Checking...</span>' ;
404
361
405
- } ) ;
406
- } )
407
- . catch ( ( err ) => {
408
- console . log ( '[Org Check] Error validating organisation:' , org , err ) ;
409
- const oldToast = document . getElementById ( 'invalid-org-toast' ) ;
410
- if ( oldToast ) oldToast . parentNode . removeChild ( oldToast ) ;
411
- const toastDiv = document . createElement ( 'div' ) ;
412
- toastDiv . id = 'invalid-org-toast' ;
413
- toastDiv . className = 'toast' ;
414
- toastDiv . style . background = '#dc2626' ;
415
- toastDiv . style . color = '#fff' ;
416
- toastDiv . style . fontWeight = 'bold' ;
417
- toastDiv . style . padding = '12px 24px' ;
418
- toastDiv . style . borderRadius = '8px' ;
419
- toastDiv . style . position = 'fixed' ;
420
- toastDiv . style . top = '24px' ;
421
- toastDiv . style . left = '50%' ;
422
- toastDiv . style . transform = 'translateX(-50%)' ;
423
- toastDiv . style . zIndex = '9999' ;
424
- toastDiv . innerText = 'Error validating organization.' ;
425
- document . body . appendChild ( toastDiv ) ;
362
+ // Show status message
363
+ orgValidationStatus . className = 'text-sm mt-1 text-blue-600' ;
364
+ orgValidationStatus . innerHTML = '<i class="fa fa-spinner fa-spin"></i> Validating organization...' ;
365
+ orgValidationStatus . classList . remove ( 'hidden' ) ;
366
+
367
+ try {
368
+ console . log ( '[Org Check] Checking organization:' , org ) ;
369
+ const response = await fetch ( `https://api.github.com/orgs/${ org } ` ) ;
370
+
371
+ console . log ( '[Org Check] Response status for' , org , ':' , response . status ) ;
372
+
373
+ if ( response . status === 404 ) {
374
+ console . log ( '[Org Check] Organization not found on GitHub:' , org ) ;
375
+
376
+ // Show error state
377
+ validateOrgBtn . innerHTML = '<i class="fa fa-times"></i><span>Invalid</span>' ;
378
+ validateOrgBtn . className = 'bg-red-600 hover:bg-red-700 text-white font-medium py-2 px-4 rounded flex items-center gap-2 transition-colors duration-200' ;
379
+
380
+ orgValidationStatus . className = 'text-sm mt-1 text-red-600' ;
381
+ orgValidationStatus . innerHTML = '<i class="fa fa-times"></i> Organization not found on GitHub' ;
382
+
383
+ // Reset button after 3 seconds
426
384
setTimeout ( ( ) => {
427
- if ( toastDiv . parentNode ) toastDiv . parentNode . removeChild ( toastDiv ) ;
385
+ validateOrgBtn . disabled = false ;
386
+ validateOrgBtn . innerHTML = '<i class="fa fa-check"></i><span>Set</span>' ;
387
+ validateOrgBtn . className = 'bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded flex items-center gap-2 transition-colors duration-200' ;
388
+ orgValidationStatus . classList . add ( 'hidden' ) ;
428
389
} , 3000 ) ;
390
+
391
+ return ;
392
+ }
393
+
394
+ if ( ! response . ok ) {
395
+ throw new Error ( `HTTP ${ response . status } : ${ response . statusText } ` ) ;
396
+ }
397
+
398
+ console . log ( '[Org Check] Organisation exists on GitHub:' , org ) ;
399
+
400
+ // Valid org: update storage and fetch data
401
+ await new Promise ( ( resolve ) => {
402
+ chrome . storage . local . set ( { orgName : org , githubCache : null } , resolve ) ;
429
403
} ) ;
430
- } , 2500 ) ;
431
404
432
- orgInput . addEventListener ( 'input' , handleOrgInput ) ;
405
+ // Show success state
406
+ validateOrgBtn . innerHTML = '<i class="fa fa-check"></i><span>Set</span>' ;
407
+ validateOrgBtn . className = 'bg-green-600 hover:bg-green-700 text-white font-medium py-2 px-4 rounded flex items-center gap-2 transition-colors duration-200' ;
408
+
409
+ orgValidationStatus . className = 'text-sm mt-1 text-green-600' ;
410
+ orgValidationStatus . innerHTML = '<i class="fa fa-check"></i> Organization validated successfully!' ;
411
+
412
+ // Update scrum report
413
+ const scrumReport = document . getElementById ( 'scrumReport' ) ;
414
+ if ( scrumReport ) {
415
+ scrumReport . innerHTML = '<p style="text-align: center; color: #666; padding: 20px;">Organisation changed. Click "Generate Report" to fetch new data.</p>' ;
416
+ }
417
+
418
+ // Reset button after 3 seconds
419
+ setTimeout ( ( ) => {
420
+ validateOrgBtn . disabled = false ;
421
+ validateOrgBtn . innerHTML = '<i class="fa fa-check"></i><span>Set</span>' ;
422
+ validateOrgBtn . className = 'bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded flex items-center gap-2 transition-colors duration-200' ;
423
+ orgValidationStatus . classList . add ( 'hidden' ) ;
424
+ } , 3000 ) ;
425
+
426
+ } catch ( error ) {
427
+ console . log ( '[Org Check] Error validating organisation:' , org , error ) ;
428
+
429
+ // Show error state
430
+ validateOrgBtn . innerHTML = '<i class="fa fa-exclamation-triangle"></i><span>Error</span>' ;
431
+ validateOrgBtn . className = 'bg-red-600 hover:bg-red-700 text-white font-medium py-2 px-4 rounded flex items-center gap-2 transition-colors duration-200' ;
432
+
433
+ orgValidationStatus . className = 'text-sm mt-1 text-red-600' ;
434
+ orgValidationStatus . innerHTML = '<i class="fa fa-exclamation-triangle"></i> Error validating organization. Please try again.' ;
435
+
436
+ // Reset button after 3 seconds
437
+ setTimeout ( ( ) => {
438
+ validateOrgBtn . disabled = false ;
439
+ validateOrgBtn . innerHTML = '<i class="fa fa-check"></i><span>Set</span>' ;
440
+ validateOrgBtn . className = 'bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded flex items-center gap-2 transition-colors duration-200' ;
441
+ orgValidationStatus . classList . add ( 'hidden' ) ;
442
+ } , 3000 ) ;
443
+ }
444
+ }
445
+
446
+ // Add click event listener to validation button
447
+ validateOrgBtn . addEventListener ( 'click' , validateOrganization ) ;
448
+
449
+ // Add Enter key support for the input field
450
+ orgInput . addEventListener ( 'keypress' , function ( event ) {
451
+ if ( event . key === 'Enter' ) {
452
+ event . preventDefault ( ) ;
453
+ validateOrganization ( ) ;
454
+ }
455
+ } ) ;
433
456
434
457
} ) ;
435
458
0 commit comments