✨(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:
@@ -4,10 +4,12 @@
|
|||||||
* @property {string} id - The id of the user.
|
* @property {string} id - The id of the user.
|
||||||
* @property {string} email - The email of the user.
|
* @property {string} email - The email of the user.
|
||||||
* @property {string} name - The name 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 {
|
export interface User {
|
||||||
id: string;
|
id: string;
|
||||||
email: string;
|
email: string;
|
||||||
full_name: string;
|
full_name: string;
|
||||||
short_name: string;
|
short_name: string;
|
||||||
|
language: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export const DocShareInvitationItem = ({ doc, invitation }: Props) => {
|
|||||||
full_name: invitation.email,
|
full_name: invitation.email,
|
||||||
email: invitation.email,
|
email: invitation.email,
|
||||||
short_name: invitation.email,
|
short_name: invitation.email,
|
||||||
|
language: 'en-us',
|
||||||
};
|
};
|
||||||
|
|
||||||
const { toast } = useToastProvider();
|
const { toast } = useToastProvider();
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ export const DocShareModal = ({ doc, onClose }: Props) => {
|
|||||||
full_name: '',
|
full_name: '',
|
||||||
email: userQuery,
|
email: userQuery,
|
||||||
short_name: '',
|
short_name: '',
|
||||||
|
language: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasEmailInUsers = users.some((user) => user.email === userQuery);
|
const hasEmailInUsers = users.some((user) => user.email === userQuery);
|
||||||
|
|||||||
@@ -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'],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user