Skip to content

Commit 17169c4

Browse files
committed
handle root level changes
1 parent 97cacf2 commit 17169c4

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

src/webviews/src/modules/key-details/components/rejson-details/rejson-details/RejsonDetails.spec.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,66 @@ describe('RejsonDetails', () => {
338338
expect(useRejson.setReJSONDataAction).not.toBeCalled()
339339
expect(useRejson.appendReJSONArrayItemAction).toBeCalled()
340340
})
341+
342+
it('should show confirmation dialog when adding a key that already exists', () => {
343+
render(
344+
<RejsonDetails
345+
{...instance(mockedProps)}
346+
data={{ existingKey: '123' }}
347+
dataType="object"
348+
selectedKey={mockedSelectedKey}
349+
isDownloaded
350+
/>,
351+
)
352+
353+
fireEvent.click(screen.getByTestId('add-object-btn'))
354+
355+
fireEvent.change(screen.getByTestId('json-key'), {
356+
target: { value: '"existingKey"' },
357+
})
358+
359+
fireEvent.change(screen.getByTestId('json-value'), {
360+
target: { value: '"newValue"' },
361+
})
362+
363+
fireEvent.click(screen.getByTestId('apply-btn'))
364+
365+
expect(screen.getByText('Duplicate JSON key detected')).toBeInTheDocument()
366+
expect(useRejson.setReJSONDataAction).not.toBeCalled()
367+
})
368+
369+
it('should call setReJSONDataAction when user confirms overwrite', () => {
370+
render(
371+
<RejsonDetails
372+
{...instance(mockedProps)}
373+
data={{ existingKey: '123' }}
374+
dataType="object"
375+
selectedKey={mockedSelectedKey}
376+
isDownloaded
377+
/>,
378+
)
379+
380+
fireEvent.click(screen.getByTestId('add-object-btn'))
381+
382+
fireEvent.change(screen.getByTestId('json-key'), {
383+
target: { value: '"existingKey"' },
384+
})
385+
386+
fireEvent.change(screen.getByTestId('json-value'), {
387+
target: { value: '"newValue"' },
388+
})
389+
390+
fireEvent.click(screen.getByTestId('apply-btn'))
391+
392+
const confirmBtn = screen.getByTestId('confirm-btn')
393+
fireEvent.click(confirmBtn)
394+
395+
expect(useRejson.setReJSONDataAction).toBeCalledWith(
396+
mockedSelectedKey,
397+
'["existingKey"]',
398+
'"newValue"',
399+
undefined, // length is not required
400+
expect.any(Function), // callback is not required
401+
)
402+
})
341403
})

src/webviews/src/modules/key-details/components/rejson-details/rejson-details/RejsonDetails.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
setReJSONDataAction,
1717
} from '../hooks/useRejsonStore'
1818

19+
import { checkExistingPath } from '../rejson-object/tbd'
20+
import ReJSONConfirmDialog from '../rejson-object/RejsonConfirmDialog'
1921
import styles from '../styles.module.scss'
2022

2123
export const RejsonDetails = (props: BaseProps) => {
@@ -31,6 +33,8 @@ export const RejsonDetails = (props: BaseProps) => {
3133
} = props
3234

3335
const [addRootKVPair, setAddRootKVPair] = useState<boolean>(false)
36+
const [isConfirmVisible, setIsConfirmVisible] = useState<boolean>(false)
37+
const [confirmDialogAction, setConfirmDialogAction] = useState<any>(() => {})
3438

3539
const databaseId = useKeysInContext((state) => state.databaseId)
3640

@@ -61,14 +65,38 @@ export const RejsonDetails = (props: BaseProps) => {
6165
})
6266
}
6367

64-
const handleFormSubmit = ({ key, value }: { key?: string, value: string }) => {
68+
const handleFormSubmit = ({
69+
key,
70+
value,
71+
}: {
72+
key?: string
73+
value: string
74+
}) => {
75+
const updatedPath = wrapPath(key as string)
76+
if (key) {
77+
const isExisting = checkExistingPath(updatedPath as string, data)
78+
79+
if (isExisting) {
80+
setConfirmDialogAction(() => () => {
81+
setIsConfirmVisible(false)
82+
83+
if (updatedPath) {
84+
handleSetRejsonDataAction(selectedKey, updatedPath, value)
85+
}
86+
setAddRootKVPair(false)
87+
})
88+
89+
setIsConfirmVisible(true)
90+
return
91+
}
92+
}
93+
6594
setAddRootKVPair(false)
6695
if (isRealArray(data, dataType)) {
6796
handleAppendRejsonArrayItemAction(selectedKey, '.', value)
6897
return
6998
}
7099

71-
const updatedPath = wrapPath(key as string)
72100
if (updatedPath) {
73101
handleSetRejsonDataAction(selectedKey, updatedPath, value)
74102
}
@@ -101,6 +129,11 @@ export const RejsonDetails = (props: BaseProps) => {
101129
<span>{getBrackets(isObject ? ObjectTypes.Object : ObjectTypes.Array, 'start')}</span>
102130
</div>
103131
)}
132+
<ReJSONConfirmDialog
133+
open={isConfirmVisible}
134+
onClose={() => setIsConfirmVisible(false)}
135+
onConfirm={confirmDialogAction}
136+
/>
104137
<RejsonDynamicTypes
105138
data={data}
106139
parentPath={parentPath}

0 commit comments

Comments
 (0)