-
Notifications
You must be signed in to change notification settings - Fork 472
RI-8222 Add "Add element" to the array View tab #6129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pawelangelow
wants to merge
3
commits into
be/RI-8222/add-array-append-endpoint
Choose a base branch
from
fe/RI-8222/add-array-element
base: be/RI-8222/add-array-append-endpoint
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
...ser/modules/key-details/components/array-details/array-add-form/ArrayAddForm.constants.ts
This file contains hidden or 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,17 @@ | ||
| export const ARRAY_ADD_FORM_TEST_ID = 'array-add-form' | ||
|
|
||
| export const VALUE_LABEL = 'Value' | ||
| export const INDEX_LABEL = 'Index' | ||
| export const INDEX_PLACEHOLDER = 'Leave empty to append to the end' | ||
| export const INDEX_HINT = | ||
| 'Leave empty to append the value to the end of the array. Enter an index to ' + | ||
| 'set the value at that exact position (overwriting any existing value there).' | ||
| export const INVALID_INDEX_MESSAGE = | ||
| 'Index must be an integer string between 0 and 18446744073709551614' | ||
| export const ADD_BUTTON_LABEL = 'Add' | ||
| export const CANCEL_BUTTON_LABEL = 'Cancel' | ||
|
|
||
| export const CONFIRM_TITLE = 'Add element to a production database?' | ||
| export const CONFIRM_DESCRIPTION = | ||
| 'You are about to add an element to a key on a production database.' | ||
| export const CONFIRM_BUTTON_TEXT = 'Add' |
104 changes: 104 additions & 0 deletions
104
...browser/modules/key-details/components/array-details/array-add-form/ArrayAddForm.spec.tsx
This file contains hidden or 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,104 @@ | ||
| import React from 'react' | ||
| import { | ||
| act, | ||
| fireEvent, | ||
| initialStateDefault, | ||
| mockStore, | ||
| render, | ||
| screen, | ||
| waitFor, | ||
| } from 'uiSrc/utils/test-utils' | ||
| import { apiService } from 'uiSrc/services' | ||
| import { stringToBuffer } from 'uiSrc/utils' | ||
|
|
||
| import { ArrayAddForm } from './ArrayAddForm' | ||
|
|
||
| const keyProp = stringToBuffer('mykey') | ||
|
|
||
| // The form's key must be the live selected key, otherwise applyArrayWriteResult | ||
| // suppresses the success side effects (onSuccess/closePanel) as a stale write. | ||
| const stateWithKeySelected = { | ||
| ...initialStateDefault, | ||
| app: { | ||
| ...initialStateDefault.app, | ||
| context: { | ||
| ...initialStateDefault.app.context, | ||
| browser: { | ||
| ...initialStateDefault.app.context.browser, | ||
| keyList: { | ||
| ...initialStateDefault.app.context.browser.keyList, | ||
| selectedKey: keyProp, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const renderForm = (closePanel = jest.fn()) => | ||
| render(<ArrayAddForm keyProp={keyProp} closePanel={closePanel} />, { | ||
| store: mockStore(stateWithKeySelected), | ||
| }) | ||
|
|
||
| const findCall = (fragment: string) => | ||
| (apiService.post as jest.Mock).mock.calls.find(([url]) => | ||
| (url as string).includes(fragment), | ||
| ) | ||
|
|
||
| describe('ArrayAddForm', () => { | ||
| beforeEach(() => { | ||
| apiService.post = jest.fn().mockResolvedValue({ status: 200, data: {} }) | ||
| }) | ||
|
|
||
| it('renders the value and index inputs', () => { | ||
| renderForm() | ||
| expect(screen.getByTestId('array-add-form-value')).toBeInTheDocument() | ||
| expect(screen.getByTestId('array-add-form-index')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('appends (POST /array/append) when the index is left empty', async () => { | ||
| const closePanel = jest.fn() | ||
| renderForm(closePanel) | ||
|
|
||
| fireEvent.change(screen.getByTestId('array-add-form-value'), { | ||
| target: { value: 'hello' }, | ||
| }) | ||
| fireEvent.click(screen.getByTestId('array-add-form-submit')) | ||
|
|
||
| await waitFor(() => { | ||
| expect(findCall('array/append')).toBeTruthy() | ||
| }) | ||
| expect(findCall('array/set-element')).toBeFalsy() | ||
| expect(closePanel).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('sets at index (POST /array/set-element) when an index is provided', async () => { | ||
| renderForm() | ||
|
|
||
| fireEvent.change(screen.getByTestId('array-add-form-value'), { | ||
| target: { value: 'hello' }, | ||
| }) | ||
| fireEvent.change(screen.getByTestId('array-add-form-index'), { | ||
| target: { value: '5' }, | ||
| }) | ||
| fireEvent.click(screen.getByTestId('array-add-form-submit')) | ||
|
|
||
| await waitFor(() => { | ||
| const call = findCall('array/set-element') | ||
| expect(call).toBeTruthy() | ||
| expect((call?.[1] as { index: string }).index).toBe('5') | ||
| }) | ||
| expect(findCall('array/append')).toBeFalsy() | ||
| }) | ||
|
|
||
| it('disables Add for a non-canonical index', () => { | ||
| renderForm() | ||
|
|
||
| act(() => { | ||
| fireEvent.change(screen.getByTestId('array-add-form-index'), { | ||
| target: { value: '007' }, | ||
| }) | ||
| }) | ||
|
|
||
| expect(screen.getByTestId('array-add-form-submit')).toBeDisabled() | ||
| }) | ||
| }) | ||
143 changes: 143 additions & 0 deletions
143
...ages/browser/modules/key-details/components/array-details/array-add-form/ArrayAddForm.tsx
This file contains hidden or 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,143 @@ | ||
| import React, { useState } from 'react' | ||
|
|
||
| import { useAppDispatch, useAppSelector } from 'uiSrc/slices/hooks' | ||
| import { selectedKeySelector } from 'uiSrc/slices/browser/keys' | ||
| import { appendArrayElement, addArrayElement } from 'uiSrc/slices/browser/array' | ||
| import { | ||
| BrowserConfirmationCommandId, | ||
| useProductionWriteConfirmation, | ||
| } from 'uiSrc/components/production-write-confirmation' | ||
| import { stringToSerializedBufferFormat } from 'uiSrc/utils' | ||
| import { parseArrayIndex } from 'uiSrc/utils/arrayIndex' | ||
| import { FormField } from 'uiSrc/components/base/forms/FormField' | ||
| import { TextInput } from 'uiSrc/components/base/inputs' | ||
| import { Col, FlexItem, Row } from 'uiSrc/components/base/layout/flex' | ||
| import { | ||
| PrimaryButton, | ||
| SecondaryButton, | ||
| } from 'uiSrc/components/base/forms/buttons' | ||
|
|
||
| import { EntryContent } from '../../common/AddKeysContainer.styled' | ||
| import { | ||
| ARRAY_ADD_FORM_TEST_ID as TEST_ID, | ||
| ADD_BUTTON_LABEL, | ||
| CANCEL_BUTTON_LABEL, | ||
| CONFIRM_BUTTON_TEXT, | ||
| CONFIRM_DESCRIPTION, | ||
| CONFIRM_TITLE, | ||
| INDEX_HINT, | ||
| INDEX_LABEL, | ||
| INDEX_PLACEHOLDER, | ||
| INVALID_INDEX_MESSAGE, | ||
| VALUE_LABEL, | ||
| } from './ArrayAddForm.constants' | ||
| import { ArrayAddFormProps } from './ArrayAddForm.types' | ||
|
|
||
| /** | ||
| * Content of the "Add element" slide-out panel (rendered inside the shared | ||
| * `AddKeysContainer`, matching List / Vector Set). The index is optional: | ||
| * leaving it empty appends to the end (POST /array/append, atomic ARSET at the | ||
| * current length); providing one sets at that index (POST /array/set-element). | ||
| * `ARINSERT` is intentionally not used — see docs/array-modify-vertical-plan.md. | ||
| */ | ||
| export const ArrayAddForm = ({ keyProp, closePanel }: ArrayAddFormProps) => { | ||
| const dispatch = useAppDispatch() | ||
| const { viewFormat } = useAppSelector(selectedKeySelector) | ||
| const { requestConfirmation } = useProductionWriteConfirmation() | ||
|
|
||
| const [value, setValue] = useState('') | ||
| const [index, setIndex] = useState('') | ||
|
|
||
| // The index input is optional (empty → append). When provided it must be a | ||
| // canonical decimal string, matching the backend @IsArrayIndex validator. | ||
| const trimmedIndex = index.trim() | ||
| const indexInvalid = | ||
| trimmedIndex.length > 0 && parseArrayIndex(trimmedIndex) !== trimmedIndex | ||
|
|
||
| const handleSuccess = () => { | ||
| setValue('') | ||
| setIndex('') | ||
| closePanel() | ||
| } | ||
|
pawelangelow marked this conversation as resolved.
|
||
|
|
||
| const handleAdd = () => { | ||
| requestConfirmation({ | ||
| title: CONFIRM_TITLE, | ||
| actionDescription: CONFIRM_DESCRIPTION, | ||
| confirmButtonText: CONFIRM_BUTTON_TEXT, | ||
| commandId: BrowserConfirmationCommandId.AddArrayElements, | ||
| disableConfirmationInput: true, | ||
| onConfirm: () => { | ||
| const serialized = stringToSerializedBufferFormat(viewFormat, value) | ||
| if (trimmedIndex.length === 0) { | ||
| dispatch( | ||
| appendArrayElement( | ||
| { key: keyProp, value: serialized }, | ||
| handleSuccess, | ||
| ), | ||
| ) | ||
| } else { | ||
| dispatch( | ||
| addArrayElement( | ||
| { key: keyProp, index: trimmedIndex, value: serialized }, | ||
| handleSuccess, | ||
| ), | ||
| ) | ||
| } | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return ( | ||
| <Col gap="m"> | ||
| <EntryContent gap="m" data-testid={TEST_ID}> | ||
| <Row align="end" gap="m"> | ||
| <FlexItem grow> | ||
| <FormField label={VALUE_LABEL}> | ||
| <TextInput | ||
| value={value} | ||
| onChange={setValue} | ||
| placeholder="Enter value" | ||
| data-testid={`${TEST_ID}-value`} | ||
| /> | ||
| </FormField> | ||
| </FlexItem> | ||
| <FlexItem> | ||
| <FormField | ||
| label={INDEX_LABEL} | ||
| infoIconProps={{ content: INDEX_HINT }} | ||
| > | ||
| <TextInput | ||
| value={index} | ||
| onChange={setIndex} | ||
| placeholder={INDEX_PLACEHOLDER} | ||
| error={indexInvalid ? INVALID_INDEX_MESSAGE : undefined} | ||
| data-testid={`${TEST_ID}-index`} | ||
| /> | ||
| </FormField> | ||
| </FlexItem> | ||
| </Row> | ||
| </EntryContent> | ||
|
|
||
| <Row justify="end" gap="m" grow={false}> | ||
| <FlexItem grow={false}> | ||
| <SecondaryButton | ||
| onClick={() => closePanel(true)} | ||
| data-testid={`${TEST_ID}-cancel`} | ||
| > | ||
| {CANCEL_BUTTON_LABEL} | ||
| </SecondaryButton> | ||
| </FlexItem> | ||
| <FlexItem grow={false}> | ||
| <PrimaryButton | ||
| onClick={handleAdd} | ||
| disabled={indexInvalid} | ||
| data-testid={`${TEST_ID}-submit`} | ||
| > | ||
| {ADD_BUTTON_LABEL} | ||
| </PrimaryButton> | ||
| </FlexItem> | ||
| </Row> | ||
| </Col> | ||
| ) | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
...browser/modules/key-details/components/array-details/array-add-form/ArrayAddForm.types.ts
This file contains hidden or 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,9 @@ | ||
| import { RedisResponseBuffer } from 'uiSrc/slices/interfaces' | ||
|
|
||
| export interface ArrayAddFormProps { | ||
| /** The array key being viewed; the element is added to it. */ | ||
| keyProp: RedisResponseBuffer | ||
| /** Closes the add panel. `isCancelled` distinguishes an explicit Cancel from | ||
| * a close-after-success (mirrors the other types' add panels). */ | ||
| closePanel: (isCancelled?: boolean) => void | ||
| } |
1 change: 1 addition & 0 deletions
1
...ui/src/pages/browser/modules/key-details/components/array-details/array-add-form/index.ts
This file contains hidden or 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 @@ | ||
| export { ArrayAddForm } from './ArrayAddForm' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.