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

refactor: setup project tab selection component #77

Merged
merged 3 commits into from
Aug 8, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { faker } from '@faker-js/faker';

import { MockQueryResult } from '@/shared/testutils/misc';

import { ProjectSelections } from '@/grants/components/grantee-project/project-selection';
import { ProjectSelections } from '@/grants/components/grantee-project/project-selections';

import { mockGranteeProjectQuery } from '@/grants/testutils/mock-grantee-project-query';

Expand All @@ -30,22 +30,40 @@ const meta: Meta<typeof ProjectSelections> = {
export default meta;
type Story = StoryObj<typeof ProjectSelections>;

export const Success: Story = {
export const Loading: Story = {
parameters: {
msw: {
handlers: [
mockGranteeProjectQuery(MockQueryResult.SUCCESS, {
networkDelay: 'infinite',
}),
],
},
},
};

export const FirstProjectActive: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['projectId', 'project-1']],
},
},
msw: {
handlers: [mockGranteeProjectQuery(MockQueryResult.SUCCESS)],
},
},
};

export const Loading: Story = {
export const SecondProjectActive: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['projectId', 'project-2']],
},
},
msw: {
handlers: [
mockGranteeProjectQuery(MockQueryResult.SUCCESS, {
networkDelay: 'infinite',
}),
],
handlers: [mockGranteeProjectQuery(MockQueryResult.SUCCESS)],
},
},
};
25 changes: 1 addition & 24 deletions src/grants/components/grantee-project/project-selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ interface Props {
export const ProjectSelection = ({ firstId, projectId, baseHref }: Props) => {
const params = useParams();
const paramsProjectId = params.projectId as string;
const href = `${baseHref}/${projectId}`;

const isActive = paramsProjectId
? paramsProjectId === projectId
: projectId === firstId; // Default to first project if no project-id params

const { data } = useGranteeProject(paramsProjectId ?? firstId);

const href = `${baseHref}/${projectId}`;

if (!data) return <Skeleton className={cn(SHARED_CLASSNAME, 'h-14')} />;

return (
Expand All @@ -44,25 +43,3 @@ export const ProjectSelection = ({ firstId, projectId, baseHref }: Props) => {
</Link>
);
};

interface SelectionsProps {
baseHref: string;
projects: string[];
}

export const ProjectSelections = ({ baseHref, projects }: SelectionsProps) => {
if (projects.length === 0) return null;

return (
<div className="flex w-full gap-4 bg-white/5 p-4">
{projects.map((projectId) => (
<ProjectSelection
key={projectId}
firstId={projects[0]}
projectId={projectId}
baseHref={baseHref}
/>
))}
</div>
);
};
23 changes: 23 additions & 0 deletions src/grants/components/grantee-project/project-selections.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ProjectSelection } from './project-selection';

interface SelectionsProps {
baseHref: string;
projects: string[];
}

export const ProjectSelections = ({ baseHref, projects }: SelectionsProps) => {
if (projects.length === 0) return null;

return (
<div className="flex w-full gap-4 bg-white/5 p-4">
{projects.map((projectId) => (
<ProjectSelection
key={projectId}
firstId={projects[0]}
projectId={projectId}
baseHref={baseHref}
/>
))}
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Meta, StoryObj } from '@storybook/react';

import { faker } from '@faker-js/faker';

import { MockQueryResult } from '@/shared/testutils/misc';

import { mockGranteeProjectQuery } from '@/grants/testutils/mock-grantee-project-query';

import { ProjectTabSelection } from './project-tab-selection';

faker.seed(420);

const meta: Meta<typeof ProjectTabSelection> = {
title: 'grants/components/grantee-project/project-tab-selections',
component: ProjectTabSelection,
decorators: [
(Story) => {
const baseHref = '/grants/grant-1/grantees/grantee-1/projects';
const defaultId = 'project-1';

return (
<div className="flex w-full justify-center">
<Story args={{ baseHref, defaultId }} />
</div>
);
},
],
parameters: {
nextjs: {
navigation: {
segments: [['projectId', 'project-1']],
},
},
msw: {
handlers: [mockGranteeProjectQuery(MockQueryResult.SUCCESS)],
},
},
};

export default meta;
type Story = StoryObj<typeof ProjectTabSelection>;

export const OverallSummary: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['tab', 'summary']],
},
},
},
};

export const ImpactMetrics: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['tab', 'impact-metrics']],
},
},
},
};

export const GithubMetrics: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['tab', 'github-metrics']],
},
},
},
};

export const CodeMetrics: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['tab', 'code-metrics']],
},
},
},
};

export const ContractAddress: Story = {
parameters: {
nextjs: {
navigation: {
segments: [['tab', 'contract-address']],
},
},
},
};
2 changes: 1 addition & 1 deletion src/grants/pages/grantee-page-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GranteeCard } from '@/grants/components/grantee-card/grantee-card';
import { ProjectSelections } from '@/grants/components/grantee-project/project-selection';
import { ProjectSelections } from '@/grants/components/grantee-project/project-selections';
import { ProjectTabSelection } from '@/grants/components/grantee-project/project-tab-selection';

import { fakeGrantee } from '@/grants/testutils/fake-grantee';
Expand Down
2 changes: 1 addition & 1 deletion src/grants/testutils/mock-grantee-project-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const mockGranteeProjectQuery = (
const successResponse = HttpResponse.json({
success: true,
message: 'Grantee project retrieved successfully',
data: fakeGranteeProject({ id: projectId, name: 'Project 420' }),
data: fakeGranteeProject({ id: projectId }),
});
const internalErrorResponse = HttpResponse.json(
{ message: errMsg.INTERNAL },
Expand Down
Loading