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(web): updating metadata from the table is rejected #1260

Merged
merged 5 commits into from
Oct 24, 2024

Conversation

caichi-t
Copy link
Contributor

@caichi-t caichi-t commented Oct 11, 2024

Overview

This PR fixes the bug that updating metadata from the table is rejected.

How to reproduce this bug

  1. Creating a model without metadata fields.
  2. Creating an item.
  3. Adding metadata field to the model.
  4. Updating the metadata of the item saved earlier from the table. (Successfully)
  5. Updating the same metadata in the same way. (The bug happens.)

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for item and request creation and updates, providing clearer user feedback.
    • Improved management of metadata within the project components.
  • Bug Fixes

    • Adjusted expectations in test cases related to metadata updates to ensure accurate behavior verification.
  • Refactor

    • Streamlined data fetching strategies and metadata management across various components for better performance and clarity.

Copy link

coderabbitai bot commented Oct 11, 2024

Walkthrough

The changes in this pull request enhance the error handling and data management strategies within the ContentDetails and ContentList components. The createItem and related mutation signatures have been refined to focus on relevant queries, while a new Metadata type has been introduced to improve metadata management. The internal logic for handling metadata updates has been streamlined for efficiency, resulting in a more cohesive structure for error handling and data retrieval.

Changes

File Path Change Summary
web/src/components/molecules/Content/types.ts Added new Metadata type and updated Item type to use this new type for its metadata property.
web/src/components/organisms/Project/Content/ContentDetails/hooks.ts Updated createItem mutation's refetchQueries to remove "SearchItem" and improved error handling in handleItemCreate, handleRequestCreate, and handleRequestUpdate functions.
web/src/components/organisms/Project/Content/ContentList/hooks.ts Replaced metadataVersion with itemIdToMetadata map, refactored handleMetaItemUpdate for better metadata management, and updated related functions to align with new metadata structure.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ContentDetails
    participant API

    User->>ContentDetails: Create Item
    ContentDetails->>API: Call createItem
    API-->>ContentDetails: Return success
    ContentDetails->>ContentDetails: Notify user of success
    ContentDetails->>ContentDetails: Refetch GetRequests
Loading

🐇 "In the meadow where data flows,
A new type of Metadata grows.
With hooks that catch each little fall,
Error handling stands proud and tall.
So hop along, let's code anew,
For every change brings something true!" 🌼

Possibly related PRs

  • fix(web): toggle ok button on modal for updating field #1252: This PR focuses on enhancing error handling in the GeometryItem component, which aligns with the main PR's emphasis on improved error handling in the handleItemCreate, handleRequestCreate, and handleRequestUpdate functions.
  • fix(web): link to asset and refer to item icons #1272: Although primarily about linking assets and item icons, this PR includes changes to the LinkItemRequestModal, which may involve similar data handling strategies as seen in the main PR's updates to mutation signatures and error handling.

Suggested reviewers

  • nourbalaha

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9bfc4da and 6238c11.

📒 Files selected for processing (1)
  • web/src/components/organisms/Project/Content/ContentDetails/hooks.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • web/src/components/organisms/Project/Content/ContentDetails/hooks.ts

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 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 the web label Oct 11, 2024
Copy link

netlify bot commented Oct 11, 2024

Deploy Preview for reearth-cms ready!

Name Link
🔨 Latest commit 6238c11
🔍 Latest deploy log https://app.netlify.com/sites/reearth-cms/deploys/6719c8dbb08f410008baeabd
😎 Deploy Preview https://deploy-preview-1260--reearth-cms.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

@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: 4

🧹 Outside diff range and nitpick comments (4)
web/src/components/molecules/Content/types.ts (1)

20-24: LGTM! Consider simplifying the fields property type.

The new Metadata type is well-structured and provides good flexibility for metadata representation. However, the fields property type could be simplified.

Consider changing the fields property type from ItemField[] | undefined | null to just ItemField[] | null. In TypeScript, undefined is often interchangeable with null, and using both can lead to unnecessary type checking. Here's the suggested change:

export type Metadata = {
  id?: string;
  version: string;
-  fields: ItemField[] | undefined | null;
+  fields: ItemField[] | null;
};

This change would simplify the type without losing any meaningful functionality.

web/e2e/project/item/metadata/update.spec.ts (2)

29-30: Approved: Changes align with PR objective and improve test robustness.

The additions to click the switch and close the notification before checking the attribute state improve the test's accuracy by simulating real user interactions. The change in the expected state from "true" to "false" reflects the updated behavior after these interactions.

Consider adding a brief comment explaining why the expected state is now "false" after the interactions. This would improve the test's readability and make it easier for other developers to understand the expected behavior.

Also applies to: 34-34


Line range hint 1-43: Summary: Test improvements align with PR objective, but consistency should be verified.

The changes to the "Updating metadata added later from table has succeeded" test case improve its accuracy by simulating more realistic user interactions. This aligns well with the PR objective of fixing issues with metadata updates.

However, the discrepancy between the expected outcomes in the table update and edit page update test cases raises a question about consistency.

To ensure the robustness of these tests and the underlying functionality:

  1. Verify if the different expected outcomes for table updates vs. edit page updates are intentional.
  2. If not intentional, update the edit page test case to match the new expected behavior.
  3. Consider adding comments to explain any intentional differences in behavior between these two update methods.
  4. If possible, add more test cases to cover edge cases and ensure comprehensive coverage of the metadata update functionality.

These steps will help maintain the integrity of the tests and provide clear documentation for future developers working on this feature.

web/src/components/organisms/Project/Content/ContentDetails/hooks.ts (1)

Line range hint 240-275: Improved error handling in handleItemCreate function

The error handling in the handleItemCreate function has been significantly improved. It now provides separate error handling for metadata item creation and main item creation, with appropriate user notifications for both success and failure scenarios. This enhancement improves the overall robustness and user experience of the function.

Consider extracting the error notification logic into a separate function to reduce code duplication. For example:

const notifyError = (message: string) => {
  Notification.error({ message: t(message) });
};

// Usage
if (metaItem.errors || !metaItem.data?.createItem) {
  notifyError("Failed to create item.");
  return;
}

This would make the code more DRY and easier to maintain.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c7c79a4 and add3e7c.

📒 Files selected for processing (4)
  • web/e2e/project/item/metadata/update.spec.ts (1 hunks)
  • web/src/components/molecules/Content/types.ts (2 hunks)
  • web/src/components/organisms/Project/Content/ContentDetails/hooks.ts (1 hunks)
  • web/src/components/organisms/Project/Content/ContentList/hooks.ts (5 hunks)
🧰 Additional context used
🔇 Additional comments (7)
web/src/components/molecules/Content/types.ts (2)

37-37: LGTM! Good refactoring of the Item type.

The update to the Item type, using the newly defined Metadata type for its metadata property, is a good refactoring decision. This change improves code readability and maintainability by centralizing the metadata structure definition.

This change aligns well with the PR objectives of addressing metadata-related issues. It provides a more structured approach to handling metadata, which could help prevent bugs like the one described in the PR summary.


Line range hint 20-37: Good refactoring, but verify impact on metadata update functionality.

The introduction of the Metadata type and its integration into the Item type is a positive step towards better metadata management. These changes provide a more structured approach to handling metadata, which aligns with the PR's objective of fixing metadata update issues.

However, it's important to note that type changes alone may not fully resolve the issue of rejected metadata updates described in the PR summary.

To ensure these changes effectively address the reported bug, please run the following verification steps:

  1. Create a model without any metadata fields.
  2. Create an item for this model.
  3. Add a metadata field to the model.
  4. Update the metadata of the previously saved item from the table.
  5. Attempt to update the same metadata again in the same manner.

If the second update (step 5) is successful, it indicates that these changes have resolved the issue. If not, further investigation may be needed in the components or hooks that handle the actual metadata update process.

This script will help identify components or hooks that might need additional changes to fully resolve the metadata update issue.

web/e2e/project/item/metadata/update.spec.ts (1)

Line range hint 37-43: Verify consistency between table and edit page update tests.

While changes were made to the "Updating metadata added later from table has succeeded" test, the "Updating metadata added later from edit page has succeeded" test remains unchanged. The expected state of the switch is different between these two tests ("false" for table update, "true" for edit page update).

Please confirm if this difference in behavior between updating from the table and the edit page is intentional. If not, consider updating this test case to match the new expected behavior.

web/src/components/organisms/Project/Content/ContentDetails/hooks.ts (2)

Line range hint 445-449: Improved error handling and refetchQueries update

The handleRequestCreate and handleRequestUpdate functions have been enhanced with proper error handling and user notifications. This improvement provides better feedback to users and increases the robustness of the request creation and update processes.

The refetchQueries for createRequestMutation has been updated to ["GetModalRequests", "GetItem"]. This change might affect data consistency across the application.

To ensure this change doesn't introduce any data inconsistencies, please verify the following:

  1. Check if GetModalRequests is sufficient to update all necessary UI components after request creation.
  2. Confirm that GetItem is needed and correctly updates the item details after request creation.

Run the following script to help with this verification:

#!/bin/bash
# Description: Verify the usage of GetModalRequests and GetItem queries

# Check for components using GetModalRequests
echo "Components using GetModalRequests:"
rg --type typescript 'useQuery.*GetModalRequests'

# Check for components using GetItem
echo "Components using GetItem:"
rg --type typescript 'useQuery.*GetItem'

# Check for other places where requests are created or updated
echo "Other places handling request creation or update:"
rg --type typescript 'createRequest|updateRequest'

Review the results to ensure that all necessary components are updated after request creation and that no unintended side effects are introduced by this change.

Also applies to: 465-484


234-234: Consider the impact of removing "SearchItem" from refetchQueries

The refetchQueries parameter for the createItem mutation has been updated to only include "GetRequests". This change may improve performance by reducing unnecessary refetches. However, it's important to ensure that this doesn't negatively impact the immediate visibility of newly created items in search results.

To verify the impact of this change, we can check if there are any other mechanisms in place to update the search results after item creation. Run the following script:

web/src/components/organisms/Project/Content/ContentList/hooks.ts (2)

13-13: Importing Metadata is appropriate

The Metadata type is correctly imported for use in the component.


44-44: Importing ItemFieldInput is appropriate

The ItemFieldInput type is correctly imported and will be used in GraphQL mutations.

@caichi-t caichi-t enabled auto-merge (squash) October 24, 2024 04:11
@caichi-t caichi-t merged commit b85ef41 into main Oct 24, 2024
15 checks passed
@caichi-t caichi-t deleted the fix-web/metadata-from-table branch October 24, 2024 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants