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: Add text resources to create code list dialog #14646

Merged
merged 5 commits into from
Feb 13, 2025

Conversation

TomasEng
Copy link
Contributor

@TomasEng TomasEng commented Feb 12, 2025

Description

This pull request adds text resources to the create code list dialog in the code list library. This includes moving some of the conversion functions up to the CodeListPage level.

I have added the skip-manual-testing label since the this functionality is not yet in use.

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Summary by CodeRabbit

  • New Features

    • Enhanced text input components to allow flexible and robust handling of text updates.
    • Improved the create and edit dialogs in Code List views for smoother text resource interactions.
    • Introduced new utility functions for managing text resources effectively.
  • Refactor

    • Streamlined text resource management across key components for more consistent performance and stability.
  • Tests

    • Expanded test coverage to verify reliable display and update of text resources during creation and editing workflows.

Copy link
Contributor

coderabbitai bot commented Feb 12, 2025

📝 Walkthrough

Walkthrough

The update refactors text resource management across several studio components. The onChangeTextResource callback is converted from a required to an optional property, with corresponding updates in its invocation. Components such as StudioTextResourceInput, CodeListPage, CodeLists, EditCodeList, and related dialogs now consistently manage text resources with improved type signatures. New utility functions and test cases were introduced while legacy utility files and tests were removed. The export list in the content library index was also updated to include new text resource types.

Changes

File(s) Change Summary
frontend/libs/.../StudioTextResourceInput.tsx Changed onChangeTextResource from required to optional and updated its invocation using optional chaining.
frontend/libs/.../CodeListPage/CodeListPage.tsx, .../CodeListPage/CodeListPage.test.tsx, .../CodeListPage/utils/utils.ts, .../CodeListPage/utils/utils.test.ts Introduced handleChangeTextResource with memoization; added utility functions (getTextResourcesForLanguage, createTextResourceWithLanguage) and new test cases to simulate dialog interactions and manage text resources.
frontend/libs/.../CodeListPage/CodeLists/CodeLists.tsx, .../EditCodeList/EditCodeList.tsx, .../EditCodeList/utils.ts, .../EditCodeList/utils.test.ts Updated prop types by replacing onUpdateTextResource with optional onChangeTextResource and revising textResources type; removed old utility functions and their tests.
frontend/libs/.../CodeListPage/CodeListsActionsBar/CodeListsActionsBar.tsx, .../CreateNewCodeListModal/CreateNewCodeListModal.tsx Integrated new optional props (onChangeTextResource, textResources) into the action bar and modal components for enhanced text resource handling.
frontend/libs/studio-content-library/src/index.ts Added new type exports: TextResource, TextResourceWithLanguage, and TextResources.

Possibly related PRs

  • feat: StudioCodeListEditor - display Numberfield or Checkbox based on value type #14398: The changes in the main PR regarding the optional onChangeTextResource property and its usage in the StudioTextResourceInput component are related to the modifications in the retrieved PR, which also introduces an onChangeTextResource prop in various components, indicating a shared focus on handling text resource changes.
  • fix: Text resource unsetting issues #14568: The changes in the main PR regarding the optional onChangeTextResource property and its handling in the StudioTextResourceInput component are related to the modifications in the retrieved PR, which also addresses the handling of text resources in the StudioTextResourcePicker component, specifically focusing on user interactions and state management.
  • fix: Support undefined text resource in text resource selector #14495: The changes in the main PR are related to the modifications of the onChangeTextResource property and its handling, which aligns with the updates in the retrieved PR that also involve handling optional text resources and modifying related function signatures.

Suggested labels

quality/testing

Suggested reviewers

  • github-actions
  • ErlingHauan

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added solution/studio/designer Issues related to the Altinn Studio Designer solution. frontend labels Feb 12, 2025
…-create-code-list-dialog

# Conflicts:
#	frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx
#	frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/CodeLists.tsx
#	frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/utils/utils.test.ts
@TomasEng TomasEng force-pushed the add-text-resources-to-create-code-list-dialog branch from 4871a9e to 9fd63fd Compare February 12, 2025 14:06
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.75%. Comparing base (2badc34) to head (6ecebe1).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14646      +/-   ##
==========================================
- Coverage   95.75%   95.75%   -0.01%     
==========================================
  Files        1913     1912       -1     
  Lines       24925    24924       -1     
  Branches     2848     2848              
==========================================
- Hits        23868    23867       -1     
  Misses        799      799              
  Partials      258      258              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@TomasEng TomasEng marked this pull request as ready for review February 12, 2025 14:51
@TomasEng TomasEng added skip-releasenotes Issues that do not make sense to list in our release notes team/studio-domain1 skip-manual-testing PRs that do not need to be tested manually skip-documentation Issues where updating documentation is not relevant labels Feb 12, 2025
@TomasEng TomasEng added the area/content-library Area: Related to library for shared resources label Feb 12, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/utils/utils.ts (1)

40-48: Add JSDoc documentation for the new utility functions.

While the functions are well-implemented, adding JSDoc documentation would improve maintainability by clearly describing:

  • Purpose and usage of each function
  • Parameter types and their significance
  • Return value types and possible undefined cases

Example documentation:

+/**
+ * Retrieves text resources for a specific language
+ * @param language - The language code to filter text resources
+ * @param textResources - Optional object containing text resources by language
+ * @returns Array of text resources for the specified language or undefined if not found
+ */
 export const getTextResourcesForLanguage = (
   language: string,
   textResources?: TextResources,
 ): TextResource[] | undefined => textResources?.[language];

+/**
+ * Creates a TextResourceWithLanguage object by combining a language code with a text resource
+ * @param language - The language code to associate with the text resource
+ * @param textResource - The text resource to be wrapped
+ * @returns Combined object containing both language and text resource
+ */
 export const createTextResourceWithLanguage = (
   language: string,
   textResource: TextResource,
 ): TextResourceWithLanguage => ({ language, textResource });
frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx (1)

67-67: Consider extracting the hardcoded language value.

The language code 'nb' is hardcoded in two places. Consider extracting it to a named constant at the top of the file until the referenced issue #14572 is implemented.

+const DEFAULT_LANGUAGE = 'nb';
+
 export function CodeListPage({
   // ...
 }) {
   // ...
   const handleChangeTextResource = useCallback(
     (textResource: TextResource) => {
-      const updatedTextResource = createTextResourceWithLanguage('nb', textResource);
+      const updatedTextResource = createTextResourceWithLanguage(DEFAULT_LANGUAGE, textResource);
       onUpdateTextResource?.(updatedTextResource);
     },
     [onUpdateTextResource],
   );
   // ...
 }

-const language: string = 'nb'; // Todo: Let the user choose the language: https://github.com/Altinn/altinn-studio/issues/14572
+const language: string = DEFAULT_LANGUAGE; // Todo: Let the user choose the language: https://github.com/Altinn/altinn-studio/issues/14572

Also applies to: 112-112

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/utils/utils.test.ts (1)

167-174: Consider adding edge case tests.

While the basic functionality is tested, consider adding tests for edge cases such as empty strings or special characters in the language parameter.

 describe('createTextResourceWithLanguage', () => {
   it('Creates a TextResourceWithLanguage object from the parameters', () => {
     const language = 'nb';
     const textResource = label1ResourceNb;
     const expectedResult: TextResourceWithLanguage = { language, textResource };
     expect(createTextResourceWithLanguage(language, textResource)).toEqual(expectedResult);
   });
+  it('Handles empty language string', () => {
+    const language = '';
+    const textResource = label1ResourceNb;
+    const expectedResult: TextResourceWithLanguage = { language, textResource };
+    expect(createTextResourceWithLanguage(language, textResource)).toEqual(expectedResult);
+  });
+  it('Handles special characters in language', () => {
+    const language = 'zh-CN';
+    const textResource = label1ResourceNb;
+    const expectedResult: TextResourceWithLanguage = { language, textResource };
+    expect(createTextResourceWithLanguage(language, textResource)).toEqual(expectedResult);
+  });
 });
frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.test.tsx (1)

259-291: Consider consolidating dialog interaction utilities.

The utility functions are well-organized but could benefit from being grouped into a test helper class/object for better maintainability.

+class DialogTestHelper {
+  static async openCreateDialog(user: UserEvent): Promise<HTMLElement> {
+    const createButtonLabel = textMock('app_content_library.code_lists.create_new_code_list');
+    await user.click(screen.getByRole('button', { name: createButtonLabel }));
+    return screen.getByRole('dialog');
+  }
+
+  static async addCodeListItem(user: UserEvent, area: HTMLElement): Promise<void> {
+    const addButtonLabel = textMock('code_list_editor.add_option');
+    await user.click(within(area).getByRole('button', { name: addButtonLabel }));
+  }
+  // ... other methods
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a5839e1 and 9fd63fd.

📒 Files selected for processing (12)
  • frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx (2 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.test.tsx (4 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx (3 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/CodeLists.tsx (1 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/EditCodeList/EditCodeList.tsx (3 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/EditCodeList/utils.test.ts (0 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/EditCodeList/utils.ts (0 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListsActionsBar/CodeListsActionsBar.tsx (3 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListsActionsBar/CreateNewCodeListModal/CreateNewCodeListModal.tsx (5 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/utils/utils.test.ts (3 hunks)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/utils/utils.ts (2 hunks)
  • frontend/libs/studio-content-library/src/index.ts (1 hunks)
💤 Files with no reviewable changes (2)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/EditCodeList/utils.ts
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/EditCodeList/utils.test.ts
🔇 Additional comments (10)
frontend/libs/studio-content-library/src/index.ts (1)

8-10: LGTM! Clean and focused type exports.

The new type exports are well-organized and align with the PR's objective to enhance text resource management.

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListsActionsBar/CodeListsActionsBar.tsx (1)

13-20: LGTM! Well-structured prop additions and component integration.

The new optional props are properly typed and consistently passed to child components, maintaining good component composition.

Also applies to: 57-62

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx (1)

60-63: LGTM! Well-implemented memoization and callback handling.

The textResourcesForLanguage and handleChangeTextResource are properly memoized with correct dependencies, preventing unnecessary re-renders.

Also applies to: 65-71

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/CodeLists.tsx (1)

10-10: LGTM! Props type updated for consistent text resource handling.

The changes to CodeListsProps align with the standardized approach to text resource management across components.

Also applies to: 14-15, 21-21

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListsActionsBar/CreateNewCodeListModal/CreateNewCodeListModal.tsx (1)

8-8: LGTM! Text resource handling properly implemented.

The changes correctly implement text resource management in the create dialog, with proper prop drilling through the component hierarchy.

Also applies to: 18-18, 21-21, 25-26, 28-29, 53-54, 56-57, 66-67, 69-70, 75-76, 78-79, 138-139, 141-142

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/utils/utils.test.ts (1)

2-15: LGTM! Comprehensive test coverage for text resource utilities.

The test cases thoroughly cover the functionality of getTextResourcesForLanguage, including edge cases and error conditions.

Also applies to: 153-165

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeLists/EditCodeList/EditCodeList.tsx (1)

1-1: LGTM! Consistent text resource handling implementation.

The changes align with the standardized approach to text resource management across components, maintaining a clean and consistent implementation.

Also applies to: 23-23, 29-29, 35-35, 41-41, 70-72

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx (1)

65-65: LGTM!

The optional chaining operator is correctly implemented to safely handle the optional callback.

frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.test.tsx (2)

187-220: LGTM! Comprehensive test coverage for text resource handling.

The new test cases thoroughly verify:

  • Text resource rendering in the create dialog
  • Text resource updates with correct language handling

240-243: LGTM! Clean parameter update.

The function modification improves reusability by accepting a generic area parameter instead of a specific accordion.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx (2)

60-71: Optimize memoization dependencies.

The textResourcesForLanguage memoization should include language in its dependency array since it's used within the memoized computation.

  const textResourcesForLanguage = useMemo(
    () => getTextResourcesForLanguage(language, textResources),
-    [textResources],
+    [textResources, language],
  );

112-112: Consider moving the language constant.

The hardcoded language constant should be moved to a configuration file or constants file for better maintainability. This will make it easier to update when implementing the language selection feature referenced in issue #14572.

Consider creating a new file constants.ts:

export const DEFAULT_LANGUAGE = 'nb';

Then import and use it in this file:

+import { DEFAULT_LANGUAGE } from './constants';
-const language: string = 'nb';
+const language: string = DEFAULT_LANGUAGE;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9fd63fd and 6ecebe1.

📒 Files selected for processing (1)
  • frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build environment and run e2e test
  • GitHub Check: CodeQL
  • GitHub Check: Testing
🔇 Additional comments (2)
frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/CodeListPage.tsx (2)

1-15: LGTM! Well-organized imports.

The new utility functions for text resource management are appropriately imported and organized.


89-107: LGTM! Consistent prop updates.

The text resource handling props are consistently updated across child components, improving the overall data flow.

Copy link
Contributor

@wrt95 wrt95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@wrt95 wrt95 merged commit ce58b90 into main Feb 13, 2025
12 checks passed
@wrt95 wrt95 deleted the add-text-resources-to-create-code-list-dialog branch February 13, 2025 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/content-library Area: Related to library for shared resources frontend skip-documentation Issues where updating documentation is not relevant skip-manual-testing PRs that do not need to be tested manually skip-releasenotes Issues that do not make sense to list in our release notes solution/studio/designer Issues related to the Altinn Studio Designer solution. team/studio-domain1
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

2 participants