Skip to content

Commit

Permalink
refactor: make generalized AlertContext and AlertProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Nov 8, 2023
1 parent fe434bd commit ae4afc5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ interface DatasetAlertContextProps {
removeDatasetAlert: (alertId: AlertMessageKey) => void
}

export const DatasetAlertContext = createContext<DatasetAlertContextProps>({
export const AlertContext = createContext<DatasetAlertContextProps>({
datasetAlerts: [],
addDatasetAlert: /* istanbul ignore next */ () => {},
removeDatasetAlert: /* istanbul ignore next */ () => {}
})
export const useAlertContext = () => useContext(DatasetAlertContext)
export const useAlertContext = () => useContext(AlertContext)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PropsWithChildren, useState } from 'react'
import { DatasetAlertContext } from './DatasetAlertContext'
import { AlertContext } from './AlertContext'

import { Alert, AlertMessageKey } from '../../alert/domain/models/Alert'

export const DatasetAlertProvider = ({ children }: PropsWithChildren) => {
export const AlertProvider = ({ children }: PropsWithChildren) => {
const [datasetAlerts, setDatasetAlerts] = useState<Alert[]>([])

const addDatasetAlert = (newAlert: Alert) => {
Expand All @@ -19,13 +19,13 @@ export const DatasetAlertProvider = ({ children }: PropsWithChildren) => {
}

return (
<DatasetAlertContext.Provider
<AlertContext.Provider
value={{
datasetAlerts,
addDatasetAlert,
removeDatasetAlert
}}>
{children}
</DatasetAlertContext.Provider>
</AlertContext.Provider>
)
}
2 changes: 1 addition & 1 deletion src/sections/alerts/Alerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Alert as AlertComponent } from '@iqss/dataverse-design-system'
import { useTranslation } from 'react-i18next'
import styles from './Alerts.module.scss'
import parse from 'html-react-parser'
import { useAlertContext } from '../dataset/DatasetAlertContext'
import { useAlertContext } from './AlertContext'
import { Alert } from '../../alert/domain/models/Alert'

export function Alerts() {
Expand Down
6 changes: 3 additions & 3 deletions src/sections/dataset/DatasetFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SettingJSDataverseRepository } from '../../settings/infrastructure/Sett
import { FilePermissionsProvider } from '../file/file-permissions/FilePermissionsProvider'
import { SettingsProvider } from '../settings/SettingsProvider'
import { DatasetProvider } from './DatasetProvider'
import { DatasetAlertProvider } from './DatasetAlertProvider'
import { AlertProvider } from '../alerts/AlertProvider'

const datasetRepository = new DatasetJSDataverseRepository()
const fileRepository = new FileJSDataverseRepository()
Expand All @@ -25,9 +25,9 @@ export class DatasetFactory {
<SettingsProvider repository={settingRepository}>
<MetadataBlockInfoProvider repository={metadataBlockInfoRepository}>
<AnonymizedProvider>
<DatasetAlertProvider>
<AlertProvider>
<DatasetWithSearchParams />
</DatasetAlertProvider>
</AlertProvider>
</AnonymizedProvider>
</MetadataBlockInfoProvider>
</SettingsProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/sections/dataset/dataset-alerts/DatasetAlerts.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAlertContext } from '../DatasetAlertContext'
import { useAlertContext } from '../../alerts/AlertContext'
import { Alerts } from '../../alerts/Alerts'
import { Alert } from '../../../alert/domain/models/Alert'

Expand Down
6 changes: 3 additions & 3 deletions src/stories/WithAlerts.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { StoryFn } from '@storybook/react'
import { DatasetAlertProvider } from '../sections/dataset/DatasetAlertProvider'
import { AlertProvider } from '../sections/alerts/AlertProvider'

export const WithAlerts = (Story: StoryFn) => {
return (
<DatasetAlertProvider>
<AlertProvider>
<Story />
</DatasetAlertProvider>
</AlertProvider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DatasetMother,
DatasetPermissionsMother
} from '../../../../tests/component/dataset/domain/models/DatasetMother'
import { useAlertContext } from '../../../sections/dataset/DatasetAlertContext'
import { useAlertContext } from '../../../sections/alerts/AlertContext'
import { WithAlerts } from '../../WithAlerts'
import { Alert, AlertMessageKey } from '../../../alert/domain/models/Alert'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
DatasetVersionMother
} from '../../../dataset/domain/models/DatasetMother'
import { Alert, AlertMessageKey } from '../../../../../src/alert/domain/models/Alert'
import { DatasetAlertProvider } from '../../../../../src/sections/dataset/DatasetAlertProvider'
import { AlertProvider } from '../../../../../src/sections/alerts/AlertProvider'

function removeMarkup(htmlString: string): string {
// Use a regular expression to match HTML tags and replace them with an empty string
Expand Down Expand Up @@ -48,9 +48,9 @@ it('renders the correct number of alerts', () => {
]
cy.fixture('../../../public/locales/en/dataset.json').then((dataset: DatasetTranslation) => {
cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={alerts} />
</DatasetAlertProvider>
</AlertProvider>
)
const headingProps = [
dataset.alerts.draftVersion.heading,
Expand All @@ -70,9 +70,9 @@ it('renders alerts with correct text', () => {

cy.fixture('../../../public/locales/en/dataset.json').then((dataset: DatasetTranslation) => {
cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={alerts} />
</DatasetAlertProvider>
</AlertProvider>
)
const alertHeading = dataset.alerts[draftAlert.messageKey].heading
const alertText = removeMarkup(dataset.alerts[draftAlert.messageKey].alertText)
Expand All @@ -95,9 +95,9 @@ it('renders dynamic text', () => {
dynamicFields
)
cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={[notFoundAlert]} />
</DatasetAlertProvider>
</AlertProvider>
)

cy.findByRole('alert').should('contain.text', dynamicFields.requestedVersion)
Expand All @@ -110,9 +110,9 @@ it('shows draft alert if version is DRAFT', () => {
})

cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={dataset.alerts} />
</DatasetAlertProvider>
</AlertProvider>
)

cy.findByRole('alert').should('contain.text', 'draft')
Expand All @@ -124,9 +124,9 @@ it('does not show draft alert if version is RELEASED', () => {
})

cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={dataset.alerts} />
</DatasetAlertProvider>
</AlertProvider>
)
cy.findByRole('alert').should('not.exist')
})
Expand All @@ -142,9 +142,9 @@ it('shows draft & share private url message if privateUrl exists and user can ed
}
})
cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={dataset.alerts} />
</DatasetAlertProvider>
</AlertProvider>
)
const expectedMessageKeys = [
AlertMessageKey.DRAFT_VERSION,
Expand All @@ -169,9 +169,9 @@ it('shows private url message only if privateUrl exists and user cannot edit',
}
})
cy.mount(
<DatasetAlertProvider>
<AlertProvider>
<DatasetAlerts alerts={dataset.alerts} />
</DatasetAlertProvider>
</AlertProvider>
)
const expectedMessageKey = AlertMessageKey.UNPUBLISHED_DATASET

Expand Down

0 comments on commit ae4afc5

Please sign in to comment.