From 7ea2508185ae96582eae74561970bf771d827b93 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Sun, 31 Dec 2023 16:07:34 +0100 Subject: [PATCH] add month titles on birthdays list --- src/App.tsx | 5 ++++ src/components/BirthdayList.tsx | 45 +++++++++++++++++++-------------- src/utilities.ts | 2 ++ 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 1fa3fe2..e366641 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -38,6 +38,11 @@ function App() { result.push(item) return result }, []) + .sort((a, b) => { + if (b.birthday < a.birthday) return 1 + if (b.birthday > a.birthday) return -1 + return 0 + }) return birthdays } diff --git a/src/components/BirthdayList.tsx b/src/components/BirthdayList.tsx index b9ed4cc..be84b85 100644 --- a/src/components/BirthdayList.tsx +++ b/src/components/BirthdayList.tsx @@ -1,5 +1,6 @@ -import {FC} from "react"; +import {FC, ReactNode} from "react"; import {Birthday} from "../types.ts"; +import {MONTHS} from "../utilities.ts"; const BirthdayList: FC = ({birthdays, selectedPerson, selectPerson}) => { @@ -9,26 +10,32 @@ const BirthdayList: FC = ({birthdays, selectedPerson, selectP } return
    - {birthdays.sort((a, b) => { - if (b.birthday < a.birthday) return 1 - if (b.birthday > a.birthday) return -1 - return 0 - }) - .map(birthday =>
  • -
    selectPerson(birthday)}> - {birthday.name} -
    -
    -
    -
    {birthday.day} / {birthday.month}
    -
    {birthday.year ? birthday.year : null}
    + {birthdays.map((birthday, index) => { + const content: ReactNode[] = [] + if (index === 0 || (index > 0 && birthdays[index].month !== birthdays[index - 1].month)) { + content.push(
  • + {MONTHS[birthday.month - 1]} +
  • ) + } + content.push(
  • +
    selectPerson(birthday)}> + {birthday.name}
    -
    - {birthday.year ? age(birthday) + ' ans' : null}
    +
    +
    +
    {birthday.day} / {birthday.month}
    +
    {birthday.year ? birthday.year : null}
    +
    +
    + {birthday.year ? age(birthday) + ' ans' : null}
    -
  • )} + ) + + return content + })}
} diff --git a/src/utilities.ts b/src/utilities.ts index 04e3490..bc65b01 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -9,3 +9,5 @@ export const arrayRange = (end: number, start = 0) => { export const pad = (number: number, pad = 2, char = '0') => { return (new Array(pad).join(char) + number).slice(-pad) } + +export const MONTHS = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'] \ No newline at end of file