Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/pages/mentorship/mentors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
Collapse,
} from '@mui/material';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { BreadCrumbsDynamic, MentorProfileCard, Title } from '@components';
import {
Expand Down Expand Up @@ -63,6 +63,9 @@ const MentorsPage = () => {
const [filterSection, setFilterSection] = useState<FilterSection | null>(
null,
);
// Ensures the open-cycle type default is applied only once, so an explicit
// "All" selection is never overridden.
const didApplyCycleDefaultRef = useRef(false);

// Initialize filter state from URL query params
useEffect(() => {
Expand Down Expand Up @@ -127,6 +130,35 @@ const MentorsPage = () => {
setMentorsState([]);
setFilterSection(null);
}

// On first load, scope the default view to the currently open cycle's
// mentorship type so an ad-hoc cycle lists only ad-hoc mentors (and a
// long-term cycle only long-term mentors). The backend then applies the
// full type + month scoping. Applied once; an explicit "All" selection
// clears the filter and is preserved.
const openCycle = result?.openCycle ?? result?.data?.openCycle;
if (
!didApplyCycleDefaultRef.current &&
!query.mentorshipTypes &&
openCycle?.active &&
FILTER_MENTORSHIP_TYPES_OPTIONS.some(
(option) => option.value === openCycle.mentorshipType,
)
) {
didApplyCycleDefaultRef.current = true;
setSelectedMentorshipType(openCycle.mentorshipType);
router.replace(
{
pathname: router.pathname,
query: {
...router.query,
mentorshipTypes: openCycle.mentorshipType,
},
},
undefined,
{ shallow: true },
);
}
} catch (err) {
// eslint-disable-next-line no-console
console.error('Failed to fetch mentors with params', err);
Expand Down
Loading