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

Open
wants to merge 16 commits into
base: develop-postgres
Choose a base branch
from
Open
Changes from 5 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
60 changes: 52 additions & 8 deletions src/screens/UserPortal/People/People.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ export default function people(): JSX.Element {

// State for managing the number of rows per page in pagination
const [rowsPerPage, setRowsPerPage] = useState<number>(5);
const [members, setMembers] = useState([]);
const [members, setMembers] = useState<InterfaceMember[]>([]);
const [mode, setMode] = useState<number>(0);
const [updatedMembers, setUpdatedMembers] = useState<InterfaceMember[]>([]);

// Extracting organization ID from URL parameters
const { orgId: organizationId } = useParams();
Expand Down Expand Up @@ -135,23 +136,66 @@ export default function people(): JSX.Element {
};

useEffect(() => {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
if (data && data2) {
// Ensure organization exists
const organization = data2.organizations?.[0];
if (!organization) {
console.error('Failed to load organization data');
return;
}

interface InterfaceAdmin {
_id: string;
}
const adminIds = organization.admins.map(
(admin: InterfaceAdmin) => admin._id,
);
try {
const updatedMembers = data.organizationsMemberConnection.edges.map(
(member: InterfaceMember) => {
const isAdmin = adminIds.includes(member._id);
return {
...member,
userType: isAdmin ? 'Admin' : 'User',
};
},
);
setUpdatedMembers(updatedMembers);
setMembers(updatedMembers);
} catch (error) {
console.error('Failed to process member data:', error);
}
}
}, [data]);
}, [data, data2]);

const FILTER_MODES = {
ALL_MEMBERS: 0,
ADMINS: 1,
} as const;

/**
* Updates the list of members based on the selected filter mode.
*/
/* istanbul ignore next */
useEffect(() => {
if (mode == 0) {
if (mode === FILTER_MODES.ALL_MEMBERS) {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
setMembers(updatedMembers);
}
} else if (mode == 1) {
} else if (mode === FILTER_MODES.ADMINS) {
if (data2) {
setMembers(data2.organizations[0].admins);
const organization = data2.organizations?.[0];
if (!organization) {
console.error('Organization not found');
return;
}
const admins = data2.organizations[0].admins.map(
(admin: InterfaceMember) => ({
...admin,
userType: 'Admin' as const,
}),
);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix inconsistent organization access

There's an inconsistency in how the organization data is accessed. After checking for the organization's existence, the code still directly accesses data2.organizations[0] instead of using the validated organization variable.

Apply this fix:

-        const admins = data2.organizations[0].admins.map(
+        const admins = organization.admins.map(
           (admin: InterfaceMember) => ({
             ...admin,
             userType: 'Admin' as const,
           }),
         );
📝 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 admins = data2.organizations[0].admins.map(
(admin: InterfaceMember) => ({
...admin,
userType: 'Admin' as const,
}),
);
const admins = organization.admins.map(
(admin: InterfaceMember) => ({
...admin,
userType: 'Admin' as const,
}),
);

setMembers(admins);
}
}
}, [mode]);
Expand Down