From 3a5d7fe3ae7ece804bb1834b86a5753e2158d0c3 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Wed, 8 Nov 2023 17:19:25 -0500 Subject: [PATCH] fix: Add WithAlerts.tsx to Dataset.stories.tsx and DatasetAlert.stories.tsx --- src/sections/dataset/DatasetAlertProvider.tsx | 6 +++++- src/stories/WithAlerts.tsx | 10 ++++++++++ src/stories/dataset/Dataset.stories.tsx | 3 ++- .../dataset/dataset-alerts/DatasetAlert.stories.tsx | 11 ++--------- 4 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 src/stories/WithAlerts.tsx diff --git a/src/sections/dataset/DatasetAlertProvider.tsx b/src/sections/dataset/DatasetAlertProvider.tsx index 6d5640ffa..3d8697d64 100644 --- a/src/sections/dataset/DatasetAlertProvider.tsx +++ b/src/sections/dataset/DatasetAlertProvider.tsx @@ -7,7 +7,11 @@ export const DatasetAlertProvider = ({ children }: PropsWithChildren) => { const [datasetAlerts, setDatasetAlerts] = useState([]) const addDatasetAlert = (newAlert: DatasetAlert) => { - datasetAlerts.push(newAlert) + // Check if an alert with the same id already exists + const alertExists = datasetAlerts.some((alert) => alert.messageKey === newAlert.messageKey) + + // If it doesn't exist, add it to the array + if (!alertExists) datasetAlerts.push(newAlert) } const removeDatasetAlert = (alertId: DatasetAlertMessageKey) => { diff --git a/src/stories/WithAlerts.tsx b/src/stories/WithAlerts.tsx new file mode 100644 index 000000000..d6f70e92b --- /dev/null +++ b/src/stories/WithAlerts.tsx @@ -0,0 +1,10 @@ +import { StoryFn } from '@storybook/react' +import { DatasetAlertProvider } from '../sections/dataset/DatasetAlertProvider' + +export const WithAlerts = (Story: StoryFn) => { + return ( + + + + ) +} diff --git a/src/stories/dataset/Dataset.stories.tsx b/src/stories/dataset/Dataset.stories.tsx index ab3c7e26e..0bfd77785 100644 --- a/src/stories/dataset/Dataset.stories.tsx +++ b/src/stories/dataset/Dataset.stories.tsx @@ -15,11 +15,12 @@ import { WithDatasetDraftAsOwner } from './WithDatasetDraftAsOwner' import { WithDatasetNotFound } from './WithDatasetNotFound' import { WithDatasetLoading } from './WithDatasetLoading' import { WithLoggedInUser } from '../WithLoggedInUser' +import { WithAlerts } from '../WithAlerts' const meta: Meta = { title: 'Pages/Dataset', component: Dataset, - decorators: [WithI18next, WithCitationMetadataBlockInfo, WithSettings], + decorators: [WithI18next, WithCitationMetadataBlockInfo, WithSettings, WithAlerts], parameters: { // Sets the delay for all stories. chromatic: { delay: 15000, pauseAnimationAtEnd: true } diff --git a/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx b/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx index 583269980..bd55a7930 100644 --- a/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx +++ b/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx @@ -13,20 +13,13 @@ import { DatasetMother, DatasetPermissionsMother } from '../../../../tests/component/dataset/domain/models/DatasetMother' -import { DatasetAlertProvider } from '../../../sections/dataset/DatasetAlertProvider' import { useAlertContext } from '../../../sections/dataset/DatasetAlertContext' +import { WithAlerts } from '../../WithAlerts' const meta: Meta = { title: 'Sections/Dataset Page/DatasetAlerts', component: DatasetAlerts, - decorators: [ - WithI18next, - (Story) => ( - - - - ) - ] + decorators: [WithI18next, WithAlerts] } const allUpdateAlerts: DatasetAlert[] = [ new DatasetAlert('success', DatasetAlertMessageKey.METADATA_UPDATED),