|
| 1 | +/* |
| 2 | + * Copyright contributors to the Galasa project |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: EPL-2.0 |
| 5 | + */ |
| 6 | +"use client"; |
| 7 | +import React, { useEffect, useState } from 'react'; |
| 8 | +import { ProfileDetailsProps } from '@/utils/interfaces'; |
| 9 | +import { UserData } from '@/generated/galasaapi'; |
| 10 | +import styles from "@/styles/UserRole.module.css"; |
| 11 | +import { ButtonSet, Button, Dropdown, Loading } from '@carbon/react'; |
| 12 | +import ErrorPage from '@/app/error/page'; |
| 13 | +import { InlineNotification } from '@carbon/react'; |
| 14 | + |
| 15 | +interface DropdownItem { |
| 16 | + name: string, |
| 17 | + description: string |
| 18 | +} |
| 19 | + |
| 20 | +export default function UserRoleSection({ userProfilePromise }: ProfileDetailsProps) { |
| 21 | + |
| 22 | + const [userProfile, setUserProfile] = useState<UserData>({}); |
| 23 | + const [isLoading, setIsLoading] = useState(false); |
| 24 | + const [isError, setIsError] = useState(false); |
| 25 | + const [role, setRole] = useState({ |
| 26 | + id: "", |
| 27 | + name: "", |
| 28 | + description: "" |
| 29 | + }); |
| 30 | + const [isSaveBtnDisabled, setIsSaveBtnDisabled] = useState(true); |
| 31 | + const [isResetBtnDisabled, setIsResetBtnDisabled] = useState(true); |
| 32 | + const [isToastVisible, setIsToastVisible] = useState(false); |
| 33 | + |
| 34 | + const items = [ |
| 35 | + { |
| 36 | + id: "1", |
| 37 | + name: 'tester', |
| 38 | + description: "Test developer and runner" |
| 39 | + }, { |
| 40 | + id: "2", |
| 41 | + name: 'admin', |
| 42 | + description: "Administrator access" |
| 43 | + }, { |
| 44 | + id: "0", |
| 45 | + name: 'deactivated', |
| 46 | + description: "User has no access" |
| 47 | + } |
| 48 | + ]; |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + const loadUserProfile = async () => { |
| 52 | + setIsError(false); |
| 53 | + setIsLoading(true); |
| 54 | + |
| 55 | + try { |
| 56 | + |
| 57 | + const loadedProfile = await userProfilePromise; |
| 58 | + setUserProfile(loadedProfile); |
| 59 | + setRole({ id: loadedProfile.synthetic?.role?.metadata?.id!, name: loadedProfile.synthetic?.role?.metadata?.name!, description: loadedProfile.synthetic?.role?.metadata?.description! }); |
| 60 | + |
| 61 | + } catch (err) { |
| 62 | + setIsError(true); |
| 63 | + console.error(err); |
| 64 | + } finally { |
| 65 | + setIsLoading(false); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + loadUserProfile(); |
| 70 | + }, [userProfilePromise]); |
| 71 | + |
| 72 | + const changeUserRole = (e: any) => { |
| 73 | + |
| 74 | + setRole({ |
| 75 | + id: e.selectedItem.id, |
| 76 | + name: e.selectedItem.name, |
| 77 | + description: e.selectedItem.description |
| 78 | + }); |
| 79 | + |
| 80 | + setIsResetBtnDisabled(false); |
| 81 | + setIsSaveBtnDisabled(false); |
| 82 | + |
| 83 | + }; |
| 84 | + |
| 85 | + const resetRole = () => { |
| 86 | + |
| 87 | + setRole({ |
| 88 | + id: userProfile.synthetic?.role?.metadata?.id!, |
| 89 | + name: userProfile.synthetic?.role?.metadata?.name!, |
| 90 | + description: userProfile.synthetic?.role?.metadata?.description! |
| 91 | + }); |
| 92 | + |
| 93 | + setIsResetBtnDisabled(true); |
| 94 | + setIsSaveBtnDisabled(true); |
| 95 | + |
| 96 | + }; |
| 97 | + |
| 98 | + const updateUserRole = async () => { |
| 99 | + |
| 100 | + const requestBody = { |
| 101 | + roleDetails : { |
| 102 | + role: role.id |
| 103 | + }, |
| 104 | + userNumber: userProfile.id |
| 105 | + }; |
| 106 | + |
| 107 | + try { |
| 108 | + const response = await fetch('/users/edit/updateUserRole', { |
| 109 | + method: "PUT", |
| 110 | + headers: { |
| 111 | + "Content-Type": "application/json", |
| 112 | + }, |
| 113 | + body: JSON.stringify(requestBody) |
| 114 | + }); |
| 115 | + |
| 116 | + if (response.ok) { |
| 117 | + setIsResetBtnDisabled(true); |
| 118 | + setIsSaveBtnDisabled(true); |
| 119 | + setIsToastVisible(true); |
| 120 | + } |
| 121 | + } catch (err) { |
| 122 | + setIsError(true); |
| 123 | + } |
| 124 | + |
| 125 | + }; |
| 126 | + |
| 127 | + if (isLoading) { |
| 128 | + return <Loading small={false} active={isLoading} />; |
| 129 | + } |
| 130 | + |
| 131 | + if (isError) { |
| 132 | + return <ErrorPage />; |
| 133 | + } |
| 134 | + |
| 135 | + return ( |
| 136 | + <div className={styles.roleDetails}> |
| 137 | + <div className={styles.roleDetailsContainer}> |
| 138 | + <h2>{userProfile.loginId}</h2> |
| 139 | + <h4>User Role</h4> |
| 140 | + <p>The actions a user can or cannot on this Galasa service is controlled by their user role.</p> |
| 141 | + <div className={styles.dropdownContainer}> |
| 142 | + <Dropdown onChange={(e: any) => changeUserRole(e)} style={{ "width": "35%" }} size="lg" id="default" helperText={role.description} label={role.name} items={items} itemToString={(item: DropdownItem) => item ? item.name : ''} /> |
| 143 | + <ButtonSet className={styles.buttonSet}> |
| 144 | + <Button onClick={resetRole} disabled={isResetBtnDisabled} kind="secondary">Reset</Button> |
| 145 | + <Button onClick={updateUserRole} disabled={isSaveBtnDisabled} kind="primary">Save</Button> |
| 146 | + </ButtonSet> |
| 147 | + </div> |
| 148 | + |
| 149 | + { |
| 150 | + isToastVisible && <InlineNotification inline={true} onClose={() => setIsToastVisible(false)} lowContrast={true} kind="success" title="Success" subtitle="User role was updated successfully." /> |
| 151 | + } |
| 152 | + |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + ); |
| 156 | +} |
0 commit comments