-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protocol-designer): magnetic module change hint wire up (#16498)
closes AUTH-797
- Loading branch information
Showing
9 changed files
with
481 additions
and
178 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
38 changes: 38 additions & 0 deletions
38
protocol-designer/src/organisms/BlockingHintModal/__tests__/BlockingHintModal.test.tsx
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,38 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
import { fireEvent, screen } from '@testing-library/react' | ||
import { i18n } from '../../../assets/localization' | ||
import { renderWithProviders } from '../../../__testing-utils__' | ||
import { removeHint } from '../../../tutorial/actions' | ||
import { BlockingHintModal } from '..' | ||
import type { ComponentProps } from 'react' | ||
|
||
vi.mock('../../../tutorial/actions') | ||
|
||
const render = (props: ComponentProps<typeof BlockingHintModal>) => { | ||
return renderWithProviders(<BlockingHintModal {...props} />, { | ||
i18nInstance: i18n, | ||
})[0] | ||
} | ||
|
||
describe('BlockingHintModal', () => { | ||
let props: ComponentProps<typeof BlockingHintModal> | ||
|
||
beforeEach(() => { | ||
props = { | ||
content: <div>mock content</div>, | ||
handleCancel: vi.fn(), | ||
handleContinue: vi.fn(), | ||
hintKey: 'change_magnet_module_model', | ||
} | ||
}) | ||
it('renders the hint with buttons and checkbox', () => { | ||
render(props) | ||
fireEvent.click(screen.getByRole('button', { name: 'Cancel' })) | ||
expect(props.handleCancel).toHaveBeenCalled() | ||
expect(vi.mocked(removeHint)).toHaveBeenCalled() | ||
fireEvent.click(screen.getByRole('button', { name: 'Continue' })) | ||
expect(props.handleContinue).toHaveBeenCalled() | ||
expect(vi.mocked(removeHint)).toHaveBeenCalled() | ||
screen.getByText('mock content') | ||
}) | ||
}) |
86 changes: 86 additions & 0 deletions
86
protocol-designer/src/organisms/BlockingHintModal/index.tsx
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,86 @@ | ||
import { useCallback, useState } from 'react' | ||
import { createPortal } from 'react-dom' | ||
import { useTranslation } from 'react-i18next' | ||
import { useDispatch } from 'react-redux' | ||
import { | ||
ALIGN_CENTER, | ||
COLORS, | ||
Check, | ||
Flex, | ||
JUSTIFY_SPACE_BETWEEN, | ||
Modal, | ||
PrimaryButton, | ||
SPACING, | ||
SecondaryButton, | ||
StyledText, | ||
} from '@opentrons/components' | ||
import { actions } from '../../tutorial' | ||
import { getMainPagePortalEl } from '../../components/portals/MainPageModalPortal' | ||
import type { ReactNode } from 'react' | ||
import type { HintKey } from '../../tutorial' | ||
|
||
export interface HintProps { | ||
hintKey: HintKey | ||
handleCancel: () => void | ||
handleContinue: () => void | ||
content: ReactNode | ||
} | ||
|
||
export function BlockingHintModal(props: HintProps): JSX.Element { | ||
const { content, hintKey, handleCancel, handleContinue } = props | ||
const { t, i18n } = useTranslation(['alert', 'shared']) | ||
const dispatch = useDispatch() | ||
|
||
const [rememberDismissal, setRememberDismissal] = useState<boolean>(false) | ||
|
||
const toggleRememberDismissal = useCallback(() => { | ||
setRememberDismissal(prevDismissal => !prevDismissal) | ||
}, []) | ||
|
||
const onCancelClick = (): void => { | ||
dispatch(actions.removeHint(hintKey, rememberDismissal)) | ||
handleCancel() | ||
} | ||
|
||
const onContinueClick = (): void => { | ||
dispatch(actions.removeHint(hintKey, rememberDismissal)) | ||
handleContinue() | ||
} | ||
|
||
return createPortal( | ||
<Modal | ||
type="warning" | ||
title={t(`hint.${hintKey}.title`)} | ||
onClose={onCancelClick} | ||
footer={ | ||
<Flex | ||
alignItems={ALIGN_CENTER} | ||
justifyContent={JUSTIFY_SPACE_BETWEEN} | ||
padding={SPACING.spacing24} | ||
> | ||
<Flex | ||
alignItems={ALIGN_CENTER} | ||
onClick={toggleRememberDismissal} | ||
gridGap={SPACING.spacing8} | ||
> | ||
<Check isChecked={rememberDismissal} color={COLORS.blue50} /> | ||
<StyledText desktopStyle="bodyDefaultRegular"> | ||
{t('hint.dont_show_again')} | ||
</StyledText> | ||
</Flex> | ||
<Flex alingItems={ALIGN_CENTER} gridGap={SPACING.spacing8}> | ||
<SecondaryButton onClick={onCancelClick}> | ||
{t('shared:cancel')} | ||
</SecondaryButton> | ||
<PrimaryButton onClick={onContinueClick}> | ||
{i18n.format(t('shared:continue'), 'capitalize')} | ||
</PrimaryButton> | ||
</Flex> | ||
</Flex> | ||
} | ||
> | ||
{content} | ||
</Modal>, | ||
getMainPagePortalEl() | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
protocol-designer/src/organisms/BlockingHintModal/useBlockingHint.tsx
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,40 @@ | ||
import { useSelector } from 'react-redux' | ||
import { getDismissedHints } from '../../tutorial/selectors' | ||
import { BlockingHintModal } from './index' | ||
import type { HintKey } from '../../tutorial' | ||
|
||
export interface HintProps { | ||
/** `enabled` should be a condition that the parent uses to toggle whether the hint should be active or not. | ||
* If the hint is enabled but has been dismissed, it will automatically call `handleContinue` when enabled. | ||
* useBlockingHint expects the parent to disable the hint on cancel/continue */ | ||
enabled: boolean | ||
hintKey: HintKey | ||
content: React.ReactNode | ||
handleCancel: () => void | ||
handleContinue: () => void | ||
} | ||
|
||
export const useBlockingHint = (args: HintProps): JSX.Element | null => { | ||
const { enabled, hintKey, handleCancel, handleContinue, content } = args | ||
const isDismissed = useSelector(getDismissedHints).includes(hintKey) | ||
|
||
if (isDismissed) { | ||
if (enabled) { | ||
handleContinue() | ||
} | ||
return null | ||
} | ||
|
||
if (!enabled) { | ||
return null | ||
} | ||
|
||
return ( | ||
<BlockingHintModal | ||
hintKey={hintKey} | ||
handleCancel={handleCancel} | ||
handleContinue={handleContinue} | ||
content={content} | ||
/> | ||
) | ||
} |
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.