Skip to content

Commit

Permalink
Merge pull request #77
Browse files Browse the repository at this point in the history
refactor: setup project tab selection component
  • Loading branch information
johnshift authored Aug 8, 2024
2 parents 1b50476 + 94640d4 commit ba0ba2c
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 79 deletions.

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

0 comments on commit ba0ba2c

Please sign in to comment.