-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(i18n): 🐛 sync user- and frontend language
- Loading branch information
Showing
13 changed files
with
145 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { UserLanguage } from '@/i18n/types'; | ||
|
||
/** | ||
* Represents user retrieved from the API. | ||
* @interface User | ||
* @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. | ||
*/ | ||
export interface User { | ||
id: string; | ||
email: string; | ||
full_name: string; | ||
short_name: string; | ||
language: UserLanguage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/frontend/apps/impress/src/features/language/api/useChangeUserLanguage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { APIError, errorCauses, fetchAPI } from '@/api'; | ||
import { User, useAuthStore } from '@/core'; | ||
|
||
interface SetUserLanguageParams { | ||
language: User['language']; | ||
} | ||
|
||
export const setUserLanguage = async ({ | ||
language, | ||
}: SetUserLanguageParams): Promise<User> => { | ||
const { userData } = useAuthStore.getState(); | ||
|
||
if (!userData?.id) { | ||
console.warn('Id of user is needed for this request.'); | ||
return {} as Promise<User>; | ||
} | ||
|
||
const response = await fetchAPI(`users/${userData.id}/`, { | ||
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 useSetUserLanguage() { | ||
const queryClient = useQueryClient(); | ||
return useMutation<User, APIError, SetUserLanguageParams>({ | ||
mutationFn: setUserLanguage, | ||
onSuccess: () => { | ||
void queryClient.invalidateQueries({ | ||
queryKey: ['set-user-language'], | ||
}); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// See: https://github.com/numerique-gouv/impress/blob/ac58341984c99c10ebfac7f8bbe1e8756c48e4d4/src/backend/impress/settings.py#L156-L161 | ||
export type ContentLanguage = 'en-us' | 'fr-fr'; | ||
export type UserLanguage = 'en-us' | 'fr-fr'; |