Merge branch 'master' into feature/tab-warning-flag
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { apiClient } from './client';
|
||||
import type { AppointmentColumnProvider, AppointmentRecord } from '@/types/appointment';
|
||||
import { toDateInputValue } from '@/components/appointments/appointmentTime';
|
||||
|
||||
export interface CreateAppointmentBody {
|
||||
patientId: string;
|
||||
@@ -12,8 +13,11 @@ export interface CreateAppointmentBody {
|
||||
export type UpdateAppointmentBody = Partial<CreateAppointmentBody>;
|
||||
|
||||
export const appointmentsApi = {
|
||||
columnProviders: async (): Promise<{ success: boolean; data: AppointmentColumnProvider[] }> => {
|
||||
const response = await apiClient.get('/appointments/column-providers');
|
||||
columnProviders: async (
|
||||
scheduleDate?: Date,
|
||||
): Promise<{ success: boolean; data: AppointmentColumnProvider[] }> => {
|
||||
const params = scheduleDate ? { date: toDateInputValue(scheduleDate) } : undefined;
|
||||
const response = await apiClient.get('/appointments/column-providers', { params });
|
||||
return response.data;
|
||||
},
|
||||
|
||||
|
||||
@@ -53,6 +53,13 @@ export const authApi = {
|
||||
await apiClient.post('/auth/logout');
|
||||
},
|
||||
|
||||
updateLanguage: async (
|
||||
language: string,
|
||||
): Promise<{ success: boolean; data: { user: { language: string } } }> => {
|
||||
const response = await apiClient.patch('/auth/profile/language', { language });
|
||||
return response.data;
|
||||
},
|
||||
|
||||
// Refresh token
|
||||
refreshToken: async (refreshToken: string): Promise<{ accessToken: string }> => {
|
||||
const response = await apiClient.post('/auth/refresh', { refreshToken });
|
||||
|
||||
@@ -120,4 +120,29 @@ export const staffApi = {
|
||||
const response = await apiClient.delete(`/staff/members/${membershipId}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getWorkingHours: async (
|
||||
membershipId: string,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
data: {
|
||||
autoRepeatWeekly: boolean;
|
||||
blocks: { dayOfWeek: number; startMinute: number; endMinute: number; sortOrder?: number }[];
|
||||
hasWorkingHours: boolean;
|
||||
};
|
||||
}> => {
|
||||
const response = await apiClient.get(`/staff/members/${membershipId}/working-hours`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
upsertWorkingHours: async (
|
||||
membershipId: string,
|
||||
body: {
|
||||
autoRepeatWeekly: boolean;
|
||||
blocks: { dayOfWeek: number; startMinute: number; endMinute: number; sortOrder?: number }[];
|
||||
},
|
||||
): Promise<{ success: boolean; message: string }> => {
|
||||
const response = await apiClient.put(`/staff/members/${membershipId}/working-hours`, body);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useRouter } from '@/i18n/navigation';
|
||||
import { authApi } from '@/lib/api/auth';
|
||||
import { User, Organization } from '@/types/organization';
|
||||
import { isAppLocale, getLocaleFromPathname } 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,12 +31,14 @@ interface AuthContextType {
|
||||
organizationType: 'CLINIC' | 'LAB',
|
||||
planName?: string,
|
||||
) => Promise<string>;
|
||||
setUserLanguage: (language: string) => void;
|
||||
clearError: () => void;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const t = useTranslations('auth');
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [organizations, setOrganizations] = useState<Organization[]>([]);
|
||||
const [currentOrganization, setCurrentOrganization] = useState<Organization | null>(null);
|
||||
@@ -48,7 +52,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 +67,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
id: payload.id,
|
||||
email: payload.email,
|
||||
name: payload.name,
|
||||
language: payload.language ?? 'en',
|
||||
},
|
||||
organizations,
|
||||
};
|
||||
@@ -112,6 +123,23 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
void checkAuth();
|
||||
}, [checkAuth]);
|
||||
|
||||
const applyUrlLocaleToUser = useCallback(async (user: User): Promise<User> => {
|
||||
if (typeof window === 'undefined') return user;
|
||||
|
||||
const urlLocale = getLocaleFromPathname(window.location.pathname);
|
||||
if (!isAppLocale(urlLocale) || user.language === urlLocale) {
|
||||
return user;
|
||||
}
|
||||
|
||||
try {
|
||||
await authApi.updateLanguage(urlLocale);
|
||||
} catch {
|
||||
/* keep URL locale in client state even if persistence fails */
|
||||
}
|
||||
|
||||
return { ...user, language: urlLocale };
|
||||
}, []);
|
||||
|
||||
// ✅ REGISTER
|
||||
const registerTrial = useCallback(async (
|
||||
email: string,
|
||||
@@ -134,7 +162,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
organizationType,
|
||||
});
|
||||
|
||||
setUser(response.data.user);
|
||||
const userData = await applyUrlLocaleToUser(response.data.user);
|
||||
setUser(userData);
|
||||
setOrganizations(response.data.organizations);
|
||||
|
||||
const orgs = response.data.organizations;
|
||||
@@ -151,12 +180,12 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Registration failed');
|
||||
setError(err.message || t('errorRegistrationFailed'));
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [router]);
|
||||
}, [applyUrlLocaleToUser, router, t]);
|
||||
|
||||
// ✅ LOGIN
|
||||
const login = useCallback(async (email: string, password: string) => {
|
||||
@@ -166,7 +195,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const response = await authApi.login({ email, password });
|
||||
|
||||
setUser(response.data.user);
|
||||
const userData = await applyUrlLocaleToUser(response.data.user);
|
||||
setUser(userData);
|
||||
setOrganizations(response.data.organizations);
|
||||
|
||||
const orgs = response.data.organizations;
|
||||
@@ -182,17 +212,17 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Login failed');
|
||||
setError(err.message || t('errorLoginFailed'));
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [router]);
|
||||
}, [applyUrlLocaleToUser, router, t]);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
try {
|
||||
// Important: clear auth cookies/session on the server first,
|
||||
// otherwise middleware may still treat the user as authenticated.
|
||||
// otherwise proxy may still treat the user as authenticated.
|
||||
await authApi.logout();
|
||||
} catch (err) {
|
||||
console.error('Logout API failed:', err);
|
||||
@@ -263,15 +293,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
return createResponse.data.organization.id as string;
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Failed to create organization');
|
||||
setError(err.message || t('errorCreateOrganization'));
|
||||
throw err;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [normalizeProfilePayload]);
|
||||
}, [normalizeProfilePayload, t]);
|
||||
|
||||
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 +320,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
logout,
|
||||
selectOrganization,
|
||||
createOrganization,
|
||||
setUserLanguage,
|
||||
clearError,
|
||||
}),
|
||||
[
|
||||
@@ -299,6 +335,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
logout,
|
||||
selectOrganization,
|
||||
createOrganization,
|
||||
setUserLanguage,
|
||||
clearError,
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user