Skip to content

Commit

Permalink
feat(FilePage): add route to the page
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Dec 13, 2023
1 parent 4090af5 commit 9864ba1
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Preview } from '@storybook/react'
import { ThemeProvider } from '@iqss/dataverse-design-system'
import { MemoryRouter } from 'react-router-dom'

const preview: Preview = {
parameters: {
Expand All @@ -14,7 +15,9 @@ const preview: Preview = {
decorators: [
(Story) => (
<ThemeProvider>
<Story />
<MemoryRouter>
<Story />
</MemoryRouter>
</ThemeProvider>
)
]
Expand Down
7 changes: 6 additions & 1 deletion src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HelloDataverse } from './sections/hello-dataverse/HelloDataverse'
import { Layout } from './sections/layout/Layout'
import { Route } from './sections/Route.enum'
import { DatasetFactory } from './sections/dataset/DatasetFactory'
import { FileFactory } from './sections/file/FileFactory'

const router = createBrowserRouter(
[
Expand All @@ -15,8 +16,12 @@ const router = createBrowserRouter(
element: <HelloDataverse />
},
{
path: `${Route.DATASETS}`,
path: Route.DATASETS,
element: DatasetFactory.create()
},
{
path: Route.FILES,
element: FileFactory.create()
}
]
}
Expand Down
4 changes: 0 additions & 4 deletions src/files/domain/models/FilePreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,6 @@ export class FilePreview {
readonly checksum?: FileChecksum
) {}

getLink(): string {
return `/file?id=${this.id}&version=${this.version.number}`
}

get isActivelyEmbargoed(): boolean {
if (this.embargo) {
return this.embargo.isActive
Expand Down
3 changes: 2 additions & 1 deletion src/sections/Route.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export enum Route {
SIGN_UP = '/dataverseuser.xhtml?editMode=CREATE&redirectPage=%2Fdataverse.xhtml',
LOG_IN = '/loginpage.xhtml?redirectPage=%2Fdataverse.xhtml',
LOG_OUT = '/',
DATASETS = 'datasets'
DATASETS = '/datasets',
FILES = '/files'
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function FileInfoCell({ file }: { file: FilePreview }) {
<FileThumbnail file={file} />
</div>
<div className={styles['body-container']}>
<FileTitle link={file.getLink()} name={file.name} />
<FileTitle id={file.id} name={file.name} />
<div className={styles['body-container__subtext']}>
<FileDirectory directory={file.directory} />
<FileType type={file.type} size={file.size} />
Expand Down
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>
)
}
24 changes: 24 additions & 0 deletions src/sections/file/FileFactory.tsx
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} />
}
20 changes: 20 additions & 0 deletions src/sections/shared/link-to-page/LinkToPage.tsx
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('&')
}
14 changes: 6 additions & 8 deletions src/stories/WithLayout.tsx
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>
)
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 tests/component/sections/shared/link-to-page/LinkToPage.spec.tsx
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')
})
})
6 changes: 6 additions & 0 deletions tests/e2e-integration/e2e/sections/file/File.spec.tsx
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')
})
})
9 changes: 6 additions & 3 deletions tests/support/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ import { I18nextProvider } from 'react-i18next'
import i18next from '../../src/i18n'
import { UserRepository } from '../../src/users/domain/repositories/UserRepository'
import { SessionProvider } from '../../src/sections/session/SessionProvider'
import { MemoryRouter } from 'react-router-dom'

// Define your custom mount function

Cypress.Commands.add('customMount', (component: ReactNode) => {
return cy.mount(
<ThemeProvider>
<I18nextProvider i18n={i18next}>{component}</I18nextProvider>
</ThemeProvider>
<MemoryRouter>
<ThemeProvider>
<I18nextProvider i18n={i18next}>{component}</I18nextProvider>
</ThemeProvider>
</MemoryRouter>
)
})

Expand Down

0 comments on commit 9864ba1

Please sign in to comment.