Added main functionality
This commit is contained in:
@@ -13,7 +13,7 @@ interface SectionData {
|
||||
}
|
||||
|
||||
export function BirthdayList() {
|
||||
const { birthdays } = useBirthdays();
|
||||
const { birthdays, deleteBirthday } = useBirthdays();
|
||||
const groupedData = groupBirthdaysByDate(birthdays);
|
||||
|
||||
return (
|
||||
@@ -22,7 +22,12 @@ export function BirthdayList() {
|
||||
sections={groupedData}
|
||||
keyExtractor={(item) => item.id}
|
||||
renderItem={({ item }) => (
|
||||
<BirthdayItem name={item.name} date={item.date} />
|
||||
<BirthdayItem
|
||||
id={item.id}
|
||||
name={item.name}
|
||||
date={item.date}
|
||||
onDelete={deleteBirthday}
|
||||
/>
|
||||
)}
|
||||
style={styles.list}
|
||||
contentContainerStyle={styles.listContent}
|
||||
@@ -48,8 +53,23 @@ export function BirthdayList() {
|
||||
// Helper function to group and sort
|
||||
function groupBirthdaysByDate(data: BirthdayEntry[]): SectionData[] {
|
||||
// Sort by date
|
||||
const today = new Date();
|
||||
const currentYear = today.getFullYear();
|
||||
|
||||
const daysUntilNext = (month: number, day: number) => {
|
||||
let target = new Date(currentYear, month, day);
|
||||
if(target < today.setHours(0, 0, 0, 0)){
|
||||
target = new Date(currentYear + 1, month, day);
|
||||
}
|
||||
const msPerDay = 1000 * 60 * 60 * 24;
|
||||
const diff = target - new Date(today.setHours(0,0,0,0));
|
||||
return Math.ceil(diff / msPerDay);
|
||||
};
|
||||
|
||||
const sorted = [...data].sort((a, b) => {
|
||||
return parseBirthdayDate(a.date).getTime() - parseBirthdayDate(b.date).getTime();
|
||||
const dateA = parseBirthdayDate(a);
|
||||
const dateB = parseBirthdayDate(b);
|
||||
return daysUntilNext(dateA.getMonth(), dateA.getDate()) - daysUntilNext(dateB.getMonth(), dateB.getDate());
|
||||
});
|
||||
|
||||
// Group by date
|
||||
|
||||
Reference in New Issue
Block a user