Skip to content

Commit

Permalink
feat(DatasetsList): add no datasets message
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Nov 21, 2023
1 parent 5484fef commit 4985949
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 20 deletions.
6 changes: 5 additions & 1 deletion public/locales/en/home.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"title": "Root"
"title": "Root",
"noDatasetsMessage": {
"authenticated": "This dataverse currently has no datasets. You can add to it by using the Add Data button on this page.",
"anonymous": "This dataverse currently has no datasets. Please <1>log in</1> to see if you are able to add to it."
}
}
7 changes: 5 additions & 2 deletions src/sections/home/datasets-list/DatasetsList.module.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
@import "node_modules/@iqss/dataverse-design-system/src/lib/assets/styles/design-tokens/colors.module";
@import "node_modules/@iqss/dataverse-design-system/src/lib/assets/styles/design-tokens/typography.module";
@import "src/sections/assets/variables";

.container {
padding:15px;
border: 1px solid #ddd;
border-radius: 4px;
}

.empty-message-container {
padding: .5em 1em;
background: $dv-warning-box-color;
}
40 changes: 25 additions & 15 deletions src/sections/home/datasets-list/DatasetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { LinkToPage } from '../../shared/link-to-page/LinkToPage'
import { Route } from '../../Route.enum'
import { useLoading } from '../../loading/LoadingContext'
import { DatasetsListSkeleton } from './DatasetsListSkeleton'
import { NoDatasetsMessage } from './NoDatasetsMessage'

interface DatasetsListProps {
datasetRepository: DatasetRepository
}
const NO_DATASETS = 0
export function DatasetsList({ datasetRepository }: DatasetsListProps) {
const { setIsLoading } = useLoading()
const [paginationInfo, setPaginationInfo] = useState<DatasetPaginationInfo>(
Expand All @@ -30,21 +32,29 @@ export function DatasetsList({ datasetRepository }: DatasetsListProps) {

return (
<section className={styles.container}>
<div>
<PaginationResultsInfo paginationInfo={paginationInfo} />
</div>
{datasets.map((dataset) => (
<article key={dataset.persistentId}>
<LinkToPage page={Route.DATASETS} searchParams={{ persistentId: dataset.persistentId }}>
{dataset.title}
</LinkToPage>
</article>
))}
<PaginationControls
onPaginationInfoChange={setPaginationInfo}
initialPaginationInfo={paginationInfo}
showPageSizeSelector={false}
/>
{datasets.length === NO_DATASETS ? (
<NoDatasetsMessage />
) : (
<>
<div>
<PaginationResultsInfo paginationInfo={paginationInfo} />
</div>
{datasets.map((dataset) => (
<article key={dataset.persistentId}>
<LinkToPage
page={Route.DATASETS}
searchParams={{ persistentId: dataset.persistentId }}>
{dataset.title}
</LinkToPage>
</article>
))}
<PaginationControls
onPaginationInfoChange={setPaginationInfo}
initialPaginationInfo={paginationInfo}
showPageSizeSelector={false}
/>
</>
)}
</section>
)
}
24 changes: 24 additions & 0 deletions src/sections/home/datasets-list/NoDatasetsMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import styles from './DatasetsList.module.scss'
import { Trans, useTranslation } from 'react-i18next'
import { useSession } from '../../session/SessionContext'
import { Route } from '../../Route.enum'

export function NoDatasetsMessage() {
const { t } = useTranslation('home')
const { user } = useSession()

return (
<div className={styles['empty-message-container']}>
{user ? (
<p>{t('noDatasetsMessage.authenticated')}</p>
) : (
<Trans i18nKey="noDatasetsMessage.anonymous">
<p>
This dataverse currently has no datasets. Please <a href={Route.LOG_IN}>log in</a> to
see if you are able to add to it.
</p>
</Trans>
)}
</div>
)
}
2 changes: 1 addition & 1 deletion src/sections/home/datasets-list/useDatasets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useDatasets(
})
}
const fetchDatasets = (totalDatasetsCount: TotalDatasetsCount) => {
if (totalDatasetsCount) {
if (typeof totalDatasetsCount !== 'undefined') {
if (totalDatasetsCount === 0) {
setIsLoading(false)
return
Expand Down
22 changes: 22 additions & 0 deletions src/stories/dataset/NoDatasetsMockRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DatasetMockRepository } from './DatasetMockRepository'
import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo'
import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview'
import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount'
export class NoDatasetsMockRepository extends DatasetMockRepository {
// eslint-disable-next-line unused-imports/no-unused-vars
getAll(paginationInfo: DatasetPaginationInfo): Promise<DatasetPreview[]> {
return new Promise((resolve) => {
setTimeout(() => {
resolve([])
}, 1000)
})
}

getTotalDatasetsCount(): Promise<TotalDatasetsCount> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(0)
}, 1000)
})
}
}
5 changes: 5 additions & 0 deletions src/stories/home/Home.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WithI18next } from '../WithI18next'
import { WithLayout } from '../WithLayout'
import { DatasetMockRepository } from '../dataset/DatasetMockRepository'
import { DatasetLoadingMockRepository } from '../dataset/DatasetLoadingMockRepository'
import { NoDatasetsMockRepository } from '../dataset/NoDatasetsMockRepository'

const meta: Meta<typeof Home> = {
title: 'Pages/Home',
Expand All @@ -21,3 +22,7 @@ export const Default: Story = {
export const Loading: Story = {
render: () => <Home datasetRepository={new DatasetLoadingMockRepository()} />
}

export const NoResults: Story = {
render: () => <Home datasetRepository={new NoDatasetsMockRepository()} />
}
17 changes: 16 additions & 1 deletion src/stories/home/datasets-list/DatasetsList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Home } from '../../../sections/home/Home'
import { WithI18next } from '../../WithI18next'
import { DatasetMockRepository } from '../../dataset/DatasetMockRepository'
import { DatasetsList } from '../../../sections/home/datasets-list/DatasetsList'
import { DatasetLoadingMockRepository } from '../../dataset/DatasetLoadingMockRepository'
import { NoDatasetsMockRepository } from '../../dataset/NoDatasetsMockRepository'
import { MemoryRouter } from 'react-router-dom'

const meta: Meta<typeof Home> = {
title: 'Sections/Home/DatasetsList',
Expand All @@ -14,5 +17,17 @@ export default meta
type Story = StoryObj<typeof Home>

export const Default: Story = {
render: () => <DatasetsList datasetRepository={new DatasetMockRepository()} />
render: () => (
<MemoryRouter>
<DatasetsList datasetRepository={new DatasetMockRepository()} />
</MemoryRouter>
)
}

export const Loading: Story = {
render: () => <DatasetsList datasetRepository={new DatasetLoadingMockRepository()} />
}

export const NoResults: Story = {
render: () => <DatasetsList datasetRepository={new NoDatasetsMockRepository()} />
}
23 changes: 23 additions & 0 deletions src/stories/home/datasets-list/NoDatasetsMessage.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Meta, StoryObj } from '@storybook/react'
import { Home } from '../../../sections/home/Home'
import { WithI18next } from '../../WithI18next'
import { NoDatasetsMessage } from '../../../sections/home/datasets-list/NoDatasetsMessage'
import { WithLoggedInUser } from '../../WithLoggedInUser'

const meta: Meta<typeof Home> = {
title: 'Sections/Home/NoDatasetsMessage',
component: Home,
decorators: [WithI18next]
}

export default meta
type Story = StoryObj<typeof Home>

export const AnonymousUser: Story = {
render: () => <NoDatasetsMessage />
}

export const AuthenticatedUser: Story = {
decorators: [WithLoggedInUser],
render: () => <NoDatasetsMessage />
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ describe('Datasets List', () => {
})
})

it('renders no datasets message when there are no datasets', () => {
datasetRepository.getAll = cy.stub().resolves([])
cy.customMount(<DatasetsList datasetRepository={datasetRepository} />)

cy.findByText(/This dataverse currently has no datasets./).should('exist')
})

it('renders the datasets list', () => {
cy.customMount(<DatasetsList datasetRepository={datasetRepository} />)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NoDatasetsMessage } from '../../../../../src/sections/home/datasets-list/NoDatasetsMessage'

describe('No Datasets Message', () => {
it('renders the message for anonymous user', () => {
cy.customMount(<NoDatasetsMessage />)
cy.findByText(/This dataverse currently has no datasets. Please /).should('exist')
cy.findByRole('link', { name: 'log in' }).should(
'have.attr',
'href',
'/loginpage.xhtml?redirectPage=%2Fdataverse.xhtml'
)
})

it('renders the message for authenticated user', () => {
cy.mountAuthenticated(<NoDatasetsMessage />)
cy.findByText(
'This dataverse currently has no datasets. You can add to it by using the Add Data button on this page.'
).should('exist')
})
})

0 comments on commit 4985949

Please sign in to comment.