2022-08-02 11:29:40 +02:00
|
|
|
import React, { useCallback, useReducer } from 'react';
|
2022-02-15 01:02:51 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2022-08-02 11:29:40 +02:00
|
|
|
import { produce } from 'immer';
|
|
|
|
import classnames from 'classnames';
|
2022-02-15 01:02:51 +01:00
|
|
|
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';
|
|
|
|
|
2022-08-02 11:29:40 +02:00
|
|
|
/**
|
|
|
|
* 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 }) {
|
2022-02-15 01:02:51 +01:00
|
|
|
const t = useI18nContext();
|
2022-08-02 11:29:40 +02:00
|
|
|
const [checkboxState, dispatch] = useReducer(checkboxStateReducer, {});
|
2022-02-15 01:02:51 +01:00
|
|
|
|
2022-08-02 11:29:40 +02:00
|
|
|
const isAllChecked = warnings.every((warning) => checkboxState[warning.id]);
|
|
|
|
|
|
|
|
const onCheckboxClicked = useCallback((checkboxId) => {
|
|
|
|
dispatch({ type: 'check', checkboxId });
|
|
|
|
}, []);
|
2022-02-15 01:02:51 +01:00
|
|
|
|
|
|
|
const SnapInstallWarningFooter = () => {
|
|
|
|
return (
|
|
|
|
<div className="snap-install-warning__footer">
|
|
|
|
<Button
|
|
|
|
className="snap-install-warning__footer-button"
|
|
|
|
type="default"
|
|
|
|
onClick={onCancel}
|
|
|
|
>
|
|
|
|
{t('cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
className="snap-install-warning__footer-button"
|
|
|
|
type="primary"
|
2022-08-02 11:29:40 +02:00
|
|
|
disabled={!isAllChecked}
|
2022-02-15 01:02:51 +01:00
|
|
|
onClick={onSubmit}
|
|
|
|
>
|
|
|
|
{t('confirm')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
className="snap-install-warning"
|
|
|
|
title={t('areYouSure')}
|
|
|
|
footer={<SnapInstallWarningFooter />}
|
2022-03-07 16:32:09 +01:00
|
|
|
headerProps={{ padding: [6, 6, 0] }}
|
|
|
|
contentProps={{ padding: [0, 6, 4] }}
|
|
|
|
footerProps={{ padding: [4, 6] }}
|
2022-02-15 01:02:51 +01:00
|
|
|
>
|
2022-03-07 16:32:09 +01:00
|
|
|
<Typography variant={TYPOGRAPHY.H6} boxProps={{ paddingBottom: 4 }}>
|
2022-08-02 11:29:40 +02:00
|
|
|
{warnings.length > 1
|
|
|
|
? t('snapInstallWarningCheckPlural')
|
|
|
|
: t('snapInstallWarningCheck')}
|
2022-03-07 16:32:09 +01:00
|
|
|
</Typography>
|
2022-08-02 11:29:40 +02:00
|
|
|
{warnings.map((warning, i) => (
|
|
|
|
<div
|
|
|
|
className={classnames('checkbox-label', {
|
|
|
|
'checkbox-label--first': i === 0,
|
|
|
|
})}
|
|
|
|
key={warning.id}
|
|
|
|
>
|
|
|
|
<CheckBox
|
|
|
|
checked={checkboxState[warning.id] ?? false}
|
|
|
|
id={warning.id}
|
|
|
|
onClick={() => onCheckboxClicked(warning.id)}
|
|
|
|
/>
|
|
|
|
<label htmlFor={warning.id}>{warning.message}</label>
|
|
|
|
</div>
|
|
|
|
))}
|
2022-02-15 01:02:51 +01:00
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SnapInstallWarning.propTypes = {
|
|
|
|
/**
|
|
|
|
* onCancel handler
|
|
|
|
*/
|
|
|
|
onCancel: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* onSubmit handler
|
|
|
|
*/
|
|
|
|
onSubmit: PropTypes.func,
|
|
|
|
/**
|
2022-08-02 11:29:40 +02:00
|
|
|
* warnings list
|
2022-02-15 01:02:51 +01:00
|
|
|
*/
|
2022-08-02 11:29:40 +02:00
|
|
|
warnings: PropTypes.arrayOf({
|
|
|
|
message: PropTypes.node,
|
|
|
|
id: PropTypes.string,
|
|
|
|
}),
|
2022-02-15 01:02:51 +01:00
|
|
|
};
|