Skip to content

Commit

Permalink
feat: do not show user agreement modal to rsd_admin
Browse files Browse the repository at this point in the history
  • Loading branch information
dmijatovic committed Feb 6, 2023
1 parent 70956d5 commit 645f440
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
28 changes: 28 additions & 0 deletions frontend/components/user/settings/UserAgreementModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,35 @@ it('does not render modal when terms accepted', async() => {
expect(modal).toBe(null)
})

it('does not render modal when role rsd_admin', async () => {
// role is rsd_admin
if (mockSession.user && mockSession.user.role) {
mockSession.user.role = 'rsd_admin'
}

mockFetchAgreementStatus.mockResolvedValueOnce({
status: 200,
data: {
agree_terms: false,
notice_privacy_statement: false
}
})

render(
<WithAppContext options={{session: mockSession}}>
<UserAgrementModal />
</WithAppContext>
)

const modal = await screen.queryByTestId('user-agreement-modal')
expect(modal).toBe(null)
})

it('accepts TOS via modal and calls patch account', async() => {
// role is rsd_admin
if (mockSession.user && mockSession.user.role) {
mockSession.user.role = 'rsd_user'
}

mockFetchAgreementStatus.mockResolvedValueOnce({
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/user/settings/UserAgreementModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function UserAgrementModal() {
const [agreeTerms, setAgreeTerms] = useState<boolean>(false)
const [noticePrivacy, setNoticePrivacy] = useState<boolean>(false)

const userInfo = useGetUserAgreementStatus(token, user?.account ?? '', setAgreeTerms, setNoticePrivacy, setOpen)
const userInfo = useGetUserAgreementStatus(token, user, setAgreeTerms, setNoticePrivacy, setOpen)

const methods = useForm<UserSettingsType>({
mode: 'onChange'
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/user/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function UserSettings() {
const [agreeTerms, setAgreeTerms] = useState<boolean>(false)
const [noticePrivacy, setNoticePrivacy] = useState<boolean>(false)

const userInfo = useGetUserAgreementStatus(token, user?.account ?? '', setAgreeTerms, setNoticePrivacy)
const userInfo = useGetUserAgreementStatus(token, user, setAgreeTerms, setNoticePrivacy)

const methods = useForm<UserSettingsType>({
mode: 'onChange',
Expand Down
14 changes: 9 additions & 5 deletions frontend/components/user/settings/useGetUserAgreementStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
// SPDX-License-Identifier: Apache-2.0

import {useEffect, useState} from 'react'
import {RsdUser} from '~/auth'
import {UserSettingsType} from '~/types/SoftwareTypes'
import {fetchAgreementStatus} from './fetchAgreementStatus'

export function useGetUserAgreementStatus(token: string, account: string, setAgreeTerms?: any, setNoticePrivacy?: any, setOpen?: any) {
export function useGetUserAgreementStatus(token: string, user:RsdUser|null, setAgreeTerms?: any, setNoticePrivacy?: any, setOpen?: any) {
const [userInfo, setUserInfo] = useState<UserSettingsType>()

useEffect(() => {
async function getUser() {
const respData = await fetchAgreementStatus(token, account)
const respData = await fetchAgreementStatus(token, user?.account ?? '')
if (respData.status === 200 && typeof(respData.data) !== 'undefined') {
setUserInfo(respData.data)
if (typeof(setOpen) === 'function' && (respData.data.agree_terms === false || respData.data.notice_privacy_statement === false)) {
if (typeof (setOpen) === 'function' &&
(respData.data.agree_terms === false || respData.data.notice_privacy_statement === false)
// rsd_admin does not need to accept UA
&& user?.role !== 'rsd_admin') {
setOpen(true)
}
if (
Expand All @@ -29,9 +33,9 @@ export function useGetUserAgreementStatus(token: string, account: string, setAgr
}
}
}
if (token && account) {
if (token && user && user?.account) {
getUser()
}
}, [token, account, setAgreeTerms, setNoticePrivacy, setOpen])
}, [token, user, setAgreeTerms, setNoticePrivacy, setOpen])
return userInfo
}

0 comments on commit 645f440

Please sign in to comment.