diff --git a/src/components/member-profile/index.js b/src/components/member-profile/index.js index 6048d9ce..1dd99ccc 100644 --- a/src/components/member-profile/index.js +++ b/src/components/member-profile/index.js @@ -377,7 +377,7 @@ const Profile = (props) => { )} {isSuperUser && ( -
+
{showMemberRoleUpdateModal && }
)} diff --git a/src/components/member-role-update/index.js b/src/components/member-role-update/index.js index 1b9ae427..00c33a98 100644 --- a/src/components/member-role-update/index.js +++ b/src/components/member-role-update/index.js @@ -6,10 +6,14 @@ import Spinner from '@components/UI/spinner'; import MemberTagAssign from '@components/member-tag-assign'; import { BASE_API_URL } from '@constants/AppConstants'; import useFetch from '@custom-hooks/useFetch'; +import { useRouter } from 'next/router'; import classNames from './member-role-update.module.scss'; import { memberRoleUpdate } from '../../helper-functions/action-handlers'; const MemberRoleUpdate = () => { + const { query } = useRouter() || { query: { dev: false } }; + const { dev } = query; + const isDev = Boolean(dev); // convert string to boolean const { showMemberRoleUpdateModal, setShowMemberRoleUpdateModal, @@ -18,7 +22,8 @@ const MemberRoleUpdate = () => { const [isUpdating, setIsUpdating] = useState(false); const [updateStatus, setUpdateStatus] = useState(''); - + const [validateError, setvalidateError] = useState(''); + const [reasonText, setReasonText] = useState(''); const { data: userData } = useFetch( `${BASE_API_URL}/users/${selectedMember}` ); @@ -51,22 +56,28 @@ const MemberRoleUpdate = () => { } }; - const archiveUnArchiveTheMember = async (id) => { - let archiveRole = null; + const archiveUnArchiveTheMember = async (id, reason) => { setIsUpdating(true); + let body = {}; if (archived) { - archiveRole = false; + body = { + archived: false, + }; + } else if (!reason && !archived) { + body = { + archived: true, + }; } else { - archiveRole = true; + body = { + archived: true, + reason, + }; } - const role = { - archived: archiveRole, - }; try { - const { status } = await memberRoleUpdate(id, role); + const { status } = await memberRoleUpdate(id, body); setIsUpdating(false); if (status === 200) { - setUpdateStatus('user archived!'); + setUpdateStatus(archived ? 'User unarchived!' : 'User archived!'); } } catch (error) { setUpdateStatus('Some error occured, please contact admin'); @@ -75,6 +86,7 @@ const MemberRoleUpdate = () => { const memberRoleUpdateButton = ( ); - + const handleValidReason = (id, reason) => { + const isEmptyReason = !reason.length; + const isReasonEmptyOrWhitespace = /^\s*$/.test(reason); // check for empty or multiple whitespaces + const isMoreThan99Words = reason.split(' ').length > 99; + const isMoreThan50Characters = reason.length <= 25; + switch (!archived) { + case isEmptyReason: + setvalidateError('Reason cannot be empty!'); + break; + case isReasonEmptyOrWhitespace: + setvalidateError('Reason cannot be empty or multiple whitespaces!'); + break; + case isMoreThan99Words: + setvalidateError('Reason cannot be more than 99 words!'); + break; + case isMoreThan50Characters: + setvalidateError('Reason should have more than 25 characters!'); + break; + default: + setvalidateError(''); + } + const isValid = + !isEmptyReason && + !isReasonEmptyOrWhitespace && + !isMoreThan99Words && + !isMoreThan50Characters; + if (isValid || archived) { + archiveUnArchiveTheMember(id, reason); + } + }; const memeberArchiveUnArchiveButton = ( ); + const archiveReasonTextBox = ( +
+

{validateError}

+