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 1 commit
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
23 changes: 18 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 @@ export default function people(): JSX.Element {
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,21 @@ export default function people(): JSX.Element {
};

useEffect(() => {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
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]);
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add type safety and error handling to the data processing logic

The current implementation has several potential issues:

  1. The admin mapping uses any type
  2. No null checks for organizations[0]
  3. Potential race condition in data processing

Consider applying these improvements:

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



/**
* Updates the list of members based on the selected filter mode.
Expand All @@ -147,11 +159,12 @@ export default function people(): JSX.Element {
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) => { 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 mode-based filtering implementation

The current implementation has several areas for improvement:

  1. Uses magic numbers for mode comparison
  2. Inconsistent type handling for admin mapping
  3. No cleanup for state updates

Consider these improvements:

+ // 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 admins = data2.organizations[0].admins.map((admin: any) => { return { ...admin, userType: 'Admin' } });
+       const admins = data2.organizations[0].admins.map((admin: InterfaceMember) => ({
+         ...admin,
+         userType: 'Admin' as const
+       }));
        setMembers(admins);
      }
    }
+   return () => {
+     // Cleanup to prevent state updates after unmount
+     setMembers([]);
+   };
  }, [mode]);

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

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