Added main functionality

This commit is contained in:
2026-04-21 23:24:54 +02:00
parent cb7555590f
commit e8e902119b
7 changed files with 545 additions and 39 deletions

View File

@@ -2,10 +2,13 @@ import {
createContext,
ReactNode,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { syncBirthdayNotifications } from "@/utils/birthday-notifications";
export interface BirthdayEntry {
id: string;
name: string;
@@ -20,14 +23,70 @@ interface AddBirthdayInput {
interface BirthdaysContextValue {
birthdays: BirthdayEntry[];
addBirthday: (input: AddBirthdayInput) => void;
deleteBirthday: (id: string) => void;
}
const STORAGE_KEY = "birthdays";
const BirthdaysContext = createContext<BirthdaysContextValue | undefined>(
undefined
);
export function BirthdaysProvider({ children }: { children: ReactNode }) {
const [birthdays, setBirthdays] = useState<BirthdayEntry[]>([]);
const [isLoaded, setIsLoaded] = useState(false);
// Load birthdays on app start from storage
useEffect(() => {
const loadBirthdays = async () => {
try {
const stored = await AsyncStorage.getItem(STORAGE_KEY);
if (stored) {
setBirthdays(JSON.parse(stored));
}
} catch(e) {
console.log("Error loading birthdays", e);
} finally {
setIsLoaded(true);
}
};
loadBirthdays();
}, []);
// Save birthdays whenever they change
useEffect(() => {
if (!isLoaded) return;
const saveBirthdays = async () => {
try {
await AsyncStorage.setItem(
STORAGE_KEY,
JSON.stringify(birthdays)
);
} catch(e) {
console.log("Error saving birthdays", e);
}
};
saveBirthdays();
}, [birthdays, isLoaded]);
useEffect(() => {
if (!isLoaded) {
return;
}
const synchronizeNotifications = async () => {
try {
await syncBirthdayNotifications(birthdays);
} catch (error) {
console.log("Error syncing birthday notifications", error);
}
};
synchronizeNotifications();
}, [birthdays, isLoaded]);
const value = useMemo(
() => ({
@@ -48,6 +107,11 @@ export function BirthdaysProvider({ children }: { children: ReactNode }) {
},
]);
},
deleteBirthday: (id: string) => {
setBirthdays((currentBirthdays) =>
currentBirthdays.filter((birthday) => birthday.id !== id)
);
},
}),
[birthdays]
);