Works
This commit is contained in:
70
apk/context/birthdays-context.tsx
Normal file
70
apk/context/birthdays-context.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
createContext,
|
||||
ReactNode,
|
||||
useContext,
|
||||
useMemo,
|
||||
useState,
|
||||
} from "react";
|
||||
|
||||
export interface BirthdayEntry {
|
||||
id: string;
|
||||
name: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
interface AddBirthdayInput {
|
||||
name: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
interface BirthdaysContextValue {
|
||||
birthdays: BirthdayEntry[];
|
||||
addBirthday: (input: AddBirthdayInput) => void;
|
||||
}
|
||||
|
||||
const BirthdaysContext = createContext<BirthdaysContextValue | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
export function BirthdaysProvider({ children }: { children: ReactNode }) {
|
||||
const [birthdays, setBirthdays] = useState<BirthdayEntry[]>([]);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
birthdays,
|
||||
addBirthday: ({ name, date }: AddBirthdayInput) => {
|
||||
const trimmedName = name.trim();
|
||||
|
||||
if (!trimmedName) {
|
||||
return;
|
||||
}
|
||||
|
||||
setBirthdays((currentBirthdays) => [
|
||||
...currentBirthdays,
|
||||
{
|
||||
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
name: trimmedName,
|
||||
date,
|
||||
},
|
||||
]);
|
||||
},
|
||||
}),
|
||||
[birthdays]
|
||||
);
|
||||
|
||||
return (
|
||||
<BirthdaysContext.Provider value={value}>
|
||||
{children}
|
||||
</BirthdaysContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useBirthdays() {
|
||||
const context = useContext(BirthdaysContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error("useBirthdays must be used within a BirthdaysProvider");
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user