-
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.
✨(frontend) sync user and frontend language
On Language change in the frontend, the user language is updated via API. If user language is available, it will be preferred and set in the frontend.
- Loading branch information
Showing
8 changed files
with
128 additions
and
16 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
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