@@ -207,92 +207,27 @@ const WeeklySummariesReport = props => {
207207 }
208208 } ;
209209
210- // Initial data loading
211210 const createIntialSummaries = async ( ) => {
212211 try {
213- const {
214- allBadgeData,
215- getWeeklySummariesReport,
216- fetchAllBadges,
217- hasPermission,
218- auth,
219- setTeamCodes,
220- } = props ;
221-
222- // Get the active tab from session storage or use default
223- const activeTab =
224- sessionStorage . getItem ( 'tabSelection' ) === null
225- ? navItems [ 1 ]
226- : sessionStorage . getItem ( 'tabSelection' ) ;
227-
228- // Get the week index for the active tab
229- const weekIndex = navItems . indexOf ( activeTab ) ;
230-
231- // console.log(`Initial load: Fetching data for tab ${activeTab} with weekIndex ${weekIndex}`);
232-
233- // Set initial loading and active tab state
234- setState ( prevState => ( {
235- ...prevState ,
236- loading : true ,
237- activeTab,
238- tabsLoading : {
239- ...prevState . tabsLoading ,
240- [ activeTab ] : true ,
241- } ,
242- } ) ) ;
243-
244- // Get permissions
245- const badgeStatusCode = await fetchAllBadges ( ) ;
246- setPermissionState ( prev => ( {
247- ...prev ,
248- bioEditPermission : hasPermission ( 'putUserProfileImportantInfo' ) ,
249- canEditSummaryCount : hasPermission ( 'putUserProfileImportantInfo' ) ,
250- codeEditPermission :
251- hasPermission ( 'editTeamCode' ) ||
252- auth . user . role === 'Owner' ||
253- auth . user . role === 'Administrator' ,
254- canSeeBioHighlight : hasPermission ( 'highlightEligibleBios' ) ,
255- } ) ) ;
212+ const initialTabIndex = 0 ;
213+ const activeTab = navItems [ initialTabIndex ] ;
256214
257- // Fetch data for the active tab only
258- const res = await getWeeklySummariesReport ( weekIndex ) ;
259- // console.log('API response:', res);
260- // console.log('Response data:', res?.data);
261- // console.log('Data is array:', Array.isArray(res?.data));
262- // console.log('Data length:', res?.data?.length);
215+ const res = await props . getWeeklySummariesReport ( initialTabIndex ) ;
263216 const summaries = res ?. data ?? [ ] ;
264217
265- if ( ! Array . isArray ( summaries ) || summaries . length === 0 ) {
266- setState ( prevState => ( {
267- ...prevState ,
268- loading : false ,
269- tabsLoading : {
270- ...prevState . tabsLoading ,
271- [ activeTab ] : false ,
272- } ,
273- } ) ) ;
274- return null ;
275- }
276-
277- // Process the data
278- const teamCodeGroup = { } ;
279- const teamCodes = [ ] ;
280-
281- // Shallow copy and sort
282218 let summariesCopy = [ ...summaries ] ;
283219 summariesCopy = alphabetize ( summariesCopy ) ;
284220
285- // Add new key of promised hours by week
286221 summariesCopy = summariesCopy . map ( summary => {
287222 const promisedHoursByWeek = weekDates . map ( weekDate =>
288223 getPromisedHours ( weekDate . toDate , summary . weeklycommittedHoursHistory ) ,
289224 ) ;
290-
291225 const filterColor = summary . filterColor || null ;
292-
293226 return { ...summary , promisedHoursByWeek, filterColor } ;
294227 } ) ;
295228
229+ const teamCodeGroup = { } ;
230+ const teamCodes = [ ] ;
296231 const colorOptionGroup = new Set ( ) ;
297232 const colorOptions = [ ] ;
298233 const COLORS = [
@@ -318,7 +253,6 @@ const WeeklySummariesReport = props => {
318253 '#C8A2C8' ,
319254 ] ;
320255
321- // Process team codes and colors
322256 summariesCopy . forEach ( summary => {
323257 const code = summary . teamCode || 'noCodeLabel' ;
324258 if ( teamCodeGroup [ code ] ) {
@@ -327,70 +261,60 @@ const WeeklySummariesReport = props => {
327261 teamCodeGroup [ code ] = [ summary ] ;
328262 }
329263
330- if ( summary . weeklySummaryOption ) colorOptionGroup . add ( summary . weeklySummaryOption ) ;
264+ if ( summary . weeklySummaryOption ) {
265+ colorOptionGroup . add ( summary . weeklySummaryOption ) ;
266+ }
331267 } ) ;
332268
333269 Object . keys ( teamCodeGroup ) . forEach ( code => {
334270 if ( code !== 'noCodeLabel' ) {
335271 teamCodes . push ( {
336272 value : code ,
337273 label : `${ code } (${ teamCodeGroup [ code ] . length } )` ,
338- _ids : teamCodeGroup [ code ] ? .map ( item => item . _id ) ,
274+ _ids : teamCodeGroup [ code ] . map ( item => item . _id ) ,
339275 } ) ;
340276 }
341277 } ) ;
342278
343- setTeamCodes ( teamCodes ) ;
279+ if ( teamCodeGroup . noCodeLabel ?. length > 0 ) {
280+ teamCodes . push ( {
281+ value : '' ,
282+ label : `Select All With NO Code (${ teamCodeGroup . noCodeLabel . length } )` ,
283+ _ids : teamCodeGroup . noCodeLabel . map ( item => item . _id ) ,
284+ } ) ;
285+ }
344286
345287 colorOptionGroup . forEach ( option => {
346- colorOptions . push ( {
347- value : option ,
348- label : option ,
349- } ) ;
288+ colorOptions . push ( { value : option , label : option } ) ;
350289 } ) ;
351290
352- colorOptions . sort ( ( a , b ) => `${ a . label } ` . localeCompare ( `${ b . label } ` ) ) ;
353- teamCodes
354- . sort ( ( a , b ) => `${ a . label } ` . localeCompare ( `${ b . label } ` ) )
355- . push ( {
356- value : '' ,
357- label : `Select All With NO Code (${ teamCodeGroup . noCodeLabel ?. length || 0 } )` ,
358- _ids : teamCodeGroup ?. noCodeLabel ?. map ( item => item . _id ) ,
359- } ) ;
291+ colorOptions . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ;
292+ teamCodes . sort ( ( a , b ) => a . label . localeCompare ( b . label ) ) ;
360293
361294 const chartData = [ ] ;
362295
363- // Store the data in the tab-specific state
364296 setState ( prevState => ( {
365297 ...prevState ,
366298 loading : false ,
367- allRoleInfo : [ ] ,
299+ activeTab ,
368300 summaries : summariesCopy ,
369- loadedTabs : [ activeTab ] ,
370- summariesByTab : {
371- [ activeTab ] : summariesCopy ,
372- } ,
373- badges : allBadgeData ,
374- hasSeeBadgePermission : badgeStatusCode === 200 ,
375301 filteredSummaries : summariesCopy ,
302+ loadedTabs : [ activeTab ] ,
303+ summariesByTab : { [ activeTab ] : summariesCopy } ,
376304 tableData : teamCodeGroup ,
377305 chartData,
378306 COLORS ,
379307 colorOptions,
380308 teamCodes,
381309 teamCodeWarningUsers : summariesCopy . filter ( s => s . teamCodeWarning ) ,
382- auth,
383- tabsLoading : {
384- [ activeTab ] : false ,
385- } ,
310+ tabsLoading : { ...prevState . tabsLoading , [ activeTab ] : false } ,
386311 } ) ) ;
387312
388- // Now load info collections
313+ // 🔐 Now load info collections
389314 await intialInfoCollections ( summariesCopy ) ;
390-
391- return summariesCopy ;
392315 } catch ( error ) {
393- // console.error('Error in createInitialSummaries:', error);
316+ // eslint-disable-next-line no-console
317+ console . error ( 'Error in createInitialSummaries:' , error ) ;
394318 setState ( prevState => ( {
395319 ...prevState ,
396320 loading : false ,
@@ -399,7 +323,6 @@ const WeeklySummariesReport = props => {
399323 [ prevState . activeTab ] : false ,
400324 } ,
401325 } ) ) ;
402- return null ;
403326 }
404327 } ;
405328
@@ -763,10 +686,20 @@ const WeeklySummariesReport = props => {
763686 } ;
764687
765688 const handleSelectCodeChange = event => {
766- setState ( prev => ( {
767- ...prev ,
768- selectedCodes : event ,
769- } ) ) ;
689+ setState ( prev => {
690+ const selectedValues = event . map ( e => e . value ) ;
691+ // Move selected codes to the front of the dropdown list
692+ const reorderedTeamCodes = [
693+ ...prev . teamCodes . filter ( code => selectedValues . includes ( code . value ) ) , // selected first
694+ ...prev . teamCodes . filter ( code => ! selectedValues . includes ( code . value ) ) , // then the rest
695+ ] ;
696+
697+ return {
698+ ...prev ,
699+ selectedCodes : event ,
700+ teamCodes : reorderedTeamCodes ,
701+ } ;
702+ } ) ;
770703 } ;
771704
772705 const handleOverHoursToggleChange = ( ) => {
@@ -1068,10 +1001,12 @@ const WeeklySummariesReport = props => {
10681001 let isMounted = true ;
10691002 window . _isMounted = isMounted ;
10701003
1071- // console.log('Initial useEffect running');
1072-
1073- // Only load the initial tab, nothing else
1074- createIntialSummaries ( ) ;
1004+ // Wrap createIntialSummaries in an async fn so we can await it
1005+ const loadInitialData = async ( ) => {
1006+ await createIntialSummaries ( ) ;
1007+ } ;
1008+ // Kick off the async load on mount
1009+ loadInitialData ( ) ;
10751010
10761011 return ( ) => {
10771012 isMounted = false ;
@@ -1103,6 +1038,37 @@ const WeeklySummariesReport = props => {
11031038 state . summaries ,
11041039 state . activeTab ,
11051040 ] ) ;
1041+
1042+ useEffect ( ( ) => {
1043+ // On mount: fetch all badges before deriving permissions
1044+ const fetchInitialPermissions = async ( ) => {
1045+ try {
1046+ // Fetch all badges first so we can derive up‑to‑date permissions
1047+ await props . fetchAllBadges ( ) ;
1048+ setPermissionState ( prev => ( {
1049+ ...prev ,
1050+ bioEditPermission : props . hasPermission ( 'putUserProfileImportantInfo' ) ,
1051+ // codeEditPermission: props.hasPermission('replaceTeamCodes'),
1052+ // allow team‑code edits for specific roles or permissions
1053+ codeEditPermission :
1054+ props . hasPermission ( 'editTeamCode' ) ||
1055+ props . auth ?. user ?. role === 'Owner' ||
1056+ props . auth ?. user ?. role === 'Administrator' ,
1057+ // Permit editing of summary hour counts if the user has that badge
1058+ canEditSummaryCount : props . hasPermission ( 'editSummaryHoursCount' ) ,
1059+ // Show bio highlights only to users with that permission
1060+ canSeeBioHighlight : props . hasPermission ( 'highlightEligibleBios' ) ,
1061+ } ) ) ;
1062+ } catch ( error ) {
1063+ // log failure fetching badges or permissions
1064+ // eslint-disable-next-line no-console
1065+ console . error ( 'Failed to fetch badges or permissions' , error ) ;
1066+ }
1067+ } ;
1068+
1069+ fetchInitialPermissions ( ) ;
1070+ } , [ ] ) ;
1071+
11061072 const { role, darkMode } = props ;
11071073 const { error } = props ;
11081074 const hasPermissionToFilter = role === 'Owner' || role === 'Administrator' ;
0 commit comments