import React, { useCallback, useReducer } from 'react'; import PropTypes from 'prop-types'; import { produce } from 'immer'; import classnames from 'classnames'; import { useI18nContext } from '../../../../hooks/useI18nContext'; import CheckBox from '../../../ui/check-box/check-box.component'; import Typography from '../../../ui/typography/typography'; import { TYPOGRAPHY } from '../../../../helpers/constants/design-system'; import Popover from '../../../ui/popover'; import Button from '../../../ui/button'; /** * a very simple reducer using produce from Immer to keep checkboxes state manipulation * immutable and painless. */ const checkboxStateReducer = produce((state, action) => { switch (action.type) { case 'check': state[action.checkboxId] = state[action.checkboxId] ? !state[action.checkboxId] : true; break; default: throw new Error( 'You must provide a type when dispatching an action for checkboxState', ); } }); export default function SnapInstallWarning({ onCancel, onSubmit, warnings }) { const t = useI18nContext(); const [checkboxState, dispatch] = useReducer(checkboxStateReducer, {}); const isAllChecked = warnings.every((warning) => checkboxState[warning.id]); const onCheckboxClicked = useCallback((checkboxId) => { dispatch({ type: 'check', checkboxId }); }, []); const SnapInstallWarningFooter = () => { return (
); }; return ( } headerProps={{ padding: [6, 6, 0] }} contentProps={{ padding: [0, 6, 4] }} footerProps={{ padding: [4, 6] }} > {warnings.length > 1 ? t('snapInstallWarningCheckPlural') : t('snapInstallWarningCheck')} {warnings.map((warning, i) => (
onCheckboxClicked(warning.id)} />
))}
); } SnapInstallWarning.propTypes = { /** * onCancel handler */ onCancel: PropTypes.func, /** * onSubmit handler */ onSubmit: PropTypes.func, /** * warnings list */ warnings: PropTypes.arrayOf({ message: PropTypes.node, id: PropTypes.string, }), };