diff --git a/backend/benefit/helsinkibenefit/urls.py b/backend/benefit/helsinkibenefit/urls.py index ff8a7c8f1f..8ad96bff1c 100644 --- a/backend/benefit/helsinkibenefit/urls.py +++ b/backend/benefit/helsinkibenefit/urls.py @@ -26,7 +26,7 @@ HandlerNoteViewSet, ) from terms.api.v1.views import ApproveTermsOfServiceView -from users.api.v1.views import CurrentUserView, UserUuidGDPRAPIView +from users.api.v1.views import CurrentUserView, UserOptionsView, UserUuidGDPRAPIView router = routers.DefaultRouter() router.register( @@ -70,6 +70,7 @@ path("v1/company/search//", SearchOrganisationsView.as_view()), path("v1/company/get//", GetOrganisationByIdView.as_view()), path("v1/users/me/", CurrentUserView.as_view()), + path("v1/users/options/", UserOptionsView.as_view()), path( "v1/handlerapplications//review/", ReviewStateView.as_view() ), diff --git a/backend/benefit/users/api/v1/views.py b/backend/benefit/users/api/v1/views.py index 230c6e37d9..6b23edd1c7 100644 --- a/backend/benefit/users/api/v1/views.py +++ b/backend/benefit/users/api/v1/views.py @@ -1,3 +1,4 @@ +from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.base_user import AbstractBaseUser from django.core.exceptions import PermissionDenied @@ -37,6 +38,25 @@ def _get_current_user(self, request): return request.user +@extend_schema(description="API for setting currently logged in user's language.") +class UserOptionsView(APIView): + permission_classes = [BFIsAuthenticated] + + def get(self, request): + lang = request.GET.get("lang") + + if lang in ["fi", "en", "sv"]: + token = request.COOKIES.get("yjdhcsrftoken") + + response = Response( + {"lang": lang, "token": token}, status=status.HTTP_200_OK + ) + response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang, httponly=True) + return response + + return Response(status=status.HTTP_400_BAD_REQUEST) + + class UserUuidGDPRAPIView(GDPRAPIView): """GDPR API view that is used from Helsinki profile to query and delete user data.""" diff --git a/frontend/benefit/applicant/src/components/header/useHeader.ts b/frontend/benefit/applicant/src/components/header/useHeader.ts index 986f2bfebb..9877f076b9 100644 --- a/frontend/benefit/applicant/src/components/header/useHeader.ts +++ b/frontend/benefit/applicant/src/components/header/useHeader.ts @@ -1,10 +1,12 @@ import useApplicationQuery from 'benefit/applicant/hooks/useApplicationQuery'; import { useTranslation } from 'benefit/applicant/i18n'; import { getLanguageOptions } from 'benefit/applicant/utils/common'; +import { BackendEndpoint } from 'benefit-shared/backend-api/backend-api'; import { APPLICATION_STATUSES } from 'benefit-shared/constants'; import { useRouter } from 'next/router'; import { TFunction } from 'next-i18next'; import React, { useEffect, useState } from 'react'; +import useBackendAPI from 'shared/hooks/useBackendAPI'; import useToggle from 'shared/hooks/useToggle'; import { NavigationItem, OptionType } from 'shared/types/common'; @@ -27,6 +29,8 @@ const useHeader = (): ExtendedComponentProps => { const { t } = useTranslation(); const router = useRouter(); const id = router?.query?.id?.toString() ?? ''; + const { axios } = useBackendAPI(); + const [hasMessenger, setHasMessenger] = useState(false); const [unreadMessagesCount, setUnredMessagesCount] = useState< number | undefined | null @@ -75,6 +79,10 @@ const useHeader = (): ExtendedComponentProps => { ): void => { e.preventDefault(); + void axios.get(BackendEndpoint.USER_OPTIONS, { + params: { lang: newLanguage.value }, + }); + void router.push({ pathname, query }, asPath, { locale: newLanguage.value, }); diff --git a/frontend/benefit/shared/src/backend-api/backend-api.ts b/frontend/benefit/shared/src/backend-api/backend-api.ts index 898ff1ebf3..f48d65f268 100644 --- a/frontend/benefit/shared/src/backend-api/backend-api.ts +++ b/frontend/benefit/shared/src/backend-api/backend-api.ts @@ -7,6 +7,7 @@ export const BackendEndpoint = { LOGOUT: '/oidc/logout/', USER: '/oidc/userinfo/', USER_ME: 'v1/users/me/', + USER_OPTIONS: 'v1/users/options/', COMPANY: '/v1/company/', APPLICATIONS: '/v1/applications/', APPLICATIONS_SIMPLIFIED: '/v1/applications/simplified_list/',