Skip to content

Fix DataTableContext Sort By and Grouping properties not saving #3165

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

codeWithLFN
Copy link

@codeWithLFN codeWithLFN commented Apr 19, 2025

Description

This PR fixes issue #3152 where the Sort By and Grouping properties in the DataTableContext component weren't saving correctly when jsSetting was enabled.

Problem

When jsSetting was enabled for the Sort By and Grouping properties in the DataTableContext component, the selected properties weren't persisting after saving the form configuration.

Solution

The fix addresses the issue by:

  1. Enhancing the SortingEditor component to properly initialize and structure values
  2. Ensuring proper value persistence in the SortingEditorComponent when jsSetting is enabled
  3. Adding validation to make sure all required properties are present in the data structure

Summary by CodeRabbit

  • Bug Fixes

    • Improved initialization of sorting configuration to prevent issues when the value is null or undefined.
    • Enhanced handling of sorting items to ensure each has a unique identifier and default sorting direction.
    • Addressed persistence issues with "Sort By" and "Grouping" settings in the data table context.
  • Refactor

    • Streamlined value change handling to allow for future normalization or validation.

Copy link
Contributor

coderabbitai bot commented Apr 19, 2025

Walkthrough

The updates focus on improving the initialization and change handling logic for sorting configurations within React components related to data tables. The SortingEditor component now ensures that its value prop is initialized to an empty array if undefined or null, and that each sorting item has a unique identifier and a default sorting direction. Additionally, the designer component for the sorting editor introduces a wrapper for the onChange handler, potentially facilitating future normalization or validation, while maintaining the existing component signatures.

Changes

Files/Paths Change Summary
shesha-reactjs/src/components/dataTable/sortingConfigurator/index.tsx Added initialization of the value prop to an empty array if null/undefined using useEffect; introduced handleItemChange to ensure sorting items have unique IDs and a default sort direction before updating the list.
shesha-reactjs/src/designer-components/sortingEditor/index.tsx Wrapped the onChange handler in a new handleChange function within the render function for ConfigurableFormItem, preparing for potential value normalization or validation; no changes to component signatures.

Sequence Diagram(s)

sequenceDiagram
    participant ParentComponent
    participant SortingEditor
    participant ListEditor

    ParentComponent->>SortingEditor: Render with value/onChange
    SortingEditor->>SortingEditor: useEffect (initialize value if null/undefined)
    SortingEditor->>ListEditor: Pass handleItemChange as onItemChange
    ListEditor->>SortingEditor: onItemChange (item changed)
    SortingEditor->>SortingEditor: Ensure item has id and sorting direction
    SortingEditor->>ParentComponent: onChange (updated value)
Loading

Suggested reviewers

  • James-Baloyi

Poem

In the garden of code where the data flows free,
Sorting seeds are planted, with IDs for each tree.
With every change handled, and order set right,
The tables now sparkle, columns shining bright.
🐇✨
Hop along, dear coder, your work’s quite a sight!

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between cd45eee and c4104e3.

📒 Files selected for processing (2)
  • shesha-reactjs/src/components/dataTable/sortingConfigurator/index.tsx (2 hunks)
  • shesha-reactjs/src/designer-components/sortingEditor/index.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • shesha-reactjs/src/components/dataTable/sortingConfigurator/index.tsx
🔇 Additional comments (6)
shesha-reactjs/src/designer-components/sortingEditor/index.tsx (6)

3-3: Good addition of React hooks.

Adding the useEffect and useState hooks is necessary for the new initialization logic to properly track component state.


13-13: Appropriate type import.

Importing GroupingItem as SortingItem provides proper type safety for the sorting items, which helps maintain consistency in the data structure.


36-37: Good initialization tracking.

Using a state variable to track initialization status prevents redundant resets of the form value.


45-55: Effective initialization logic.

The initialization approach correctly handles cases where the value is null, undefined, or not an array by setting an empty array as the default value. This ensures consistent data structure throughout the component lifecycle.


57-64: Robust change handler implementation.

The custom change handler properly normalizes null values to empty arrays, ensuring data consistency and fixing the persistence issue with jsSetting.


66-71: Clean component rendering with proper defaults.

The SortingEditor component is now rendered with guaranteed valid props, ensuring it always receives an array as its value and has proper handling for changes.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @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.

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: 2

🧹 Nitpick comments (1)
shesha-reactjs/src/designer-components/sortingEditor/index.tsx (1)

42-47: Consider normalizing incoming values in handleChange.

Since the comment indicates this is fixing a persistence issue, you might want to ensure the value is properly structured before passing it to onChange. This could include checking if the value is null/undefined and converting it to an empty array, or ensuring all items have required properties.

 const handleChange = (newValue) => {
     // Make sure we're passing a properly structured value for the jsSetting scenario
+    if (newValue === null || newValue === undefined) {
+        newValue = [];
+    } else if (Array.isArray(newValue)) {
+        // Ensure all items have required properties
+        newValue = newValue.map(item => ({
+            id: item.id || getNanoId(),
+            propertyName: item.propertyName || '',
+            sorting: item.sorting || 'asc',
+        }));
+    }
     onChange(newValue);
 };
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2bbd08b and cd45eee.

📒 Files selected for processing (2)
  • shesha-reactjs/src/components/dataTable/sortingConfigurator/index.tsx (2 hunks)
  • shesha-reactjs/src/designer-components/sortingEditor/index.tsx (1 hunks)
🔇 Additional comments (2)
shesha-reactjs/src/components/dataTable/sortingConfigurator/index.tsx (1)

44-44: LGTM! Good addition of the onItemChange handler.

Adding the onItemChange handler to the ListEditor ensures that all items have proper IDs and default sorting directions.

shesha-reactjs/src/designer-components/sortingEditor/index.tsx (1)

41-55:

❓ Verification inconclusive

The handleChange function is currently just a pass-through.

The handleChange function is described in comments as fixing the persistence issue with jsSetting, but it currently just forwards the value without any additional processing. Consider adding validation or normalization logic if needed.


🏁 Script executed:

#!/bin/bash
# This script checks if the sorting values are properly structured when saved with jsSetting enabled

# Look for usage of the SortingEditor with jsSetting enabled
echo "Checking for usages of SortingEditor with jsSetting enabled..."
rg -A 5 "canBeJsSetting: true" --glob "*.tsx" | grep -B 5 -A 5 "dataSortingEditor"

# Check if there are any references to handleChange in other sorting-related files
echo "Checking for related handleChange implementations..."
rg "handleChange.*sorting" --glob "*.tsx"

Length of output: 674


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "🔍 Searching for any jsSetting or canBeJsSetting references across the repository:"
rg 'jsSetting|canBeJsSetting' -n

echo
echo "📄 Showing the first 200 lines of the SortingEditor wrapper for full context:"
sed -n '1,200p' shesha-reactjs/src/designer-components/sortingEditor/index.tsx || echo "File not found: please verify the path."

Length of output: 229


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "🔍 Locating SortingEditor usages:"
rg "SortingEditor" -n --glob "*.tsx" || echo "No matches for SortingEditor"

echo
echo "🗂 Locating sortingEditor directory path:"
find . -type d -iname "sortingeditor" || echo "No sortingEditor directory found"

echo
echo "📄 Displaying content of index.tsx in sortingEditor if found:"
file=$(find . -type f -iname "index.tsx" 2>/dev/null | grep -i sortingeditor | head -n1 || true)
if [[ -n "$file" ]]; then
  echo "Found: $file"
  sed -n '1,200p' "$file"
else
  echo "index.tsx under sortingEditor not found"
fi

Length of output: 820


It looks like I can’t locate shesha-reactjs/src/designer-components/sortingEditor/index.tsx or any references to SortingEditor in the repo—this may mean the path or filename has changed. Could you please:

  • Confirm the exact location and casing of the sortingEditor/index.tsx file.
  • Verify whether handleChange is still a no‑op pass‑through in that file, or if any normalization/validation logic has been added elsewhere.

Once we have the correct path and code, we can ensure the original concern about missing processing in handleChange is addressed.

Comment on lines 19 to 24
// Ensure value is properly initialized when component is first rendered
useEffect(() => {
if (value === null || value === undefined) {
onChange([]);
}
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Initialization handling could be improved with dependencies.

The useEffect hook correctly initializes the value to an empty array when null or undefined, but it's missing dependencies in the dependency array. This could lead to unexpected behavior if the value or onChange props change after the component mounts.

- useEffect(() => {
-     if (value === null || value === undefined) {
-         onChange([]);
-     }
- }, []);
+ useEffect(() => {
+     if (value === null || value === undefined) {
+         onChange([]);
+     }
+ }, [value, onChange]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Ensure value is properly initialized when component is first rendered
useEffect(() => {
if (value === null || value === undefined) {
onChange([]);
}
}, []);
// Ensure value is properly initialized when component is first rendered
useEffect(() => {
if (value === null || value === undefined) {
onChange([]);
}
}, [value, onChange]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant