(frontend) add API access for 'language' attribute on User model

- 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
This commit is contained in:
rvveber
2025-03-04 14:01:18 +01:00
committed by Samuel Paccoud
parent fda5f8f008
commit f244509de3
4 changed files with 49 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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<User> => {
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<User>;
};
export function useChangeUserLanguage() {
const queryClient = useQueryClient();
return useMutation<User, APIError, ChangeUserLanguageParams>({
mutationFn: changeUserLanguage,
onSuccess: () => {
void queryClient.invalidateQueries({
queryKey: ['change-user-language'],
});
},
});
}