-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dropdown to make user an org admin
- Loading branch information
1 parent
a1faf2a
commit edfa784
Showing
3 changed files
with
98 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Dropdown, DropdownItem, DropdownList, MenuToggle, MenuToggleElement } from '@patternfly/react-core'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { IntlShape } from 'react-intl'; | ||
import messages from '../../Messages'; | ||
import useChrome from '@redhat-cloud-services/frontend-components/useChrome'; | ||
import { useDispatch } from 'react-redux'; | ||
import { updateUserIsOrgAdminStatus } from '../../redux/actions/user-actions'; | ||
|
||
const OrgAdminDropdown: React.FC<{ | ||
isOrgAdmin: boolean; | ||
username: string; | ||
intl: IntlShape; | ||
}> = ({ isOrgAdmin, username, intl }) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [isDisabled, setIsDisabled] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
const { auth } = useChrome(); | ||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
const setUserDetails = async () => { | ||
if (!auth) { | ||
console.warn('Auth is undefined'); | ||
return; | ||
} | ||
|
||
const userData = await auth.getUser(); | ||
|
||
if (!userData || !userData.identity) { | ||
console.warn('User data or identity is undefined'); | ||
return; | ||
} | ||
|
||
const { user } = userData.identity; | ||
|
||
if (user?.username === username) { | ||
setIsDisabled(true); | ||
} | ||
}; | ||
|
||
setUserDetails(); | ||
}, [auth, username]); | ||
|
||
const onToggleClick = () => { | ||
setIsOpen((prev) => !prev); | ||
}; | ||
|
||
const onSelect = async (_event: React.MouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => { | ||
if (loading) return; | ||
|
||
const newStatus = value === 'yes'; | ||
if (newStatus === isOrgAdmin) return; | ||
|
||
setLoading(true); | ||
|
||
try { | ||
await dispatch(updateUserIsOrgAdminStatus(username, newStatus)); | ||
} catch (error) { | ||
console.error('Failed to update org admin status:', error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
|
||
setIsOpen(false); | ||
}; | ||
|
||
return ( | ||
<Dropdown | ||
isOpen={isOpen} | ||
onSelect={onSelect} | ||
onOpenChange={(isOpen: boolean) => setIsOpen(isOpen)} | ||
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( | ||
<MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen} isDisabled={isDisabled}> | ||
{isOrgAdmin ? intl.formatMessage(messages.yes) : intl.formatMessage(messages.no)} | ||
</MenuToggle> | ||
)} | ||
ouiaId="OrgAdminDropdown" | ||
shouldFocusToggleOnSelect | ||
> | ||
<DropdownList> | ||
<DropdownItem value="yes" key="yes"> | ||
{intl.formatMessage(messages.yes)} | ||
</DropdownItem> | ||
<DropdownItem value="no" key="no"> | ||
{intl.formatMessage(messages.no)} | ||
</DropdownItem> | ||
</DropdownList> | ||
</Dropdown> | ||
); | ||
}; | ||
|
||
export default OrgAdminDropdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters