Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Nov 1, 2024
1 parent d1c203f commit 03ef567
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 23 deletions.
4 changes: 4 additions & 0 deletions api/core/_transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export function languageQueryParamsToDto(
];

return {
open: query?.open,
slugs: query?.projects,
certified: query?.certified,
labels: combinedLabels.length > 0 ? combinedLabels : undefined,
with_technologies: query?.withTechnologies,
certified_or_labels: combinedLabels.length > 0 && query?.certified,
};
}
7 changes: 6 additions & 1 deletion api/core/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export async function getAllLanguages(query?: LanguageQueryParams) {
const response = await coreApiClient.get<string[]>(url, {
tag: tags.languages,
});
return response;

const uniqueLanguages = Array.from(
new Set(response.map((language) => language.toLowerCase())),
);

return uniqueLanguages;
}

export default { getAllLanguages };
1 change: 1 addition & 0 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default async function Home() {
offset: 0,
limit: DEFAULT_HOMEPAGE_PAGE_SIZE,
certified: true,
open: true,
},
tags.latestIssues,
).catch((error) => {
Expand Down
15 changes: 5 additions & 10 deletions app/projects/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import KudosWeeksBanner from "@/components/kudos-weeks-banner";
import { DEFAULT_QUERY } from "@/data/fetch";
import { TECHNOLOGY_KEY } from "@/data/filters";
import { fetchProjectIssues } from "@/lib/api/issues";
import { fetchProjectInfo } from "@/lib/api/projects";
import { fetchProject, fetchProjectInfo } from "@/lib/api/projects";
import { buildCheckboxFilters } from "@/lib/filters";
import { Issue, IssueQueryParams } from "@/types/issue";
import { PaginatedCustomResponse } from "@/types/pagination";
Expand All @@ -22,12 +22,6 @@ const SELECT_FILTERS: SelectFilterConfig[] = [
{ key: TECHNOLOGY_KEY, options: [] },
];

function getUniqueRepositoryIds(issues: PaginatedCustomResponse<Issue>) {
return Array.from(
new Set(issues.data.map(({ repository }) => repository.id)),
);
}

interface IProps {
params: { slug: string };
}
Expand All @@ -41,12 +35,13 @@ export default async function SingleProjectPage({ params }: IProps) {
const query: IssueQueryParams = {
projects: [slug],
certified: true,
open: true,
};
const project = await fetchProject(slug);
const issues = await fetchProjectIssues(slug, query);
const metrics = await constructProjectMetrics(infos, issues);

const labels = constructLabels(infos, metrics);
const repositoryIds = getUniqueRepositoryIds(issues);
const checkboxFilters = buildCheckboxFilters(metrics);

return (
Expand All @@ -56,7 +51,7 @@ export default async function SingleProjectPage({ params }: IProps) {
>
<div className="flex-grow md:basis-1/2 lg:basis-3/4">
<ProjectHeader
avatar={issues.data[0]?.project.avatar}
avatar={project?.avatar ?? null}
name={infos.name}
description={infos.description}
links={infos.links}
Expand Down Expand Up @@ -123,7 +118,7 @@ export default async function SingleProjectPage({ params }: IProps) {
</section>
)}

<DefaultFiltersProvider repositoryIds={repositoryIds}>
<DefaultFiltersProvider slugs={[infos.slug]}>
<div className="flex flex-col">
<Toolbar
label={`${infos?.name ?? "Open"} contributions`}
Expand Down
8 changes: 4 additions & 4 deletions components/providers/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { initFilters } from "@/utils/filters";

export async function DefaultFiltersProvider({
children,
repositoryIds,
slugs,
}: {
children: React.ReactNode;
repositoryIds?: number[];
slugs?: string[];
}) {
const filters = initFilters();
const filterOptions = await (
repositoryIds && repositoryIds.length > 0
? getProjectFilterOptions(repositoryIds)
slugs && slugs.length > 0
? getProjectFilterOptions(slugs)
: getFilterOptions()
).catch((error) => {
console.error("Error fetching filter options:", error);
Expand Down
14 changes: 13 additions & 1 deletion lib/api/projects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ConfigApi from "@/api/config/api";
import ProjectApi from "@/api/core/projects";
import { DEFAULT_BIG_PAGE_SIZE } from "@/data/fetch";
import { DEFAULT_BIG_PAGE_SIZE, DEFAULT_QUERY } from "@/data/fetch";
import { IFilterOption } from "@/types/filters";
import { Project } from "@/types/project";
import tags from "@/utils/tags";
Expand Down Expand Up @@ -32,6 +32,18 @@ async function fetchPaginatedProjects(tag?: string): Promise<Project[]> {
return projects;
}

export async function fetchProject(slug: string): Promise<Project | undefined> {
const res = await ProjectApi.getProjects({
slugs: [slug],
...DEFAULT_QUERY,
}).catch((error) => {
console.error(`Error fetching issues for project "${slug}":`, error);
return undefined;
});

return res?.data[0];
}

export async function getAllProjects(): Promise<Project[]> {
return (await fetchPaginatedProjects(tags.allProjects)).sort((a, b) =>
a.name.localeCompare(b.name),
Expand Down
17 changes: 10 additions & 7 deletions lib/filters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LanguagesApi from "@/api/core/languages";
import LanguagesApi, { getAllLanguages } from "@/api/core/languages";
import RepositoriesApi from "@/api/core/repositories";
import { KudosCertifiedIcon } from "@/assets/icons";
import { CheckboxFilterConfig } from "@/components/filters/config";
Expand Down Expand Up @@ -27,16 +27,16 @@ import { createFilterOptions } from "@/utils/filters";
import { getAllProjectOptions } from "./api/projects";

export async function getProjectFilterOptions(
repositoryIds: number[],
projects: string[],
): Promise<FilterOptions> {
let languageValues: string[] = [];

try {
const languagePromises = repositoryIds.map((id) =>
RepositoriesApi.getRepositoryById(id).then((repo) => repo.language),
);

languageValues = await Promise.all(languagePromises);
languageValues = await getAllLanguages({
open: true,
certified: true,
projects,
});
} catch (error) {
console.error("Error fetching language values - ", error);
languageValues = [];
Expand All @@ -60,6 +60,8 @@ export async function getFilterOptions(): Promise<FilterOptions> {
let languageValues: string[];
try {
languageValues = await LanguagesApi.getAllLanguages({
open: true,
certified: true,
withTechnologies: true,
});
} catch (error) {
Expand Down Expand Up @@ -101,6 +103,7 @@ export function filtersToIssuesQuery(
offset: 0,
limit: DEFAULT_BIG_PAGE_SIZE,
open: true,
certified: true,
};

if (filters[TECHNOLOGY_KEY].length > 0) {
Expand Down
7 changes: 7 additions & 0 deletions types/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ export type Repository = {
};

export type LanguageQueryParams = Partial<{
open: boolean;
certified: boolean;
labels: string[];
projects: string[];
withTechnologies: boolean;
}>;

export type LanguageQueryParamsDto = Partial<{
slugs: string[];
open: boolean;
certified: boolean;
labels: string[];
with_technologies: boolean;
certified_or_labels: boolean;
}>;

0 comments on commit 03ef567

Please sign in to comment.