Skip to content

Commit

Permalink
feat: send language to backend on language selection
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast committed Aug 17, 2023
1 parent 16204b4 commit f94eb18
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/benefit/helsinkibenefit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -70,6 +70,7 @@
path("v1/company/search/<str:name>/", SearchOrganisationsView.as_view()),
path("v1/company/get/<str:business_id>/", GetOrganisationByIdView.as_view()),
path("v1/users/me/", CurrentUserView.as_view()),
path("v1/users/options/", UserOptionsView.as_view()),
path(
"v1/handlerapplications/<str:application_id>/review/", ReviewStateView.as_view()
),
Expand Down
20 changes: 20 additions & 0 deletions backend/benefit/users/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."""

Expand Down
8 changes: 8 additions & 0 deletions frontend/benefit/applicant/src/components/header/useHeader.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<boolean>(false);
const [unreadMessagesCount, setUnredMessagesCount] = useState<
number | undefined | null
Expand Down Expand Up @@ -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,
});
Expand Down
1 change: 1 addition & 0 deletions frontend/benefit/shared/src/backend-api/backend-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down

0 comments on commit f94eb18

Please sign in to comment.