diff --git a/src/pages/centers/[cohortId]/index.tsx b/src/pages/centers/[cohortId]/index.tsx index 6fe7b554..5967e8f2 100644 --- a/src/pages/centers/[cohortId]/index.tsx +++ b/src/pages/centers/[cohortId]/index.tsx @@ -5,6 +5,7 @@ import { getMonthName, getTodayDate, shortDateFormat, + sortSessions, sortSessionsByTime, toPascalCase, } from '@/utils/Helper'; @@ -326,7 +327,8 @@ const CohortPage = () => { } }); } - setSessions(sessionArray); + const eventList = sortSessions(sessionArray); + setSessions(eventList); setEventUpdated(false); setEventDeleted(false); } catch (error) { @@ -382,7 +384,8 @@ const CohortPage = () => { useEffect(() => { if (extraSessions) { const { sessionList, index } = sortSessionsByTime(extraSessions); - setSortedSessions(sessionList); + const eventList = sortSessions(sessionList); + setSortedSessions(eventList); if (index > 0) { setInitialSlideIndex(index); diff --git a/src/pages/dashboard.tsx b/src/pages/dashboard.tsx index 557894f0..d9665e29 100644 --- a/src/pages/dashboard.tsx +++ b/src/pages/dashboard.tsx @@ -15,6 +15,7 @@ import { getBeforeDate, getTodayDate, shortDateFormat, + sortSessions, toPascalCase, } from '../utils/Helper'; import { @@ -787,8 +788,10 @@ const Dashboard: React.FC = () => { } }); } - setSessions(sessionArray); - setExtraSessions(extraSessionArray); + const eventList = sortSessions(sessionArray); + setSessions(eventList); + const ExtraEventList = sortSessions(extraSessionArray); + setExtraSessions(ExtraEventList); } setEventUpdated(false); setEventDeleted(false); diff --git a/src/utils/Helper.ts b/src/utils/Helper.ts index 377c35bf..23575201 100644 --- a/src/utils/Helper.ts +++ b/src/utils/Helper.ts @@ -2,7 +2,11 @@ import { Role, Status, labelsToExtractForMiniProfile } from './app.constant'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import FingerprintJS from 'fingerprintjs2'; -import { BoardEnrollmentStageCounts, CustomField, UpdateCustomField } from './Interfaces'; +import { + BoardEnrollmentStageCounts, + CustomField, + UpdateCustomField, +} from './Interfaces'; dayjs.extend(utc); import { format, parseISO } from 'date-fns'; import manageUserStore from '@/store/manageUserStore'; @@ -31,16 +35,14 @@ export const MONTHS = [ ]; export const formatDate = (dateString: string) => { - if(dateString) - { + if (dateString) { const dateOnly = dateString?.split('T')[0]; - + const [year, monthIndex, day] = dateOnly.split('-'); const month = MONTHS[parseInt(monthIndex, 10) - 1]; - + return `${day} ${month}, ${year}`; } - }; export const formatToShowDateMonth = (date: Date) => { @@ -119,7 +121,10 @@ export const debounce = any>( ) => { let timeout: ReturnType | undefined; - const debounced = function (this: ThisParameterType, ...args: Parameters) { + const debounced = function ( + this: ThisParameterType, + ...args: Parameters + ) { const context = this; clearTimeout(timeout); @@ -140,7 +145,6 @@ export const debounce = any>( return debounced; }; - //Function to convert names in capitalize case export const toPascalCase = (name: string | any) => { if (typeof name !== 'string') { @@ -323,10 +327,13 @@ export const generateUsernameAndPassword = ( export const mapFieldIdToValue = ( fields: CustomField[] ): { [key: string]: string } => { - return fields?.reduce((acc: { [key: string]: string }, field: CustomField) => { - acc[field.fieldId] = field.value; - return acc; - }, {}); + return fields?.reduce( + (acc: { [key: string]: string }, field: CustomField) => { + acc[field.fieldId] = field.value; + return acc; + }, + {} + ); }; export const convertUTCToIST = (utcDateTime: string) => { @@ -370,13 +377,13 @@ export const convertLocalToUTC = (localDateTime: any) => { export const getCurrentYearPattern = () => { const currentYear = new Date().getFullYear(); - + // Build the dynamic part for the current century let regexPart = ''; if (currentYear >= 2000 && currentYear < 2100) { const lastDigit = currentYear % 10; const middleDigit = Math.floor((currentYear % 100) / 10); - + regexPart = `20[0-${middleDigit - 1}][0-9]|20${middleDigit}[0-${lastDigit}]`; } @@ -464,6 +471,16 @@ export const format2DigitDate = (dateStr: any) => { return format(dateObj, 'd MMM, yyyy'); }; +export const sortSessions = (sessionArray: any[]) => { + if (!Array.isArray(sessionArray)) return []; + + return sessionArray.sort((a: any, b: any) => { + const timeA = new Date(a?.startDateTime).getTime(); + const timeB = new Date(b?.startDateTime).getTime(); + return timeA - timeB; // Ascending order (earliest first) + }); +}; + export const sortSessionsByTime = (sessionsArray: any) => { const passed: any = []; const live: any = []; @@ -497,7 +514,7 @@ export const getOptionsByCategory = (frameworks: any, categoryCode: string) => { return category.terms.map((term: any) => ({ name: term.name, code: term.code, - associations: term.associations + associations: term.associations, })); }; @@ -515,55 +532,64 @@ interface DataItem { code: string; associations: Association[]; } -export const getAssociationsByName = (data: DataItem[], name: string): Association[] | [] => { - const foundItem = data.find(item => item.name === name); +export const getAssociationsByName = ( + data: DataItem[], + name: string +): Association[] | [] => { + const foundItem = data.find((item) => item.name === name); return foundItem ? foundItem.associations : []; }; - -export const getAssociationsByCodeNew = (data: DataItem[], code: string): Association[] | [] => { - const foundItem = data.find(item => item.name === code); +export const getAssociationsByCodeNew = ( + data: DataItem[], + code: string +): Association[] | [] => { + const foundItem = data.find((item) => item.name === code); return foundItem ? foundItem.associations : []; }; - - -export const getAssociationsByCode = (data: DataItem[], code: string): Association[] | [] => { - const foundItem = data.find(item => item.code === code); +export const getAssociationsByCode = ( + data: DataItem[], + code: string +): Association[] | [] => { + const foundItem = data.find((item) => item.code === code); return foundItem ? foundItem.associations : []; }; export const findCommonAssociations = (data1: any[], data2: any[]) => { - if (!data1.length) return data2; if (!data2.length) return data1; - return data1.map((item1) => { - const item2 = data2.find((item) => item.code === item1.code); - if (item2) { - const commonAssociations = item1.associations.filter((assoc1: any) => - item2.associations.some((assoc2: any) => assoc1.identifier === assoc2.identifier) - ); - if (commonAssociations.length > 0) { - return { - name: item1.name, - code: item1.code, - associations: commonAssociations, - }; + return data1 + .map((item1) => { + const item2 = data2.find((item) => item.code === item1.code); + if (item2) { + const commonAssociations = item1.associations.filter((assoc1: any) => + item2.associations.some( + (assoc2: any) => assoc1.identifier === assoc2.identifier + ) + ); + if (commonAssociations.length > 0) { + return { + name: item1.name, + code: item1.code, + associations: commonAssociations, + }; + } } - } - return null; - }).filter(Boolean); + return null; + }) + .filter(Boolean); }; export const filterAndMapAssociationsNew = ( category: string, options: any[], associationsList?: any[], - codeKey: string = "code" + codeKey: string = 'code' ) => { if (!Array.isArray(options)) { - console.error("Options is not an array:", options); + console.error('Options is not an array:', options); return []; } @@ -589,8 +615,10 @@ export const filterAndMapAssociationsNew = ( export const extractCategory = (data: any[] | any, category: string) => { const items = Array.isArray(data) ? data : [data]; return items.flatMap((item) => - item.associations - .filter((association: { category: string; }) => association.category === category) + item.associations + .filter( + (association: { category: string }) => association.category === category + ) .map( ({ name, @@ -624,7 +652,10 @@ export function deepClone(obj: T): T { return JSON.parse(JSON.stringify(obj)); } -export const updateStoreFromCohorts = (activeCohorts: any, blockObject: any) => { +export const updateStoreFromCohorts = ( + activeCohorts: any, + blockObject: any +) => { const setDistrictCode = manageUserStore.getState().setDistrictCode; const setDistrictId = manageUserStore.getState().setDistrictId; const setStateCode = manageUserStore.getState().setStateCode; @@ -635,14 +666,13 @@ export const updateStoreFromCohorts = (activeCohorts: any, blockObject: any) => const setDistrictName = manageUserStore.getState().setDistrictName; const setStateName = manageUserStore.getState().setStateName; - const district = activeCohorts?.[0]?.customField?.find( (item: any) => item?.label === 'DISTRICTS' ); if (district) { setDistrictCode(district?.code); setDistrictId(district?.fieldId); - setDistrictName(district?.value) + setDistrictName(district?.value); } const state = activeCohorts?.[0]?.customField?.find( @@ -662,35 +692,35 @@ export const updateStoreFromCohorts = (activeCohorts: any, blockObject: any) => } }; -export function formatEndDate({diffDays}: any) { +export function formatEndDate({ diffDays }: any) { // Check if structuredClone is available - if(diffDays) - { - let remainingTime = ''; + if (diffDays) { + let remainingTime = ''; if (diffDays >= 365) { const years = Math.floor(diffDays / 365); const remainingDays = diffDays % 365; - - const months = Math.floor(remainingDays / 30.44); + + const months = Math.floor(remainingDays / 30.44); const days = Math.round(remainingDays % 30.44); - + remainingTime = `${years} year(s)${months > 0 ? `, ${months} month(s)` : ''}${days > 0 ? `, ${days} day(s)` : ''}`; } else if (diffDays > 31) { - const months = Math.floor(diffDays / 30.44); + const months = Math.floor(diffDays / 30.44); const days = Math.round(diffDays % 30.44); - + remainingTime = `${months} month(s) ${days > 0 ? ` , ${days} day(s)` : ''}`; } else { remainingTime = `${diffDays} day(s)`; } return remainingTime; } - return ""; - + return ''; } //TODO: Modify Helper with correct logic -export const calculateStageCounts = (data: { completedStep: any; }[]): BoardEnrollmentStageCounts => { +export const calculateStageCounts = ( + data: { completedStep: any }[] +): BoardEnrollmentStageCounts => { const stagesCount: BoardEnrollmentStageCounts = { board: 0, subjects: 0, @@ -700,11 +730,11 @@ export const calculateStageCounts = (data: { completedStep: any; }[]): BoardEnro }; const stageKeys: Record = { - 0: "board", - 1: "subjects", - 2: "registration", - 3: "fees", - 4: "completed", + 0: 'board', + 1: 'subjects', + 2: 'registration', + 3: 'fees', + 4: 'completed', }; data.forEach(({ completedStep }) => { @@ -715,12 +745,11 @@ export const calculateStageCounts = (data: { completedStep: any; }[]): BoardEnro return stagesCount; }; - export function getCohortNameById( cohorts: { cohortId: string; name: string }[], cohortId: string ): string | null { - const cohort = cohorts.find(c => c.cohortId === cohortId); + const cohort = cohorts.find((c) => c.cohortId === cohortId); return cohort ? cohort.name : null; } // const isFieldFilled = (key: string, value: any): boolean => { @@ -734,4 +763,4 @@ export function getCohortNameById( // Object.entries(formData).reduce( // (count, [key, value]) => count + (isFieldFilled(key, value) ? 1 : 0), // 0 -// ); \ No newline at end of file +// );