-
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): edit labware nickname modal (#16151)
closes AUTH-737
- Loading branch information
Showing
7 changed files
with
237 additions
and
85 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
45 changes: 45 additions & 0 deletions
45
protocol-designer/src/organisms/EditNickNameModal/__tests__/EditNickNameModal.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,45 @@ | ||
import * as React from 'react' | ||
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 { EditNickNameModal } from '..' | ||
import { getLabwareNicknamesById } from '../../../ui/labware/selectors' | ||
import { renameLabware } from '../../../labware-ingred/actions' | ||
|
||
vi.mock('../../../ui/labware/selectors') | ||
vi.mock('../../../labware-ingred/actions') | ||
|
||
const render = (props: React.ComponentProps<typeof EditNickNameModal>) => { | ||
return renderWithProviders(<EditNickNameModal {...props} />, { | ||
i18nInstance: i18n, | ||
})[0] | ||
} | ||
|
||
describe('EditNickNameModal', () => { | ||
let props: React.ComponentProps<typeof EditNickNameModal> | ||
|
||
beforeEach(() => { | ||
props = { | ||
onClose: vi.fn(), | ||
labwareId: 'mockId', | ||
} | ||
vi.mocked(getLabwareNicknamesById).mockReturnValue({ | ||
mockId: 'mockOriginalName', | ||
}) | ||
}) | ||
it('renders the text and adds a nickname', () => { | ||
render(props) | ||
screen.getByText('Rename labware') | ||
screen.getByText('Labware name') | ||
|
||
fireEvent.click(screen.getByText('Cancel')) | ||
expect(props.onClose).toHaveBeenCalled() | ||
|
||
const input = screen.getByRole('textbox') | ||
fireEvent.change(input, { target: { value: 'mockNickName' } }) | ||
fireEvent.click(screen.getByText('Save')) | ||
expect(vi.mocked(renameLabware)).toHaveBeenCalled() | ||
expect(props.onClose).toHaveBeenCalled() | ||
}) | ||
}) |
82 changes: 82 additions & 0 deletions
82
protocol-designer/src/organisms/EditNickNameModal/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,82 @@ | ||
import * as React from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { useTranslation } from 'react-i18next' | ||
import { createPortal } from 'react-dom' | ||
import { | ||
COLORS, | ||
DIRECTION_COLUMN, | ||
Flex, | ||
InputField, | ||
JUSTIFY_END, | ||
Modal, | ||
PrimaryButton, | ||
SecondaryButton, | ||
SPACING, | ||
StyledText, | ||
} from '@opentrons/components' | ||
import { selectors as uiLabwareSelectors } from '../../ui/labware' | ||
import { getTopPortalEl } from '../../components/portals/TopPortal' | ||
import { renameLabware } from '../../labware-ingred/actions' | ||
import type { ThunkDispatch } from '../../types' | ||
|
||
interface EditNickNameModalProps { | ||
labwareId: string | ||
onClose: () => void | ||
} | ||
export function EditNickNameModal(props: EditNickNameModalProps): JSX.Element { | ||
const { onClose, labwareId } = props | ||
const { t } = useTranslation(['create_new_protocol', 'shared']) | ||
const dispatch = useDispatch<ThunkDispatch<any>>() | ||
const nickNames = useSelector(uiLabwareSelectors.getLabwareNicknamesById) | ||
const savedNickname = nickNames[labwareId] | ||
const [nickName, setNickName] = React.useState<string>(savedNickname) | ||
const saveNickname = (): void => { | ||
dispatch(renameLabware({ labwareId, name: nickName })) | ||
onClose() | ||
} | ||
|
||
return createPortal( | ||
<Modal | ||
title={t('rename_labware')} | ||
type="info" | ||
onClose={onClose} | ||
footer={ | ||
<Flex | ||
justifyContent={JUSTIFY_END} | ||
gridGap={SPACING.spacing8} | ||
padding={SPACING.spacing24} | ||
> | ||
<SecondaryButton | ||
onClick={() => { | ||
onClose() | ||
}} | ||
> | ||
{t('shared:cancel')} | ||
</SecondaryButton> | ||
<PrimaryButton onClick={saveNickname}> | ||
{t('shared:save')} | ||
</PrimaryButton> | ||
</Flex> | ||
} | ||
> | ||
<Flex flexDirection={DIRECTION_COLUMN} girGap={SPACING.spacing4}> | ||
<Flex color={COLORS.grey60}> | ||
<StyledText desktopStyle="bodyDefaultRegular"> | ||
{t('labware_name')} | ||
</StyledText> | ||
</Flex> | ||
<InputField | ||
data-testid="renameLabware_inputField" | ||
name="renameLabware" | ||
onChange={e => { | ||
setNickName(e.target.value) | ||
}} | ||
value={nickName} | ||
type="text" | ||
autoFocus | ||
/> | ||
</Flex> | ||
</Modal>, | ||
getTopPortalEl() | ||
) | ||
} |
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
Oops, something went wrong.