From 2691ffbf6affeac1584500930a1791d276cb5bb2 Mon Sep 17 00:00:00 2001 From: Sampo Tawast Date: Fri, 27 Oct 2023 13:06:30 +0300 Subject: [PATCH] feat: use 'terms' param to provide terms from API --- .../terms/tests/test_terms_of_service_api.py | 4 ++-- backend/benefit/users/api/v1/serializers.py | 8 +++++++- .../applicant/src/components/layout/Layout.tsx | 12 ++++++------ .../components/termsOfService/TermsOfService.tsx | 6 +++--- .../applicant/src/hooks/useTermsOfServiceData.ts | 9 +++++---- .../benefit/applicant/src/hooks/useUserQuery.ts | 14 ++++++++++++-- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/backend/benefit/terms/tests/test_terms_of_service_api.py b/backend/benefit/terms/tests/test_terms_of_service_api.py index 477710e543..d1300068b6 100644 --- a/backend/benefit/terms/tests/test_terms_of_service_api.py +++ b/backend/benefit/terms/tests/test_terms_of_service_api.py @@ -41,7 +41,7 @@ def test_terms_of_service_in_effect_pdf( ) # ... wrong type of terms TermsFactory(effective_from=date.today(), terms_type=TermsType.APPLICANT_TERMS) - response = api_client.get(get_current_user_url()) + response = api_client.get(get_current_user_url() + "?terms=1") assert ( get_company_from_request(response.wsgi_request) @@ -87,7 +87,7 @@ def test_terms_of_service_in_effect_md( ) # ... wrong type of terms TermsFactory(effective_from=date.today(), terms_type=TermsType.APPLICANT_TERMS) - response = api_client.get(get_current_user_url()) + response = api_client.get(get_current_user_url() + "?terms=1") assert ( get_company_from_request(response.wsgi_request) diff --git a/backend/benefit/users/api/v1/serializers.py b/backend/benefit/users/api/v1/serializers.py index 762a115fb7..2b8588ed7c 100644 --- a/backend/benefit/users/api/v1/serializers.py +++ b/backend/benefit/users/api/v1/serializers.py @@ -48,8 +48,14 @@ class Meta: @extend_schema_field(TermsSerializer()) def get_terms_of_service_in_effect(self, obj): + request = self.context.get("request") + include_terms = ( + request.query_params.get("terms") + if request and request.query_params + else None + ) terms = Terms.objects.get_terms_in_effect(TermsType.TERMS_OF_SERVICE) - if terms: + if terms and include_terms: # If given the request in context, DRF will output the URL for FileFields context = {"request": self.context.get("request")} return TermsSerializer(terms, context=context).data diff --git a/frontend/benefit/applicant/src/components/layout/Layout.tsx b/frontend/benefit/applicant/src/components/layout/Layout.tsx index 0d7b02446e..7c595d4f78 100644 --- a/frontend/benefit/applicant/src/components/layout/Layout.tsx +++ b/frontend/benefit/applicant/src/components/layout/Layout.tsx @@ -6,6 +6,7 @@ import { useRouter } from 'next/router'; import * as React from 'react'; import useAuth from 'shared/hooks/useAuth'; import theme from 'shared/styles/theme'; +import { getLocalStorageItem } from 'shared/utils/localstorage.utils'; import { DefaultTheme } from 'styled-components'; import { ROUTES } from '../../constants'; @@ -33,7 +34,7 @@ const selectBgColor = (pathname: string): keyof DefaultTheme['colors'] => { const Layout: React.FC = ({ children, ...rest }) => { const { isAuthenticated } = useAuth(); - const [isTermsOfServiceApproved, setIsTermsOfSerivceApproved] = + const [isTermsOfServiceApproved, setIsTermsOfServiceApproved] = React.useState(false); const router = useRouter(); @@ -44,11 +45,10 @@ const Layout: React.FC = ({ children, ...rest }) => { React.useEffect(() => { if (IS_CLIENT) { - setIsTermsOfSerivceApproved( + setIsTermsOfServiceApproved( // eslint-disable-next-line scanjs-rules/identifier_localStorage - localStorage.getItem( - LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED - ) === 'true' + getLocalStorageItem(LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED) === + 'true' ); } }, []); @@ -60,7 +60,7 @@ const Layout: React.FC = ({ children, ...rest }) => { !isAccessibilityStatement && !isTermsOfServiceApproved ? ( ) : ( children diff --git a/frontend/benefit/applicant/src/components/termsOfService/TermsOfService.tsx b/frontend/benefit/applicant/src/components/termsOfService/TermsOfService.tsx index 1b6d1005b4..ec0fb8643b 100644 --- a/frontend/benefit/applicant/src/components/termsOfService/TermsOfService.tsx +++ b/frontend/benefit/applicant/src/components/termsOfService/TermsOfService.tsx @@ -37,11 +37,11 @@ export const $Markdown = styled(ReactMarkdown)` `; type TermsOfServiceProps = { - setIsTermsOfSerivceApproved: (isTermsOfServiceApproved: boolean) => void; + setIsTermsOfServiceApproved: (isTermsOfServiceApproved: boolean) => void; }; const TermsOfService: React.FC = ({ - setIsTermsOfSerivceApproved, + setIsTermsOfServiceApproved, }) => { const { theme, @@ -50,7 +50,7 @@ const TermsOfService: React.FC = ({ termsInEffectMarkdown, user, approveTermsOfService, - } = useTermsOfServiceData(setIsTermsOfSerivceApproved); + } = useTermsOfServiceData(setIsTermsOfServiceApproved); const { mutate } = useApproveTermsOfServiceMutation(); diff --git a/frontend/benefit/applicant/src/hooks/useTermsOfServiceData.ts b/frontend/benefit/applicant/src/hooks/useTermsOfServiceData.ts index 3fc352016f..4815da6020 100644 --- a/frontend/benefit/applicant/src/hooks/useTermsOfServiceData.ts +++ b/frontend/benefit/applicant/src/hooks/useTermsOfServiceData.ts @@ -3,6 +3,7 @@ import { TermsProp, User } from 'benefit-shared/types/application'; import { TFunction } from 'next-i18next'; import React from 'react'; import { Language } from 'shared/i18n/i18n'; +import { setLocalStorageItem } from 'shared/utils/localstorage.utils'; import { capitalize } from 'shared/utils/string.utils'; import { DefaultTheme, useTheme } from 'styled-components'; @@ -21,7 +22,7 @@ type ExtendedComponentProps = { }; const useTermsOfServiceData = ( - setIsTermsOfSerivceApproved: (isTermsOfServiceApproved: boolean) => void + setIsTermsOfServiceApproved: (isTermsOfServiceApproved: boolean) => void ): ExtendedComponentProps => { const locale = useLocale(); const textLocale = capitalize(locale); @@ -39,16 +40,16 @@ const useTermsOfServiceData = ( ); const approveTermsOfService = (): void => { - setIsTermsOfSerivceApproved(true); + setIsTermsOfServiceApproved(true); // eslint-disable-next-line scanjs-rules/identifier_localStorage - localStorage.setItem( + setLocalStorageItem( LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED, 'true' ); }; const getTermsMarkdownByLanguage = (): string => { - if (!user.termsOfServiceInEffect) { + if (!user?.termsOfServiceInEffect) { return ''; } switch (locale) { diff --git a/frontend/benefit/applicant/src/hooks/useUserQuery.ts b/frontend/benefit/applicant/src/hooks/useUserQuery.ts index 98233451f1..9d0b591b89 100644 --- a/frontend/benefit/applicant/src/hooks/useUserQuery.ts +++ b/frontend/benefit/applicant/src/hooks/useUserQuery.ts @@ -7,7 +7,10 @@ import { useQuery, UseQueryResult } from 'react-query'; import showErrorToast from 'shared/components/toast/show-error-toast'; import useBackendAPI from 'shared/hooks/useBackendAPI'; import useLocale from 'shared/hooks/useLocale'; -import { setLocalStorageItem } from 'shared/utils/localstorage.utils'; +import { + getLocalStorageItem, + setLocalStorageItem, +} from 'shared/utils/localstorage.utils'; import { LOCAL_STORAGE_KEYS } from '../constants'; @@ -42,9 +45,16 @@ const useUserQuery = ( } }; + const params = + getLocalStorageItem(LOCAL_STORAGE_KEYS.IS_TERMS_OF_SERVICE_APPROVED) === + 'true' + ? {} + : { terms: 1 }; + return useQuery( queryKeys ?? `${BackendEndpoint.USER}`, - () => handleResponse(axios.get(BackendEndpoint.USER_ME)), + () => + handleResponse(axios.get(BackendEndpoint.USER_ME, { params })), { refetchInterval: FIVE_MINUTES, enabled: !logout,