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

feat(role-update): Settings User Role | Ask user to relogin once the role has been updated #6742

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions frontend/src/api/user/editUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const editUser = async (
try {
const response = await axios.put(`/user/${props.userId}`, {
Name: props.name,
Role: props.role,
});

return {
Expand Down
32 changes: 25 additions & 7 deletions frontend/src/container/MySettings/UserInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import '../MySettings.styles.scss';
import './UserInfo.styles.scss';

import { Button, Card, Flex, Input, Space, Typography } from 'antd';
import { Button, Card, Flex, Input, Space, Select, Typography } from 'antd';
import editUser from 'api/user/editUser';
import { useNotifications } from 'hooks/useNotifications';
import { PencilIcon } from 'lucide-react';
Expand All @@ -15,15 +14,18 @@ import { UPDATE_USER } from 'types/actions/app';
import AppReducer from 'types/reducer/app';

import { NameInput } from '../styles';
import { ROLES, USER_ROLES } from 'types/roles';
import { Logout } from 'api/utils';

function UserInfo(): JSX.Element {
const { user, role, org, userFlags } = useSelector<AppState, AppReducer>(
const { user, org, role, userFlags } = useSelector<AppState, AppReducer>(
(state) => state.app,
);
const { t } = useTranslation();
const dispatch = useDispatch<Dispatch<AppActions>>();

const [changedName, setChangedName] = useState<string>(user?.name || '');
const [changedRole, setChangedRole] = useState<ROLES>(role || 'ADMIN');
const [loading, setLoading] = useState<boolean>(false);

const { notifications } = useNotifications();
Expand All @@ -32,12 +34,17 @@ function UserInfo(): JSX.Element {
return <div />;
}

const updateRole = (value: ROLES): void => {
setChangedRole(value);
};

const onClickUpdateHandler = async (): Promise<void> => {
try {
setLoading(true);
const { statusCode } = await editUser({
name: changedName,
userId: user.userId,
role: changedRole,
});

if (statusCode === 200) {
Expand All @@ -51,12 +58,19 @@ function UserInfo(): JSX.Element {
payload: {
...user,
name: changedName,
ROLE: role || 'ADMIN',
ROLE: changedRole || 'ADMIN',
orgId: org[0].id,
orgName: org[0].name,
userFlags: userFlags || {},
},
});

if (changedRole !== role) {
notifications.info({
message: t('Please login again', { ns: 'common' }),
});
Logout();
}
} else {
notifications.error({
message: t('something_went_wrong', {
Expand Down Expand Up @@ -130,11 +144,15 @@ function UserInfo(): JSX.Element {
{' '}
Role{' '}
</Typography>
<Input
<Select
className="userInfo-value"
value={role || ''}
disabled
data-testid="role-textbox"
value={changedRole}
onChange={updateRole}
options={Object.entries(USER_ROLES).map(([role]) => ({
label: role,
value: role,
}))}
/>
</Space>
</Space>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/api/user/editUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type PayloadProps = Payload;
export interface Props {
userId: User['userId'];
name: User['name'];
role: User['role'];
}
1 change: 1 addition & 0 deletions frontend/src/types/reducer/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface User {
userId: string;
email: UserPayload['email'];
name: UserPayload['name'];
role: UserPayload['role'];
profilePictureURL: UserPayload['profilePictureURL'];
}

Expand Down