-
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.
Merge pull request #217 from IQSS/feature/211-edit-publish-alert-mess…
…ages Feature/211 edit publish alert messages
- Loading branch information
Showing
15 changed files
with
274 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { AlertVariant } from '@iqss/dataverse-design-system/dist/components/alert/AlertVariant' | ||
|
||
export enum AlertMessageKey { | ||
DRAFT_VERSION = 'draftVersion', | ||
REQUESTED_VERSION_NOT_FOUND = 'requestedVersionNotFound', | ||
REQUESTED_VERSION_NOT_FOUND_SHOW_DRAFT = 'requestedVersionNotFoundShowDraft', | ||
SHARE_UNPUBLISHED_DATASET = 'shareUnpublishedDataset', | ||
UNPUBLISHED_DATASET = 'unpublishedDataset', | ||
METADATA_UPDATED = 'metadataUpdated', | ||
FILES_UPDATED = 'filesUpdated', | ||
TERMS_UPDATED = 'termsUpdated', | ||
THUMBNAIL_UPDATED = 'thumbnailUpdated', | ||
DATASET_DELETED = 'datasetDeleted', | ||
PUBLISH_IN_PROGRESS = 'publishInProgress' | ||
} | ||
|
||
export class Alert { | ||
constructor( | ||
public readonly variant: AlertVariant, | ||
public readonly messageKey: AlertMessageKey, | ||
public dynamicFields?: object | ||
) {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createContext, useContext } from 'react' | ||
|
||
import { Alert, AlertMessageKey } from '../../alert/domain/models/Alert' | ||
|
||
interface DatasetAlertContextProps { | ||
datasetAlerts: Alert[] | ||
addDatasetAlert: (newAlert: Alert) => void | ||
removeDatasetAlert: (alertId: AlertMessageKey) => void | ||
} | ||
|
||
export const AlertContext = createContext<DatasetAlertContextProps>({ | ||
datasetAlerts: [], | ||
addDatasetAlert: /* istanbul ignore next */ () => {}, | ||
removeDatasetAlert: /* istanbul ignore next */ () => {} | ||
}) | ||
export const useAlertContext = () => useContext(AlertContext) |
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,31 @@ | ||
import { PropsWithChildren, useState } from 'react' | ||
import { AlertContext } from './AlertContext' | ||
|
||
import { Alert, AlertMessageKey } from '../../alert/domain/models/Alert' | ||
|
||
export const AlertProvider = ({ children }: PropsWithChildren) => { | ||
const [datasetAlerts, setDatasetAlerts] = useState<Alert[]>([]) | ||
|
||
const addDatasetAlert = (newAlert: Alert) => { | ||
// 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: AlertMessageKey) => { | ||
setDatasetAlerts(datasetAlerts.filter((alert) => alert.messageKey !== alertId)) | ||
} | ||
|
||
return ( | ||
<AlertContext.Provider | ||
value={{ | ||
datasetAlerts, | ||
addDatasetAlert, | ||
removeDatasetAlert | ||
}}> | ||
{children} | ||
</AlertContext.Provider> | ||
) | ||
} |
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,4 @@ | ||
.container > * { | ||
margin-top: 1em; | ||
margin-right: 0.5em; | ||
} |
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,32 @@ | ||
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 './AlertContext' | ||
import { Alert } from '../../alert/domain/models/Alert' | ||
|
||
export function Alerts() { | ||
const { t } = useTranslation('dataset') | ||
const { datasetAlerts } = useAlertContext() | ||
return ( | ||
<div className={styles.container}> | ||
{datasetAlerts.map((alert: Alert, index) => { | ||
const translatedMsg = alert.dynamicFields | ||
? t(`alerts.${alert.messageKey}.alertText`, alert.dynamicFields) | ||
: t(`alerts.${alert.messageKey}.alertText`) | ||
const translatedHeading = t(`alerts.${alert.messageKey}.heading`) | ||
const alertKey = `alert-${index}` | ||
return ( | ||
<AlertComponent | ||
key={alertKey} | ||
variant={alert.variant} | ||
customHeading={translatedHeading} | ||
dismissible={false}> | ||
{parse(translatedMsg)} | ||
</AlertComponent> | ||
) | ||
})} | ||
</div> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,13 @@ | ||
import { Alert } from '@iqss/dataverse-design-system' | ||
import { DatasetAlert } from '../../../dataset/domain/models/Dataset' | ||
import { useTranslation } from 'react-i18next' | ||
import styles from './DatasetAlerts.module.scss' | ||
import parse from 'html-react-parser' | ||
import { useAlertContext } from '../../alerts/AlertContext' | ||
import { Alerts } from '../../alerts/Alerts' | ||
import { Alert } from '../../../alert/domain/models/Alert' | ||
|
||
interface DatasetAlertsProps { | ||
alerts: DatasetAlert[] | ||
alerts: Alert[] | ||
} | ||
|
||
export function DatasetAlerts({ alerts }: DatasetAlertsProps) { | ||
const { t } = useTranslation('dataset') | ||
|
||
return ( | ||
<div className={styles.container}> | ||
{alerts.map((alert: DatasetAlert, index) => { | ||
const translatedMsg = alert.dynamicFields | ||
? t(`alerts.${alert.message}.alertText`, alert.dynamicFields) | ||
: t(`alerts.${alert.message}.alertText`) | ||
const translatedHeading = t(`alerts.${alert.message}.heading`) | ||
const alertKey = `alert-${index}` | ||
return ( | ||
<Alert | ||
key={alertKey} | ||
variant={alert.variant} | ||
customHeading={translatedHeading} | ||
dismissible={false}> | ||
{parse(translatedMsg)} | ||
</Alert> | ||
) | ||
})} | ||
</div> | ||
) | ||
const { addDatasetAlert } = useAlertContext() | ||
alerts.forEach((alert) => addDatasetAlert(alert)) | ||
return <Alerts></Alerts> | ||
} |
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,10 @@ | ||
import { StoryFn } from '@storybook/react' | ||
import { AlertProvider } from '../sections/alerts/AlertProvider' | ||
|
||
export const WithAlerts = (Story: StoryFn) => { | ||
return ( | ||
<AlertProvider> | ||
<Story /> | ||
</AlertProvider> | ||
) | ||
} |
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
Oops, something went wrong.