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

WEB: Discard changes working fine when switching tab of app settings #312

Merged
merged 5 commits into from
Oct 8, 2024

Conversation

nxtCoder19
Copy link
Contributor

@nxtCoder19 nxtCoder19 commented Oct 8, 2024

Summary by Sourcery

Implement a new hook to check account ownership and enhance the unsaved changes handling by introducing a callback mechanism and a standardized action management system.

New Features:

  • Introduce a new hook useIsOwner to determine if the current user is the owner of an account by checking their role against the account's team members.

Enhancements:

  • Enhance the UnsavedChangesProvider to accept an onProceed callback, allowing actions to be performed when changes are discarded.
  • Refactor the handling of unsaved changes by introducing a DISCARD_ACTIONS object to manage different discard actions consistently across components.

Copy link

sourcery-ai bot commented Oct 8, 2024

Reviewer's Guide by Sourcery

This pull request implements changes to improve the functionality of discarding changes when switching tabs in app settings. It introduces a new hook for checking owner status, updates the UnsavedChangesProvider to handle discarding changes, and refactors the use of action constants across multiple components.

Class diagram for useIsOwner hook

classDiagram
    class useIsOwner {
        +boolean isOwner
        +boolean isLoading
        +useCallback()
    }
    useIsOwner --> useCustomSwr
    useIsOwner --> useConsoleApi
    useCustomSwr --> useConsoleApi
Loading

File-Level Changes

Change Details Files
Implement discard changes functionality in app settings
  • Add useEffect hook to handle discarding changes
  • Update useForm hook to include reset functionality
  • Modify initialValues to use getReadOnlyContainer().env
  • Add reset() call in useEffect for readOnlyApp changes
src/apps/console/routes/_main+/$account+/env+/$environment+/new-app/app-environment-variables.tsx
Refactor UnsavedChangesProvider and introduce DISCARD_ACTIONS constants
  • Add onProceed prop to UnsavedChangesProvider
  • Implement DISCARD_ACTIONS object with action constants
  • Update Popup.Button onClick to call onProceed
lib/client/hooks/use-unsaved-changes.tsx
Update app settings layout to use new DISCARD_ACTIONS constants
  • Replace string literals with DISCARD_ACTIONS constants
  • Add onProceed prop to UnsavedChangesProvider in Settings component
src/apps/console/routes/_main+/$account+/env+/$environment+/app+/$app+/settings+/_layout.tsx
Refactor other components to use DISCARD_ACTIONS constants
  • Update performAction checks to use DISCARD_ACTIONS constants
src/apps/console/page-components/app/general.tsx
src/apps/iot-console/routes/_main+/$account+/$project+/deviceblueprint+/$deviceblueprint+/app+/$app+/settings+/_layout.tsx
src/apps/console/page-components/app/compute.tsx
Implement useIsOwner hook for checking account ownership
  • Create useIsOwner hook to fetch team members and current user
  • Implement isOwner function to check if current user is account owner
src/apps/console/hooks/use-is-owner.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @nxtCoder19 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using the DISCARD_ACTIONS object consistently throughout the codebase instead of hardcoded strings for action types. This will improve maintainability and reduce the chance of typos.
  • Remove or add TODO comments for commented-out code blocks to improve code cleanliness and maintainability.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

import useCustomSwr from '~/root/lib/client/hooks/use-custom-swr';
import { useConsoleApi } from '../server/gql/api-provider';

export const useIsOwner = ({ accountName }: { accountName: string }) => {
Copy link

Choose a reason for hiding this comment

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

suggestion: Add error handling to API calls in useIsOwner hook

Consider adding error handling for the API calls in this custom hook. This will make the hook more robust and provide better feedback in case of network issues or API errors.

export const useIsOwner = ({ accountName }: { accountName: string }) => {
  const api = useConsoleApi();
  const { data: teamMembers, isLoading: teamMembersLoading, error } = useCustomSwr(
    // ... existing code ...
  );

  if (error) {
    console.error('Error fetching team members:', error);
  }

@@ -205,9 +209,11 @@ const EnvironmentVariablesList = ({
};

export const EnvironmentVariables = () => {
const { setContainer, getContainer } = useAppState();
const { setContainer, getContainer, getReadOnlyContainer, readOnlyApp } =
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the component to reduce complexity and improve code organization.

The changes have introduced unnecessary complexity to this component. While the new functionality is valuable, it can be implemented more efficiently. Consider the following improvements:

  1. Consolidate form handling logic:
const { values, setValues, submit, reset } = useForm({
  initialValues: getReadOnlyContainer().env || [],
  validationSchema: Yup.array(entry),
  onSubmit: (val) => {
    setContainer((c) => ({ ...c, env: val }));
  },
});

useEffect(() => {
  submit();
}, [values]);
  1. Move unsaved changes logic to a higher-level component or a more appropriate place in the application structure. This component should focus on form handling.

  2. Simplify useEffect hooks:

useEffect(() => {
  if (performAction === DISCARD_ACTIONS.DISCARD_CHANGES) {
    reset();
  }
}, [performAction, reset]);

useEffect(() => {
  reset();
}, [readOnlyApp, reset]);

These changes will maintain the new functionality while significantly reducing the complexity of the component. The form handling becomes more straightforward, and the concerns of managing unsaved changes are separated from the direct form manipulation.

);

const isOwner = useCallback(() => {
if (!teamMembers || !currentUser) return false;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Use block braces for ifs, whiles, etc. (use-braces)

Suggested change
if (!teamMembers || !currentUser) return false;
if (!teamMembers || !currentUser) {


ExplanationIt is recommended to always use braces and create explicit statement blocks.

Using the allowed syntax to just write a single statement can lead to very confusing
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).

@nxtCoder19 nxtCoder19 merged commit 7a9d432 into devx/changes Oct 8, 2024
3 checks passed
@nxtCoder19 nxtCoder19 deleted the update/environments branch October 8, 2024 07:14
abdheshnayak pushed a commit that referenced this pull request Oct 28, 2024
WEB: Discard changes working fine when switching tab of app settings
tulsiojha pushed a commit that referenced this pull request Nov 1, 2024
WEB: Discard changes working fine when switching tab of app settings
abdheshnayak pushed a commit that referenced this pull request Nov 5, 2024
WEB: Discard changes working fine when switching tab of app settings
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.

2 participants