From f244509de35baf1acaab74c7309d7ff1601fb6c3 Mon Sep 17 00:00:00 2001 From: rvveber Date: Tue, 4 Mar 2025 14:01:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(frontend)=20add=20API=20access=20for?= =?UTF-8?q?=20'language'=20attribute=20on=20User=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - allow the language attribute on the user to be updated via API - add frontend function to update the user language via API - extend defaults on the test users, to have fixed language in E2E tests - extend types and variables using the types with the new field --- .../impress/src/features/auth/api/types.ts | 2 + .../components/DocShareInvitationItem.tsx | 1 + .../doc-share/components/DocShareModal.tsx | 1 + .../language/api/useChangeUserLanguage.tsx | 45 +++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 src/frontend/apps/impress/src/features/language/api/useChangeUserLanguage.tsx diff --git a/src/frontend/apps/impress/src/features/auth/api/types.ts b/src/frontend/apps/impress/src/features/auth/api/types.ts index ef189360..6d911e51 100644 --- a/src/frontend/apps/impress/src/features/auth/api/types.ts +++ b/src/frontend/apps/impress/src/features/auth/api/types.ts @@ -4,10 +4,12 @@ * @property {string} id - The id of the user. * @property {string} email - The email of the user. * @property {string} name - The name of the user. + * @property {string} language - The language of the user. e.g. 'en-us', 'fr-fr', 'de-de'. */ export interface User { id: string; email: string; full_name: string; short_name: string; + language: string; } diff --git a/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareInvitationItem.tsx b/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareInvitationItem.tsx index 6ffc5f33..7f782cc8 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareInvitationItem.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareInvitationItem.tsx @@ -30,6 +30,7 @@ export const DocShareInvitationItem = ({ doc, invitation }: Props) => { full_name: invitation.email, email: invitation.email, short_name: invitation.email, + language: 'en-us', }; const { toast } = useToastProvider(); diff --git a/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareModal.tsx b/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareModal.tsx index 9f26d059..fcc23be0 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareModal.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-share/components/DocShareModal.tsx @@ -132,6 +132,7 @@ export const DocShareModal = ({ doc, onClose }: Props) => { full_name: '', email: userQuery, short_name: '', + language: '', }; const hasEmailInUsers = users.some((user) => user.email === userQuery); diff --git a/src/frontend/apps/impress/src/features/language/api/useChangeUserLanguage.tsx b/src/frontend/apps/impress/src/features/language/api/useChangeUserLanguage.tsx new file mode 100644 index 00000000..a11e1ac5 --- /dev/null +++ b/src/frontend/apps/impress/src/features/language/api/useChangeUserLanguage.tsx @@ -0,0 +1,45 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; + +import { APIError, errorCauses, fetchAPI } from '@/api'; +import { User } from '@/features/auth/api/types'; + +export interface ChangeUserLanguageParams { + userId: User['id']; + language: User['language']; +} + +export const changeUserLanguage = async ({ + userId, + language, +}: ChangeUserLanguageParams): Promise => { + const response = await fetchAPI(`users/${userId}/`, { + method: 'PATCH', + body: JSON.stringify({ + language, + }), + }); + + if (!response.ok) { + throw new APIError( + `Failed to change the user language to ${language}`, + await errorCauses(response, { + value: language, + type: 'language', + }), + ); + } + + return response.json() as Promise; +}; + +export function useChangeUserLanguage() { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: changeUserLanguage, + onSuccess: () => { + void queryClient.invalidateQueries({ + queryKey: ['change-user-language'], + }); + }, + }); +}