Skip to content

Commit

Permalink
refactor: move logic into getLatestRuleSet
Browse files Browse the repository at this point in the history
  • Loading branch information
brage-andreas committed Sep 13, 2024
1 parent b582de3 commit ed177f0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/profile/components/Settings/Penalties/Rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 (
<>
Expand Down

0 comments on commit ed177f0

Please sign in to comment.