feature: localization's first implmentation
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useRouter } from '@/i18n/navigation';
|
||||
import { authApi } from '@/lib/api/auth';
|
||||
import { User, Organization } from '@/types/organization';
|
||||
import { isAppLocale } from '@/i18n/routing';
|
||||
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
organizations: Organization[];
|
||||
currentOrganization: Organization | null;
|
||||
isLoading: boolean;
|
||||
isAuthReady: boolean; // ✅ NEW
|
||||
isAuthReady: boolean;
|
||||
error: string | null;
|
||||
registerTrial: (
|
||||
email: string,
|
||||
@@ -29,6 +30,7 @@ interface AuthContextType {
|
||||
organizationType: 'CLINIC' | 'LAB',
|
||||
planName?: string,
|
||||
) => Promise<string>;
|
||||
setUserLanguage: (language: string) => void;
|
||||
clearError: () => void;
|
||||
}
|
||||
|
||||
@@ -48,7 +50,13 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const organizations = payload?.organizations || [];
|
||||
|
||||
if (payload?.user) {
|
||||
return { user: payload.user as User, organizations };
|
||||
return {
|
||||
user: {
|
||||
...(payload.user as User),
|
||||
language: (payload.user as User).language ?? 'en',
|
||||
},
|
||||
organizations,
|
||||
};
|
||||
}
|
||||
|
||||
if (payload?.id && payload?.email && payload?.name) {
|
||||
@@ -57,6 +65,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
id: payload.id,
|
||||
email: payload.email,
|
||||
name: payload.name,
|
||||
language: payload.language ?? 'en',
|
||||
},
|
||||
organizations,
|
||||
};
|
||||
@@ -272,6 +281,11 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const clearError = useCallback(() => setError(null), []);
|
||||
|
||||
const setUserLanguage = useCallback((language: string) => {
|
||||
const normalized = isAppLocale(language) ? language : 'en';
|
||||
setUser((current) => (current ? { ...current, language: normalized } : current));
|
||||
}, []);
|
||||
|
||||
const contextValue = useMemo(
|
||||
() => ({
|
||||
user,
|
||||
@@ -285,6 +299,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
logout,
|
||||
selectOrganization,
|
||||
createOrganization,
|
||||
setUserLanguage,
|
||||
clearError,
|
||||
}),
|
||||
[
|
||||
@@ -299,6 +314,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
logout,
|
||||
selectOrganization,
|
||||
createOrganization,
|
||||
setUserLanguage,
|
||||
clearError,
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user