Skip to content

Commit

Permalink
Corrected Admin button functionality (PalisadoesFoundation#1509)
Browse files Browse the repository at this point in the history
* Corrected placeholder value and added missing fields

* Corrected placeholder value and added missing fields

* Corrected placeholder value and added missing fields

* Corrected placeholder value and added missing fields

* added tests

* Update .env.example

* Update .env.example

* fixed

* fixed files

* added tests

* Corrected Add Admin Button's functionality

* Added superadmin role

* Added tests

* Addded Tests

* Added Tests
  • Loading branch information
Anvita0305 authored Feb 3, 2024
1 parent 95d4869 commit ddc7e42
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 17 deletions.
1 change: 1 addition & 0 deletions public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@
"memberDetail": {
"title": "User Details",
"addAdmin": "Add Admin",
"alreadyIsAdmin": "Member is already an Admin",
"organizations": "Organizations",
"events": "Events",
"role": "Role",
Expand Down
1 change: 1 addition & 0 deletions public/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@
"memberDetail": {
"title": "Détails de l'utilisateur",
"addAdmin": "Ajouter un administrateur",
"alreadyIsAdmin": "Le membre est déjà un administrateur",
"organizations": "Organisations",
"events": "Événements",
"role": "Rôle",
Expand Down
1 change: 1 addition & 0 deletions public/locales/hi.json
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@
"memberDetail": {
"title": "उपयोगकर्ता विवरण",
"addAdmin": "व्यवस्थापक जोड़ें",
"alreadyIsAdmin": "सदस्य पहले से ही एक व्यवस्थापक है",
"organizations": "संगठन",
"events": "घटनाएं",
"role": "भूमिका",
Expand Down
1 change: 1 addition & 0 deletions public/locales/sp.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@
"memberDetail": {
"title": "Detalles del usuario",
"addAdmin": "Agregar administrador",
"alreadyIsAdmin": "El Miembro ya es Administrador",
"organizations": "Organizaciones",
"events": "Eventos",
"role": "Rol",
Expand Down
1 change: 1 addition & 0 deletions public/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@
"memberDetail": {
"title": "用户详细信息",
"addAdmin": "添加管理员",
"alreadyIsAdmin": "会员已经是管理员",
"organizations": "组织",
"events": "事件",
"role": "角色",
Expand Down
34 changes: 26 additions & 8 deletions src/components/OrgAdminListCard/OrgAdminListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { useMutation } from '@apollo/client';
import { toast } from 'react-toastify';

import styles from './OrgAdminListCard.module.css';
import { REMOVE_ADMIN_MUTATION } from 'GraphQl/Mutations/mutations';
import {
REMOVE_ADMIN_MUTATION,
UPDATE_USERTYPE_MUTATION,
} from 'GraphQl/Mutations/mutations';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router-dom';
import defaultImg from 'assets/images/blank.png';
Expand All @@ -25,6 +28,8 @@ const currentUrl = window.location.href.split('=')[1];

function orgAdminListCard(props: InterfaceOrgPeopleListCardProps): JSX.Element {
const [remove] = useMutation(REMOVE_ADMIN_MUTATION);
const [updateUserType] = useMutation(UPDATE_USERTYPE_MUTATION);

const [showRemoveAdminModal, setShowRemoveAdminModal] = React.useState(false);

const { t } = useTranslation('translation', {
Expand All @@ -36,19 +41,32 @@ function orgAdminListCard(props: InterfaceOrgPeopleListCardProps): JSX.Element {

const removeAdmin = async (): Promise<void> => {
try {
const { data } = await remove({
const { data } = await updateUserType({
variables: {
userid: props.id,
orgid: currentUrl,
id: props.id,
userType: 'USER',
},
});

/* istanbul ignore next */
if (data) {
toast.success(t('adminRemoved'));
setTimeout(() => {
window.location.reload();
}, 2000);
try {
const { data } = await remove({
variables: {
userid: props.id,
orgid: currentUrl,
},
});
if (data) {
toast.success(t('adminRemoved'));
setTimeout(() => {
window.location.reload();
}, 2000);
}
} catch (error: any) {
/* istanbul ignore next */
errorHandler(t, error);
}
}
} catch (error: any) {
/* istanbul ignore next */
Expand Down
27 changes: 26 additions & 1 deletion src/screens/MemberDetail/MemberDetail.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from 'state/store';
import { I18nextProvider } from 'react-i18next';
import { ADD_ADMIN_MUTATION } from 'GraphQl/Mutations/mutations';
import {
ADD_ADMIN_MUTATION,
UPDATE_USERTYPE_MUTATION,
} from 'GraphQl/Mutations/mutations';
import { USER_DETAILS } from 'GraphQl/Queries/Queries';
import i18nForTest from 'utils/i18nForTest';
import { StaticMockLink } from 'utils/StaticMockLink';
Expand Down Expand Up @@ -60,6 +63,20 @@ const MOCKS1 = [
},
},
},
{
request: {
query: UPDATE_USERTYPE_MUTATION,
variables: {
id: '123',
userType: 'Admin',
},
},
result: {
data: {
success: true,
},
},
},
];

const MOCKS2 = [
Expand Down Expand Up @@ -168,6 +185,14 @@ describe('MemberDetail', () => {
expect(screen.getAllByText(/Role/i)).toHaveLength(2);
expect(screen.getAllByText(/Created/i)).toHaveLength(4);
expect(screen.getAllByText(/Joined/i)).toHaveLength(2);
expect(screen.getByTestId('addAdminBtn')).toBeInTheDocument();
const addAdminBtn = MOCKS1[2].request.variables.userType;
// if the button is not disabled
expect(screen.getByTestId('addAdminBtn').getAttribute('disabled')).toBe(
addAdminBtn == 'ADMIN' || addAdminBtn == 'SUPERADMIN'
? expect.anything()
: null
);
expect(screen.getByTestId('stateBtn')).toBeInTheDocument();
userEvent.click(screen.getByTestId('stateBtn'));
});
Expand Down
45 changes: 37 additions & 8 deletions src/screens/MemberDetail/MemberDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import Button from 'react-bootstrap/Button';
import { useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';
import UserUpdate from 'components/UserUpdate/UserUpdate';

import OrganizationScreen from 'components/OrganizationScreen/OrganizationScreen';
import { USER_DETAILS } from 'GraphQl/Queries/Queries';
import styles from './MemberDetail.module.css';
import { languages } from 'utils/languages';
import { ADD_ADMIN_MUTATION } from 'GraphQl/Mutations/mutations';
import {
ADD_ADMIN_MUTATION,
UPDATE_USERTYPE_MUTATION,
} from 'GraphQl/Mutations/mutations';
import { toast } from 'react-toastify';
import { errorHandler } from 'utils/errorHandler';
import Loader from 'components/Loader/Loader';
Expand All @@ -26,12 +28,16 @@ const MemberDetail: React.FC<MemberDetailProps> = ({ id }): JSX.Element => {
});

const [state, setState] = useState(1);
const [isAdmin, setIsAdmin] = useState(false);

const location = useLocation<MemberDetailProps>();
const currentUrl = location.state?.id || localStorage.getItem('id') || id;
const orgId = window.location.href.split('=')[1];
document.title = t('title');

const [adda] = useMutation(ADD_ADMIN_MUTATION);
const [updateUserType] = useMutation(UPDATE_USERTYPE_MUTATION);

const {
data: userData,
loading: loading,
Expand Down Expand Up @@ -62,20 +68,40 @@ const MemberDetail: React.FC<MemberDetailProps> = ({ id }): JSX.Element => {
const { data } = await adda({
variables: {
userid: location.state?.id,
orgid: currentUrl,
orgid: orgId,
},
});

/* istanbul ignore next */
if (data) {
toast.success(t('addedAsAdmin'));
setTimeout(() => {
window.location.reload();
}, 2000);
try {
const { data } = await updateUserType({
variables: {
id: location.state?.id,
userType: 'ADMIN',
},
});
if (data) {
toast.success(t('addedAsAdmin'));
setTimeout(() => {
window.location.reload();
}, 2000);
}
} catch (error: any) {
errorHandler(t, error);
}
}
} catch (error: any) {
/* istanbul ignore next */
errorHandler(t, error);
if (
userData.user.userType === 'ADMIN' ||
userData.user.userType === 'SUPERADMIN'
) {
setIsAdmin(true);
toast.error(t('alreadyIsAdmin'));
} else {
errorHandler(t, error);
}
}
};

Expand All @@ -96,10 +122,13 @@ const MemberDetail: React.FC<MemberDetailProps> = ({ id }): JSX.Element => {
<div className={styles.btngroup}>
<Button
className={styles.memberfontcreatedbtn}
data-testid="addAdminBtn"
onClick={addAdmin}
disabled={isAdmin}
>
{t('addAdmin')}
</Button>

<Button
className={styles.memberfontcreatedbtn}
role="stateBtn"
Expand Down

0 comments on commit ddc7e42

Please sign in to comment.