From ed177f0da026666836585d9404d8b080e45ba0d0 Mon Sep 17 00:00:00 2001 From: brage-andreas Date: Fri, 13 Sep 2024 14:03:01 +0200 Subject: [PATCH] refactor: move logic into `getLatestRuleSet` --- .../components/Settings/Penalties/Rules.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/profile/components/Settings/Penalties/Rules.tsx b/src/profile/components/Settings/Penalties/Rules.tsx index c5f1dc4f..3011da91 100644 --- a/src/profile/components/Settings/Penalties/Rules.tsx +++ b/src/profile/components/Settings/Penalties/Rules.tsx @@ -12,9 +12,19 @@ interface IProps { rules: IMarkRule[]; } +const NO_RULES = 'Det er ingen prikkeregler enda. Kontakt styret for mer informasjon.'; + const inFuture = (date: string) => new Date(date) > new Date(); -const getLatestRuleSet = (rules: IMarkRule[]): IMarkRule => { +const getLatestRuleSet = (rules: IMarkRule[]): IMarkRule | null => { + if (rules.length === 0) { + return null; + } + + if (rules.length === 1) { + return rules[0]; + } + return rules.reduce((latest, rule) => { if (inFuture(rule.valid_from_date)) { return latest; @@ -32,9 +42,7 @@ export const Info = ({ rules }: IProps) => { const [collapsed, toggleCollapse] = useCollapse(hash !== rulesId); - const { content: ruleset } = rules.length - ? getLatestRuleSet(rules) - : { content: 'Det er ingen prikkeregler enda. Kontakt styret for mer informasjon.' }; + const ruleset = getLatestRuleSet(rules)?.content ?? NO_RULES; return ( <>