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: trigger contributions refetch after redeeming code #2341

Merged
merged 5 commits into from
Jan 10, 2025

Conversation

sunilsabatp
Copy link
Contributor

Fixes #
This fix ensures that the contributions data is refetched after a redeem code is successfully processed. Previously, the contributions might not have been updated in real time, leading to stale or inconsistent data in the application.

Copy link

vercel bot commented Dec 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
planet-webapp-multi-tenancy-setup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 10, 2025 5:20am
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
planet-webapp ⬜️ Ignored (Inspect) Jan 10, 2025 5:20am
planet-webapp-temp ⬜️ Ignored (Inspect) Jan 10, 2025 5:20am

Copy link
Contributor

coderabbitai bot commented Dec 26, 2024

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm warn config production Use --omit=dev instead.
npm error code ERESOLVE
npm error ERESOLVE could not resolve
npm error
npm error While resolving: eslint-config-airbnb@18.2.1
npm error Found: eslint@8.57.1
npm error node_modules/eslint
npm error dev eslint@"^8.26.0" from the root project
npm error peer eslint@"6 || 7 || 8" from @emotion/eslint-plugin@11.12.0
npm error node_modules/@emotion/eslint-plugin
npm error dev @emotion/eslint-plugin@"^11.12.0" from the root project
npm error 15 more (@eslint-community/eslint-utils, ...)
npm error
npm error Could not resolve dependency:
npm error peer eslint@"^5.16.0 || ^6.8.0 || ^7.2.0" from eslint-config-airbnb@18.2.1
npm error node_modules/eslint-config-airbnb
npm error dev eslint-config-airbnb@"^18.2.0" from the root project
npm error
npm error Conflicting peer dependency: eslint@7.32.0
npm error node_modules/eslint
npm error peer eslint@"^5.16.0 || ^6.8.0 || ^7.2.0" from eslint-config-airbnb@18.2.1
npm error node_modules/eslint-config-airbnb
npm error dev eslint-config-airbnb@"^18.2.0" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry
npm error this command with --force or --legacy-peer-deps
npm error to accept an incorrect (and potentially broken) dependency resolution.
npm error
npm error
npm error For a full report see:
npm error /root/.npm/_logs/2025-01-10T05_22_27_546Z-eresolve-report.txt
npm error A complete log of this run can be found in: /root/.npm/_logs/2025-01-10T05_22_27_546Z-debug-0.log

Walkthrough

The pull request introduces modifications to the MyForestContext.tsx and RedeemModal/index.tsx files. The changes focus on enhancing the MyForestContext by adding new types and properties for refetching contributions and leaderboard data. The RedeemModal component is updated to utilize these new context methods, calling refetchContributions and refetchLeaderboard after successfully redeeming a code and updating the user's score.

Changes

File Change Summary
src/features/common/Layout/MyForestContext.tsx - Added ContributionsQueryRefetchType and LeaderboardQueryRefetchType types
- Updated MyForestContextInterface with refetchContributions and refetchLeaderboard properties
- Updated MyForestProvider to return new refetch methods
src/features/user/Profile/ProfileCard/RedeemModal/index.tsx - Imported useMyForest hook
- Added refetchContributions and refetchLeaderboard variables
- Called refetchContributions and refetchLeaderboard after successful code redemption

Possibly related PRs

Suggested labels

PR: reviewed-approved

Poem

🐰 In the forest of code, a context so bright,
Refetching contributions with rabbit-like might!
A method so nimble, a context so clean,
Updating our data with a magical sheen! 🌳
Hop, hop, hooray for context supreme! 🚀


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.

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)
src/features/user/Profile/ProfileCard/RedeemModal/index.tsx (1)

69-69: Refetch contributions after successful redemption.
Refetching upon successful unit addition is essential to keep the user’s data in sync. However, consider surrounding refetchContributions() with a try/catch block in case of errors or network failures, and handle user feedback accordingly.

            if (res.units > 0) {
              const cloneUser = { ...user };
              cloneUser.score.received = cloneUser.score.received + res.units;
              setUser(cloneUser);
-             refetchContributions();
+             try {
+               await refetchContributions();
+             } catch (error) {
+               console.error('Failed to refetch contributions:', error);
+               // Optionally display a user-facing error message
+             }
            }
src/features/common/Layout/MyForestContext.tsx (1)

41-41: Consider replacing any with a more specific type.
Static analysis flags the usage of TRPCClientErrorBase<any> as it resorts to any. If possible, please narrow it to an explicit error shape or a generic type param provided by your TRPC definitions.

-    TRPCClientErrorBase<any>
+    TRPCClientErrorBase<YourExplicitErrorType>
🧰 Tools
🪛 GitHub Check: CodeFactor

[warning] 41-41: src/features/common/Layout/MyForestContext.tsx#L41
Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b338449 and c2b3cb8.

📒 Files selected for processing (2)
  • src/features/common/Layout/MyForestContext.tsx (6 hunks)
  • src/features/user/Profile/ProfileCard/RedeemModal/index.tsx (3 hunks)
🧰 Additional context used
🪛 GitHub Check: CodeFactor
src/features/common/Layout/MyForestContext.tsx

[warning] 41-41: src/features/common/Layout/MyForestContext.tsx#L41
Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

🔇 Additional comments (4)
src/features/user/Profile/ProfileCard/RedeemModal/index.tsx (2)

20-20: Good addition of the useMyForest import.
This import integrates the contributions refetch feature into the redeem process, aligning with the PR's objective of ensuring data is updated after redemption.


42-42: Destructuring refetchContributions from context.
This step makes it straightforward to invoke a data refetch after the code is redeemed, helping avoid stale data in the UI. Good job!

src/features/common/Layout/MyForestContext.tsx (2)

33-43: Robust type definitions for refetchContributions.
Declaring the return type as QueryObserverResult<...> clearly communicates the structure of the returned data. Ensure all child components appropriately handle any potential errors, as indicated by TRPCClientErrorBase<any>.

🧰 Tools
🪛 GitHub Check: CodeFactor

[warning] 41-41: src/features/common/Layout/MyForestContext.tsx#L41
Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)


Line range hint 213-231: Context value now supports refreshing contributions.
By exposing _contributions.refetch, you allow components to efficiently refresh the data. This is a neat way to ensure real-time updates after the redemption flow, fulfilling the primary objective of this PR.

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 (1)
src/features/common/Layout/MyForestContext.tsx (1)

33-43: Consider improving type safety for error handling.

While the RefetchContributions type correctly models the refetch function's return type, we could improve type safety by replacing any with a more specific error type if available from your TRPC router.

-    TRPCClientErrorBase<any>
+    TRPCClientErrorBase<{
+      code: string;
+      message: string;
+      // Add other specific error properties from your TRPC router
+    }>
🧰 Tools
🪛 GitHub Check: CodeFactor

[warning] 41-41: src/features/common/Layout/MyForestContext.tsx#L41
Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c2b3cb8 and e3f93a8.

📒 Files selected for processing (1)
  • src/features/common/Layout/MyForestContext.tsx (5 hunks)
🧰 Additional context used
🪛 GitHub Check: CodeFactor
src/features/common/Layout/MyForestContext.tsx

[warning] 41-41: src/features/common/Layout/MyForestContext.tsx#L41
Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (3)
src/features/common/Layout/MyForestContext.tsx (3)

16-17: LGTM! Necessary type imports added.

The new type imports from @tanstack/react-query and @trpc/client are correctly added to support the refetch functionality.


57-57: LGTM! Interface properly extended.

The MyForestContextInterface is correctly extended with the refetchContributions property using the new type.


213-213: LGTM! Refetch function correctly exposed and memoized.

The implementation properly exposes the TRPC query's refetch function through the context and correctly includes it in the dependency array. This change enables the desired functionality of refetching contributions data after code redemption.

Let's verify that the refetch function is properly used in the redeem flow:

Also applies to: 231-231

✅ Verification successful

Verified: refetchContributions is properly integrated in the redeem flow

The function is correctly consumed in src/features/user/Profile/ProfileCard/RedeemModal/index.tsx and called after successful code redemption to refresh the contributions data.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of refetchContributions in the redeem-related code
rg -l "refetchContributions.*RedeemModal" --type ts --type tsx

Length of output: 92


Script:

#!/bin/bash
# First locate the RedeemModal component
echo "=== Finding RedeemModal component ==="
rg -l "RedeemModal" --type typescript --type typescriptreact

echo -e "\n=== Finding refetchContributions usage ==="
rg "refetchContributions" --type typescript --type typescriptreact

Length of output: 383


Script:

#!/bin/bash
echo "=== Finding RedeemModal component ==="
fd -e ts -e tsx | xargs rg -l "RedeemModal"

echo -e "\n=== Finding refetchContributions usage ==="
fd -e ts -e tsx | xargs rg "refetchContributions" -C 2

Length of output: 2344

Copy link
Contributor

@mariahosfeld mariahosfeld left a comment

Choose a reason for hiding this comment

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

Functionality works, values are updated immediately.

@mohitb35
Copy link
Collaborator

@sunilsabatp Made the following changes:

  1. Refetched leaderboard as well, since that could also be affected by code redemption
  2. Used inference to determine the type for refetchContributions and refetchLeaderboard instead of setting up our own types.

LGTM now.

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 (1)
src/features/user/Profile/ProfileCard/RedeemModal/index.tsx (1)

69-70: Consider handling potential race condition.

While the placement of refetch calls is logical, there might be a race condition if the backend hasn't fully processed the score update before the refetch calls are made. Consider one of these approaches:

  1. Add a small delay before refetching:
 if (res.units > 0) {
   const cloneUser = { ...user };
   cloneUser.score.received = cloneUser.score.received + res.units;
   setUser(cloneUser);
-  refetchContributions();
-  refetchLeaderboard();
+  setTimeout(() => {
+    refetchContributions();
+    refetchLeaderboard();
+  }, 1000);
 }
  1. Or better, consider making these operations sequential using async/await if the backend provides a way to verify the update:
 if (res.units > 0) {
   const cloneUser = { ...user };
   cloneUser.score.received = cloneUser.score.received + res.units;
-  setUser(cloneUser);
-  refetchContributions();
-  refetchLeaderboard();
+  await setUser(cloneUser);
+  await refetchContributions();
+  await refetchLeaderboard();
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e057d45 and 0edac97.

📒 Files selected for processing (2)
  • src/features/common/Layout/MyForestContext.tsx (4 hunks)
  • src/features/user/Profile/ProfileCard/RedeemModal/index.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/features/common/Layout/MyForestContext.tsx
🔇 Additional comments (3)
src/features/user/Profile/ProfileCard/RedeemModal/index.tsx (3)

20-20: LGTM! Import statement is well-placed.

The import of useMyForest is correctly positioned and follows the project's import structure.


42-42: LGTM! Hook usage follows React best practices.

The destructuring of refetchContributions and refetchLeaderboard from useMyForest is properly placed at the component's top level, adhering to React's rules of hooks.


Line range hint 48-102: LGTM! Robust error handling and security measures.

The implementation includes:

  • Proper authentication checks
  • Comprehensive error handling with localized messages
  • Secure API calls with authentication tokens

@mariahosfeld mariahosfeld merged commit a8a9e36 into develop Jan 10, 2025
9 checks passed
@mariahosfeld mariahosfeld deleted the hotfix/update-progress-bar branch January 10, 2025 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants