added push notifications

This commit is contained in:
2026-06-03 19:13:56 +02:00
parent a9278b8269
commit 6559a9cd4b
24 changed files with 1598 additions and 557 deletions
+17 -7
View File
@@ -3,7 +3,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
import {
authApi,
LoginPayload, RegisterPayload, subscriptionsApi, UserUpdateDTO,
LoginPayload, RegisterPayload, subscriptionsApi, userApi, UserUpdateDTO,
} from "@/services/api";
type User = {
@@ -11,6 +11,7 @@ type User = {
email: string;
token: string;
subscribedSubjectIds: string[];
notificationType: 'PUSH_NOTIFICATION' | 'NO_NOTIFICATION';
};
type AuthContextValue = {
@@ -23,6 +24,7 @@ type AuthContextValue = {
unsubscribe: (subjectId: string) => Promise<void>;
isSubscribed: (subjectId: string) => boolean;
updateUser: (newEmail?: string, newPassword?: string) => Promise<void>;
updateNotificationType: (type: 'PUSH_NOTIFICATION' | 'NO_NOTIFICATION') => Promise<void>;
};
const AuthContext = createContext<AuthContextValue | null>(null);
@@ -43,17 +45,24 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
setUser(u);
};
const updateNotificationType = async (type: 'PUSH_NOTIFICATION' | 'NO_NOTIFICATION') => {
if (!user) return;
await userApi.updateNotificationType(type, user.token);
await persist({ ...user, notificationType: type });
};
const login = async (payload: LoginPayload) => {
const res = await authApi.login(payload);
// Gracefully fetch subscriptions — don't block login if this fails
let subscribedSubjectIds: string[] = [];
let notificationType: 'PUSH_NOTIFICATION' | 'NO_NOTIFICATION' = 'PUSH_NOTIFICATION';
if (res.userId) {
try {
const dto = await authApi.getUser( res.token);
const dto = await authApi.getUser(res.token);
subscribedSubjectIds = dto.subjectSet?.map((s) => s.id) ?? [];
} catch {
// aaaa
notificationType = dto.notificationType ?? 'PUSH_NOTIFICATION';
} catch (e) {
console.error('Failed to load subscriptions on login:', e);
}
}
@@ -62,6 +71,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
email: res.email,
token: res.token,
subscribedSubjectIds,
notificationType,
});
};
@@ -116,7 +126,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
user?.subscribedSubjectIds.includes(subjectId) ?? false;
return (
<AuthContext.Provider value={{ user, loading, login, register, logout, subscribe, unsubscribe, isSubscribed, updateUser }}>
<AuthContext.Provider value={{ user, loading, login, register, logout, subscribe, unsubscribe, isSubscribed, updateUser, updateNotificationType }}>
{children}
</AuthContext.Provider>
);