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 User Role in People Section #2459

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/screens/UserPortal/People/People.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
const [rowsPerPage, setRowsPerPage] = useState<number>(5);
const [members, setMembers] = useState([]);
const [mode, setMode] = useState<number>(0);
const [updatedMembers, setUpdatedMembers] = useState([]);
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

Add type safety to the state variable

The new state variable should be properly typed to match the member interface.

- const [updatedMembers, setUpdatedMembers] = useState([]);
+ const [updatedMembers, setUpdatedMembers] = useState<InterfaceMember[]>([]);
📝 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
const [updatedMembers, setUpdatedMembers] = useState([]);
const [updatedMembers, setUpdatedMembers] = useState<InterfaceMember[]>([]);


// Extracting organization ID from URL parameters
const { orgId: organizationId } = useParams();
Expand Down Expand Up @@ -135,10 +136,24 @@
};

useEffect(() => {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
if (data && data2) {
const adminIds = data2.organizations[0].admins.map(
(admin: any) => admin._id,

Check failure on line 141 in src/screens/UserPortal/People/People.tsx

View workflow job for this annotation

GitHub Actions / Performs linting, formatting, type-checking, checking for different source and target branch

Unexpected any. Specify a different type
);

const updatedMembers = data.organizationsMemberConnection.edges.map(
(member: InterfaceMember) => {
const isAdmin = adminIds.includes(member._id);
return {
...member,
userType: isAdmin ? 'Admin' : 'User',
};
},
);
setUpdatedMembers(updatedMembers);
setMembers(updatedMembers);
}
}, [data]);
}, [data, data2]);
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

Add proper error handling and type safety to role mapping

While the role mapping logic addresses the core issue, it needs better error handling and type safety.

  useEffect(() => {
    if (data && data2) {
+     // Ensure organization exists
+     const organization = data2.organizations?.[0];
+     if (!organization) {
+       console.error('Organization not found');
+       return;
+     }
+     
+     interface Admin {
+       _id: string;
+     }
-     const adminIds = data2.organizations[0].admins.map((admin: any) => admin._id);
+     const adminIds = organization.admins.map((admin: Admin) => admin._id);

      const updatedMembers = data.organizationsMemberConnection.edges.map(
        (member: InterfaceMember) => {
          const isAdmin = adminIds.includes(member._id);
          return {
            ...member,
            userType: isAdmin ? 'Admin' : 'User',
          };
        },
      );
      setUpdatedMembers(updatedMembers);
      setMembers(updatedMembers);
    }
  }, [data, data2]);
📝 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
if (data && data2) {
const adminIds = data2.organizations[0].admins.map(
(admin: any) => admin._id,
);
const updatedMembers = data.organizationsMemberConnection.edges.map(
(member: InterfaceMember) => {
const isAdmin = adminIds.includes(member._id);
return {
...member,
userType: isAdmin ? 'Admin' : 'User',
};
},
);
setUpdatedMembers(updatedMembers);
setMembers(updatedMembers);
}
}, [data]);
}, [data, data2]);
if (data && data2) {
// Ensure organization exists
const organization = data2.organizations?.[0];
if (!organization) {
console.error('Organization not found');
return;
}
interface Admin {
_id: string;
}
const adminIds = organization.admins.map((admin: Admin) => admin._id);
const updatedMembers = data.organizationsMemberConnection.edges.map(
(member: InterfaceMember) => {
const isAdmin = adminIds.includes(member._id);
return {
...member,
userType: isAdmin ? 'Admin' : 'User',
};
},
);
setUpdatedMembers(updatedMembers);
setMembers(updatedMembers);
}
}, [data, data2]);
🧰 Tools
🪛 GitHub Check: Performs linting, formatting, type-checking, checking for different source and target branch

[failure] 141-141:
Unexpected any. Specify a different type


/**
* Updates the list of members based on the selected filter mode.
Expand All @@ -147,11 +162,14 @@
useEffect(() => {
if (mode == 0) {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
setMembers(updatedMembers);
}
} else if (mode == 1) {
if (data2) {
setMembers(data2.organizations[0].admins);
const admins = data2.organizations[0].admins.map((admin: any) => {

Check failure on line 169 in src/screens/UserPortal/People/People.tsx

View workflow job for this annotation

GitHub Actions / Performs linting, formatting, type-checking, checking for different source and target branch

Unexpected any. Specify a different type
return { ...admin, userType: 'Admin' };
});
setMembers(admins);
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

Improve type safety and readability in filtering logic

The current implementation needs improvements in type safety and maintainability.

+ // Add at the top of the file with other constants
+ const FILTER_MODES = {
+   ALL_MEMBERS: 0,
+   ADMINS: 1
+ } as const;

  useEffect(() => {
-   if (mode == 0) {
+   if (mode === FILTER_MODES.ALL_MEMBERS) {
      if (data) {
        setMembers(updatedMembers);
      }
-   } else if (mode == 1) {
+   } else if (mode === FILTER_MODES.ADMINS) {
      if (data2) {
+       const organization = data2.organizations?.[0];
+       if (!organization) {
+         console.error('Organization not found');
+         return;
+       }
-       const admins = data2.organizations[0].admins.map((admin: any) => {
+       const admins = organization.admins.map((admin: InterfaceMember) => {
          return { ...admin, userType: 'Admin' };
        });
        setMembers(admins);
      }
    }
  }, [mode]);

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Check: Performs linting, formatting, type-checking, checking for different source and target branch

[failure] 169-169:
Unexpected any. Specify a different type

}
}
}, [mode]);
Expand Down
Loading