Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reload metor registries after approve #2517

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { Dispatch, SetStateAction, useMemo, useState } from 'react';
import { Dispatch, SetStateAction } from 'react';
import { GithubUserLink } from 'components/GithubUserLink';
import { SafetyCertificateTwoTone } from '@ant-design/icons';
import { colorTagRenderer, getColumnSearchProps, stringSorter, tagsRenderer, dateSorter } from 'components/Table';
import { formatDate } from 'services/formatter';
import { Course } from 'services/models';
import CopyToClipboardButton from 'components/CopyToClipboardButton';
import {
MentorsRegistryColumnKey,
MentorsRegistryColumnName,
TABS,
MentorRegistryTabsMode,
PAGINATION,
} from '../constants';
import { MentorsRegistryColumnKey, MentorsRegistryColumnName, TABS, MentorRegistryTabsMode } from '../constants';
import { FilterValue } from 'antd/lib/table/interface';
import { Button, Dropdown, Tooltip, message } from 'antd';
import { MoreOutlined, MessageTwoTone } from '@ant-design/icons';
import { ColumnType } from 'antd/lib/table';
import { DisciplineDto, MentorRegistryDto } from 'api';
import { ModalDataMode } from 'pages/admin/mentor-registry';
import css from 'styled-jsx/css';
import { MentorRegistryService } from 'services/mentorRegistry';
import { useAsync } from 'react-use';

interface ChildrenProp {
setCurrentPage: Dispatch<SetStateAction<number>>;
Expand All @@ -41,6 +33,13 @@ interface Props {
handleModalDataChange: (mode: ModalDataMode, record: MentorRegistryDto) => void;
children: (props: ChildrenProp) => JSX.Element;
disciplines: DisciplineDto[];
tagFilters: string[];
setTagFilters: Dispatch<SetStateAction<string[]>>;
combinedFilter: CombinedFilter;
setCombinedFilter: Dispatch<SetStateAction<CombinedFilter>>;
setCurrentPage: Dispatch<SetStateAction<number>>;
currentPage: number;
total: number;
}

export interface CombinedFilter {
Expand All @@ -52,28 +51,21 @@ export interface CombinedFilter {
filterTags?: string[];
}

const mentorRegistryService = new MentorRegistryService();
export const MentorRegistryTableContainer = ({
children,
mentors,
courses,
activeTab,
handleModalDataChange,
disciplines,
tagFilters,
setTagFilters,
combinedFilter,
setCombinedFilter,
setCurrentPage,
currentPage,
total,
}: Props) => {
const [tagFilters, setTagFilters] = useState<string[]>([]);
const [combinedFilter, setCombinedFilter] = useState<CombinedFilter>({
preferredCourses: [],
preselectedCourses: [],
technicalMentoring: [],
githubId: [],
cityName: [],
});

const [currentPage, setCurrentPage] = useState(1);
const [total, setTotal] = useState(0);
const [loaded, setLoaded] = useState<MentorRegistryDto[] | null>(null);

const renderPreselectedCourses = (courses: Course[]) => {
return (values: number[], record: MentorRegistryDto) => {
return values
Expand Down Expand Up @@ -117,28 +109,6 @@ export const MentorRegistryTableContainer = ({
);
};

const filteredData = useMemo(() => {
return mentors.filter(
mentor =>
(combinedFilter.technicalMentoring.length
? mentor.technicalMentoring.some(value => combinedFilter.technicalMentoring.includes(value))
: true) &&
(combinedFilter.preselectedCourses.length
? mentor.preselectedCourses.some(value => combinedFilter.preselectedCourses.includes(value))
: true) &&
(combinedFilter.preferredCourses.length
? mentor.preferedCourses.some(value => combinedFilter.preferredCourses.includes(value))
: true) &&
(combinedFilter.githubId?.length
? mentor.githubId.toLowerCase().includes(combinedFilter.githubId[0].toLocaleLowerCase()) ||
mentor.name.toLowerCase().includes(combinedFilter.githubId[0].toLocaleLowerCase())
: true) &&
(combinedFilter.cityName?.length
? mentor.cityName?.toLowerCase().includes(combinedFilter.cityName[0].toLocaleLowerCase())
: true),
);
}, [combinedFilter, mentors]);

const handleTableChange = async (
_: any,
filters: Record<MentorsRegistryColumnKey, FilterValue | string[] | null>,
Expand Down Expand Up @@ -389,35 +359,9 @@ export const MentorRegistryTableContainer = ({
setTagFilters([]);
};

useAsync(async () => {
try {
const { mentors, total } = await mentorRegistryService.getMentors({
pageSize: PAGINATION,
currentPage,
githubId: combinedFilter.githubId?.[0] ?? undefined,
cityName: combinedFilter.cityName?.[0] ?? undefined,
preferedCourses: combinedFilter.preferredCourses?.length
? combinedFilter.preferredCourses.map(Number)
: undefined,
preselectedCourses: combinedFilter.preselectedCourses?.length
? combinedFilter.preselectedCourses.map(Number)
: undefined,
technicalMentoring: combinedFilter.technicalMentoring?.length
? (combinedFilter.technicalMentoring as string[])
: undefined,
});
setLoaded(mentors);
setTotal(total);
} catch (e) {
message.error('An error occurred. No filters have been applied.');
setLoaded(filteredData);
setTotal(filteredData.length);
}
}, [JSON.stringify(combinedFilter), currentPage]);

return children({
tagFilters,
filteredData: loaded || filteredData,
filteredData: mentors,
columns: getColumns(combinedFilter, courses),
currentPage,
total,
Expand Down
44 changes: 40 additions & 4 deletions client/src/pages/admin/mentor-registry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import { ModalForm } from 'components/Forms';
import { MentorRegistryResendModal } from 'modules/MentorRegistry/components/MentorRegistryResendModal';
import { MentorRegistryDeleteModal } from 'modules/MentorRegistry/components/MentorRegistryDeleteModal';
import { MentorRegistryTable } from 'modules/MentorRegistry/components/MentorRegistryTable';
import { MentorRegistryTableContainer } from 'modules/MentorRegistry/components/MentorRegistryTableContainer';
import { MentorRegistryTabsMode } from 'modules/MentorRegistry/constants';
import {
CombinedFilter,
MentorRegistryTableContainer,
} from 'modules/MentorRegistry/components/MentorRegistryTableContainer';
import { MentorRegistryTabsMode, PAGINATION } from 'modules/MentorRegistry/constants';
import { useLoading } from 'components/useLoading';
import { AdminPageLayout } from 'components/PageLayout';
import { tabRenderer } from 'components/TabsWithCounter/renderers';
Expand Down Expand Up @@ -61,6 +64,16 @@ function Page() {
const [activeTab, setActiveTab] = useState<MentorRegistryTabsMode>(MentorRegistryTabsMode.New);
const [disciplines, setDisciplines] = useState<DisciplineDto[]>([]);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const [tagFilters, setTagFilters] = useState<string[]>([]);
const [combinedFilter, setCombinedFilter] = useState<CombinedFilter>({
preferredCourses: [],
preselectedCourses: [],
technicalMentoring: [],
githubId: [],
cityName: [],
});
const [currentPage, setCurrentPage] = useState(1);
const [total, setTotal] = useState(0);

const updateData = (showAll: boolean, allData: MentorRegistryDto[]) => {
setShowAll(showAll);
Expand All @@ -70,9 +83,25 @@ function Page() {
};

const loadData = withLoading(async () => {
const [allData, courses] = await Promise.all([mentorRegistryService.getMentors(), coursesService.getCourses()]);
const [allData, courses] = await Promise.all([
mentorRegistryService.getMentors({
pageSize: PAGINATION,
currentPage,
githubId: combinedFilter.githubId?.[0] ?? undefined,
cityName: combinedFilter.cityName?.[0] ?? undefined,
preferedCourses: combinedFilter.preferredCourses?.length
? combinedFilter.preferredCourses.map(Number)
: undefined,
preselectedCourses: combinedFilter.preselectedCourses?.length
? combinedFilter.preselectedCourses.map(Number)
: undefined,
technicalMentoring: combinedFilter.technicalMentoring?.length ? combinedFilter.technicalMentoring : undefined,
}),
coursesService.getCourses(),
]);
const { data: disciplines } = await disciplinesApi.getDisciplines();
setAllData(allData.mentors);
setTotal(allData.total);
setCourses(courses);
updateData(showAll, allData.mentors);
setDisciplines(disciplines);
Expand All @@ -97,7 +126,7 @@ function Page() {
}
});

useAsync(loadData, []);
useAsync(loadData, [combinedFilter, currentPage]);

const openNotificationWithIcon = (type: NotificationType) => {
api[type]({
Expand Down Expand Up @@ -231,6 +260,13 @@ function Page() {
activeTab={activeTab}
disciplines={disciplines}
handleModalDataChange={handleModalDataChange}
tagFilters={tagFilters}
setTagFilters={setTagFilters}
combinedFilter={combinedFilter}
setCombinedFilter={setCombinedFilter}
setCurrentPage={setCurrentPage}
currentPage={currentPage}
total={total}
>
{mentorRegistryProps => <MentorRegistryTable {...mentorRegistryProps} />}
</MentorRegistryTableContainer>
Expand Down
Loading