Skip to content
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

fix: validation for text key #14669

Merged
merged 6 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions frontend/packages/text-editor/src/TextList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export const TextList = ({
const { t } = useTranslation();
const { data: layoutNames, isPending: layoutNamesPending } = useLayoutNamesQuery(org, app);

const textIds = useMemo(() => resourceRows.map((row) => row.textKey), [resourceRows]);
const idExists = (newTextId: string): boolean =>
textIds.some((textId) => StringUtils.areCaseInsensitiveEqual(textId, newTextId));
const getTableHeaderCellId = (language: string): string => `header-lang${language}`;
const textIds = useMemo(() => resourceRows.map((row) => row.textKey), [resourceRows]);
const idExists = (newTextId: string, oldTextId: string): boolean =>
textIds
.filter((textId: string) => textId.toLowerCase() !== oldTextId.toLowerCase())
.some((textId: string) => StringUtils.areCaseInsensitiveEqual(textId, newTextId));

return (
<Table className={classes.textListTable}>
Expand Down
18 changes: 18 additions & 0 deletions frontend/packages/text-editor/src/TextRow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ describe('TextRow', () => {
expect(screen.getByText(illegalCharMsg)).toBeInTheDocument();
});

it('should not stop a call to upsertEntry to update key when new text key is equal the original but with some uppercase', async () => {
const user = userEvent.setup();
const updateEntryId = jest.fn();
renderTextRow({ updateEntryId });
const toggleKeyEditButton = screen.getByRole('button', {
name: textMock('text_editor.toggle_edit_mode', { textKey }),
});
await user.click(toggleKeyEditButton);

const idInput = screen.getByRole('textbox', {
name: textMock('text_editor.key.edit', { textKey }),
});

await user.type(idInput, 'Value1');
await user.keyboard('{TAB}');
expect(updateEntryId).toHaveBeenCalledTimes(1);
});

test('that the full row of languages is shown even if a translation is missing', async () => {
renderTextRow({
textRowEntries: [
Expand Down
17 changes: 5 additions & 12 deletions frontend/packages/text-editor/src/TextRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AltinnConfirmDialog } from 'app-shared/components';
import { StudioButton } from '@studio/components';

export interface TextRowProps {
idExists: (textResourceId: string) => boolean;
idExists: (newTextId: string, oldTextId: string) => boolean;
removeEntry: ({ textId }) => void;
textId: string;
textRowEntries: TextTableRowEntry[];
Expand Down Expand Up @@ -50,17 +50,10 @@ export const TextRow = ({
setTextIdValue(newTextId);
};

const validateNewTextId = (newTextId: string): string | null => {
if (newTextId === textId) {
return null;
}

if (idExists(newTextId)) {
return t('text_editor.key.error_duplicate');
}
const textIdValidationResult = validateTextId(newTextId);
return textIdValidationResult ? t(textIdValidationResult) : null;
};
const validateNewTextId = (newTextId: string): string | undefined =>
idExists(newTextId, textId)
? t('text_editor.key.error_duplicate')
: validateTextId(newTextId) && t(validateTextId(newTextId));

const handleTextIdBlur = () => {
updateEntryId({ oldId: textId, newId: textIdValue });
Expand Down