-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FilePage): add route to the page
- Loading branch information
Showing
13 changed files
with
101 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 10 additions & 3 deletions
13
...s/dataset/dataset-files/files-table/file-info/file-info-cell/file-info-data/FileTitle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { LinkToPage } from '../../../../../../shared/link-to-page/LinkToPage' | ||
import { Route } from '../../../../../../Route.enum' | ||
|
||
interface FileTitleProps { | ||
link: string | ||
id: number | ||
name: string | ||
} | ||
|
||
export function FileTitle({ link, name }: FileTitleProps) { | ||
return <a href={link}>{name}</a> | ||
export function FileTitle({ id, name }: FileTitleProps) { | ||
return ( | ||
<LinkToPage page={Route.FILES} searchParams={{ id: id.toString() }}> | ||
{name} | ||
</LinkToPage> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ReactElement } from 'react' | ||
import { FileJSDataverseRepository } from '../../files/infrastructure/FileJSDataverseRepository' | ||
import { File } from './File' | ||
import { useSearchParams } from 'react-router-dom' | ||
import { PageNotFound } from '../page-not-found/PageNotFound' | ||
|
||
const repository = new FileJSDataverseRepository() | ||
export class FileFactory { | ||
static create(): ReactElement { | ||
return <FileWithSearchParams /> | ||
} | ||
} | ||
|
||
function FileWithSearchParams() { | ||
const [searchParams] = useSearchParams() | ||
const searchParamId = searchParams.get('id') ?? undefined | ||
const id = searchParamId ? parseInt(searchParamId) : undefined | ||
|
||
if (!id) { | ||
return <PageNotFound /> | ||
} | ||
|
||
return <File repository={repository} id={id} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Link } from 'react-router-dom' | ||
import { PropsWithChildren } from 'react' | ||
import { Route } from '../../Route.enum' | ||
|
||
interface LinkToPageProps { | ||
page: Route | ||
searchParams?: Record<string, string> | ||
} | ||
|
||
export function LinkToPage({ children, page, searchParams }: PropsWithChildren<LinkToPageProps>) { | ||
const searchParamsString: string = searchParams ? '?' + encodeSearchParamsToURI(searchParams) : '' | ||
|
||
return <Link to={`${page}${searchParamsString}`}>{children}</Link> | ||
} | ||
|
||
const encodeSearchParamsToURI = (searchParams: Record<string, string>) => { | ||
return Object.entries(searchParams) | ||
.map(([key, value]) => `${key}=${value}`) | ||
.join('&') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import { StoryFn } from '@storybook/react' | ||
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom' | ||
import { Routes, Route } from 'react-router-dom' | ||
import { Layout } from '../sections/layout/Layout' | ||
import { LoadingProvider } from '../sections/loading/LoadingProvider' | ||
|
||
export const WithLayout = (Story: StoryFn) => ( | ||
<LoadingProvider> | ||
<Router> | ||
<Routes> | ||
<Route element={<Layout />}> | ||
<Route path="/*" element={<Story />} /> | ||
</Route> | ||
</Routes> | ||
</Router> | ||
<Routes> | ||
<Route element={<Layout />}> | ||
<Route path="/*" element={<Story />} /> | ||
</Route> | ||
</Routes> | ||
</LoadingProvider> | ||
) |
11 changes: 2 additions & 9 deletions
11
...set/dataset-files/files-table/files-info/file-info-cell/file-info-data/FileTitle.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,17 @@ | ||
import { FilePreviewMother } from '../../../../../../../files/domain/models/FilePreviewMother' | ||
import { FileTitle } from '../../../../../../../../../src/sections/dataset/dataset-files/files-table/file-info/file-info-cell/file-info-data/FileTitle' | ||
import { FilePublishingStatus } from '../../../../../../../../../src/files/domain/models/FilePreview' | ||
|
||
describe('FileTitle', () => { | ||
it('renders the link and name correctly', () => { | ||
const id = 12345 | ||
const versionParameter = '&version=1' | ||
const name = 'file-name.txt' | ||
const file = FilePreviewMother.create({ | ||
id: id, | ||
version: { number: 1, publishingStatus: FilePublishingStatus.RELEASED }, | ||
name: name | ||
}) | ||
|
||
cy.customMount(<FileTitle link={file.getLink()} name={file.name} />) | ||
cy.customMount(<FileTitle id={file.id} name={file.name} />) | ||
|
||
cy.findByRole('link', { name: name }).should( | ||
'have.attr', | ||
'href', | ||
`/file?id=${id}${versionParameter}` | ||
) | ||
cy.findByRole('link', { name: name }).should('have.attr', 'href', `/files?id=${id}`) | ||
}) | ||
}) |
14 changes: 14 additions & 0 deletions
14
tests/component/sections/shared/link-to-page/LinkToPage.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { LinkToPage } from '../../../../../src/sections/shared/link-to-page/LinkToPage' | ||
import { Route } from '../../../../../src/sections/Route.enum' | ||
|
||
describe('LinkToPage', () => { | ||
it('renders a link to the page with the given search params', () => { | ||
cy.customMount(<LinkToPage page={Route.DATASETS} searchParams={{ foo: 'bar' }} />) | ||
cy.findByRole('link').should('have.attr', 'href', '/datasets?foo=bar') | ||
}) | ||
|
||
it('renders a link to the page without search params', () => { | ||
cy.customMount(<LinkToPage page={Route.DATASETS} />) | ||
cy.findByRole('link').should('have.attr', 'href', '/datasets') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
describe('File', () => { | ||
it('successfully loads', () => { | ||
cy.visit('/spa/files?id=23') | ||
cy.findAllByText('file.csv').should('exist') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters