Skip to content

Comments

[WEB-2055] fix: enable admins to change role of other admins and add missing observers#5236

Merged
SatishGandham merged 1 commit intopreviewfrom
fix-member-role-edit-validation
Jul 29, 2024
Merged

[WEB-2055] fix: enable admins to change role of other admins and add missing observers#5236
SatishGandham merged 1 commit intopreviewfrom
fix-member-role-edit-validation

Conversation

@anmolsinghbhatia
Copy link
Collaborator

@anmolsinghbhatia anmolsinghbhatia commented Jul 26, 2024

Problem:

  • Previously, users with the admin role were unable to change the roles of other admins.

Solution:

  • Added the necessary validation to allow admin users to change admin roles as intended.
  • Included missing observers in the component to ensure proper functionality.

Reference:

[WEB-2055]

Media:

Before After
WEB-2055 Before (1) WEB-2055 After (1)
WEB-2055 Before (2) WEB-2055 After (2)

Summary by CodeRabbit

  • New Features

    • Enhanced the responsiveness of the Account Type Column component, allowing it to reactively reflect changes in user roles and permissions.
    • Introduced conditional rendering for role editing based on the current user's status (admin or member).
  • Bug Fixes

    • Improved clarity and maintainability of rendering logic within the Account Type Column, reducing nested conditional statements.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 26, 2024

Walkthrough

The recent updates enhance the AccountTypeColumn components across multiple files by integrating MobX for reactive state management. Key changes involve wrapping components with the observer function and introducing a useUser hook to determine user roles dynamically. This improves conditional rendering, allowing the UI to respond appropriately to user permissions and states, thus refining user experience and maintainability.

Changes

Files Change Summary
web/core/components/project/settings/member-columns.tsx
web/core/components/workspace/settings/member-columns.tsx
The AccountTypeColumn component is now an MobX observer, allowing for reactive updates. The useUser hook is introduced to handle user role checks, enabling improved conditional rendering based on user permissions. Return statements are streamlined for clarity, enhancing overall functionality and responsiveness.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AccountTypeColumn
    participant MobXStore

    User->>AccountTypeColumn: Requests member data
    AccountTypeColumn->>MobXStore: Fetches user roles
    MobXStore-->>AccountTypeColumn: Returns user roles
    AccountTypeColumn->>AccountTypeColumn: Checks isCurrentUser and isRoleEditable
    alt Role is editable
        AccountTypeColumn->>User: Displays role selection
    else Role is not editable
        AccountTypeColumn->>User: Displays role
    end
Loading

Poem

🐇 In a meadow of code, where rabbits do play,
New features are hopping, brightening the day.
With MobX as our friend, roles dance with delight,
Editable dreams in the moon's soft light.
So cheers to the changes, let joy be our guide,
In this world of components, let happiness reside! 🌼


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@anmolsinghbhatia anmolsinghbhatia changed the title [WEB-2055] fix: member role edit validation [WEB-2055] fix: enable admins to change user roles and add missing observers Jul 26, 2024
@anmolsinghbhatia anmolsinghbhatia marked this pull request as ready for review July 26, 2024 07:47
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d0e68cd and 0a54c4e.

Files selected for processing (2)
  • web/core/components/project/settings/member-columns.tsx (3 hunks)
  • web/core/components/workspace/settings/member-columns.tsx (3 hunks)
Additional comments not posted (10)
web/core/components/project/settings/member-columns.tsx (5)

Line range hint 1-10: Imports look good!

The import statements for observer and useUser are correct and necessary for the new functionality.


84-84: Wrapping with observer looks good!

The AccountTypeColumn component is now correctly wrapped with the observer function to enable reactivity.


95-95: useUser hook integration looks good!

The useUser hook is correctly integrated to retrieve the current user's data.


98-100: Conditional rendering logic is well-implemented!

The derived values isCurrentUser, isAdminRole, and isRoleEditable are correctly calculated and used for conditional rendering.


104-107: Simplified role display for non-editable roles.

The role display logic for non-editable roles is correctly simplified.

web/core/components/workspace/settings/member-columns.tsx (5)

Line range hint 1-10: Imports look good!

The import statements for observer and useUser are correct and necessary for the new functionality.


83-83: Wrapping with observer looks good!

The AccountTypeColumn component is now correctly wrapped with the observer function to enable reactivity.


94-94: useUser hook integration looks good!

The useUser hook is correctly integrated to retrieve the current user's data.


97-99: Conditional rendering logic is well-implemented!

The derived values isCurrentUser, isAdminRole, and isRoleEditable are correctly calculated and used for conditional rendering.


103-106: Simplified role display for non-editable roles.

The role display logic for non-editable roles is correctly simplified.

Comment on lines +109 to +150
<Controller
name="role"
control={control}
rules={{ required: "Role is required." }}
render={({ field: { value } }) => (
<CustomSelect
value={value}
onChange={(value: EUserProjectRoles) => {
if (!workspaceSlug) return;

updateMember(workspaceSlug.toString(), projectId.toString(), rowData.member.id, {
role: value as unknown as EUserProjectRoles, // Cast value to unknown first, then to EUserWorkspaceRoles
}).catch((err) => {
console.log(err, "err");
const error = err.error;
const errorString = Array.isArray(error) ? error[0] : error;

setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: errorString ?? "An error occurred while updating member role. Please try again.",
});
});
}}
label={
<div className="flex ">
<span>{ROLE[rowData.role as keyof typeof ROLE]}</span>
</div>
}
buttonClassName={`!px-0 !justify-start hover:bg-custom-background-100 ${errors.role ? "border-red-500" : "border-none"}`}
className="rounded-md p-0 w-32"
optionsClassName="w-full"
input
>
{Object.keys(ROLE).map((item) => (
<CustomSelect.Option key={item} value={item as unknown as EUserProjectRoles}>
{ROLE[item as unknown as keyof typeof ROLE]}
</CustomSelect.Option>
))}
</CustomSelect>
)}
/>
Copy link
Contributor

Choose a reason for hiding this comment

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

Role selection logic is well-implemented but improve error handling.

The role selection logic using Controller and CustomSelect is well-implemented. However, consider improving error handling by logging errors using a logging library instead of console.log.

- console.log(err, "err");
+ // Consider using a logging library for better error tracking
+ logger.error("Error updating member role", err);
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
<Controller
name="role"
control={control}
rules={{ required: "Role is required." }}
render={({ field: { value } }) => (
<CustomSelect
value={value}
onChange={(value: EUserProjectRoles) => {
if (!workspaceSlug) return;
updateMember(workspaceSlug.toString(), projectId.toString(), rowData.member.id, {
role: value as unknown as EUserProjectRoles, // Cast value to unknown first, then to EUserWorkspaceRoles
}).catch((err) => {
console.log(err, "err");
const error = err.error;
const errorString = Array.isArray(error) ? error[0] : error;
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: errorString ?? "An error occurred while updating member role. Please try again.",
});
});
}}
label={
<div className="flex ">
<span>{ROLE[rowData.role as keyof typeof ROLE]}</span>
</div>
}
buttonClassName={`!px-0 !justify-start hover:bg-custom-background-100 ${errors.role ? "border-red-500" : "border-none"}`}
className="rounded-md p-0 w-32"
optionsClassName="w-full"
input
>
{Object.keys(ROLE).map((item) => (
<CustomSelect.Option key={item} value={item as unknown as EUserProjectRoles}>
{ROLE[item as unknown as keyof typeof ROLE]}
</CustomSelect.Option>
))}
</CustomSelect>
)}
/>
}).catch((err) => {
// Consider using a logging library for better error tracking
logger.error("Error updating member role", err);
const error = err.error;
const errorString = Array.isArray(error) ? error[0] : error;
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: errorString ?? "An error occurred while updating member role. Please try again.",
});
});

Comment on lines +108 to +150
<Controller
name="role"
control={control}
rules={{ required: "Role is required." }}
render={({ field: { value } }) => (
<CustomSelect
value={value}
onChange={(value: EUserProjectRoles) => {
console.log({ value, workspaceSlug }, "onChange");
if (!workspaceSlug) return;

updateMember(workspaceSlug.toString(), rowData.member.id, {
role: value as unknown as EUserWorkspaceRoles, // Cast value to unknown first, then to EUserWorkspaceRoles
}).catch((err) => {
console.log(err, "err");
const error = err.error;
const errorString = Array.isArray(error) ? error[0] : error;

setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: errorString ?? "An error occurred while updating member role. Please try again.",
});
});
}}
label={
<div className="flex ">
<span>{ROLE[rowData.role as keyof typeof ROLE]}</span>
</div>
}
buttonClassName={`!px-0 !justify-start hover:bg-custom-background-100 ${errors.role ? "border-red-500" : "border-none"}`}
className="rounded-md p-0 w-32"
optionsClassName="w-full"
input
>
{Object.keys(ROLE).map((item) => (
<CustomSelect.Option key={item} value={item as unknown as EUserProjectRoles}>
{ROLE[item as unknown as keyof typeof ROLE]}
</CustomSelect.Option>
))}
</CustomSelect>
)}
/>
Copy link
Contributor

Choose a reason for hiding this comment

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

Role selection logic is well-implemented but improve error handling.

The role selection logic using Controller and CustomSelect is well-implemented. However, consider improving error handling by logging errors using a logging library instead of console.log.

- console.log(err, "err");
+ // Consider using a logging library for better error tracking
+ logger.error("Error updating member role", err);
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
<Controller
name="role"
control={control}
rules={{ required: "Role is required." }}
render={({ field: { value } }) => (
<CustomSelect
value={value}
onChange={(value: EUserProjectRoles) => {
console.log({ value, workspaceSlug }, "onChange");
if (!workspaceSlug) return;
updateMember(workspaceSlug.toString(), rowData.member.id, {
role: value as unknown as EUserWorkspaceRoles, // Cast value to unknown first, then to EUserWorkspaceRoles
}).catch((err) => {
console.log(err, "err");
const error = err.error;
const errorString = Array.isArray(error) ? error[0] : error;
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: errorString ?? "An error occurred while updating member role. Please try again.",
});
});
}}
label={
<div className="flex ">
<span>{ROLE[rowData.role as keyof typeof ROLE]}</span>
</div>
}
buttonClassName={`!px-0 !justify-start hover:bg-custom-background-100 ${errors.role ? "border-red-500" : "border-none"}`}
className="rounded-md p-0 w-32"
optionsClassName="w-full"
input
>
{Object.keys(ROLE).map((item) => (
<CustomSelect.Option key={item} value={item as unknown as EUserProjectRoles}>
{ROLE[item as unknown as keyof typeof ROLE]}
</CustomSelect.Option>
))}
</CustomSelect>
)}
/>
}).catch((err) => {
// Consider using a logging library for better error tracking
logger.error("Error updating member role", err);
const error = err.error;
const errorString = Array.isArray(error) ? error[0] : error;
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: errorString ?? "An error occurred while updating member role. Please try again.",
});
});

@SatishGandham SatishGandham changed the title [WEB-2055] fix: enable admins to change user roles and add missing observers [WEB-2055] fix: enable admins to change role of other admins and add missing observers Jul 29, 2024
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.

2 participants