add pigs to event categories, fallback to those

This commit is contained in:
2024-06-06 02:47:00 +02:00
parent 0d58eeb040
commit ffacc0a8a8
7 changed files with 105 additions and 23 deletions

View File

@ -11,6 +11,7 @@ import {
import { toLocalTime, formatDate } from "./date";
import { graphql } from "@/gql";
import { EventFragment, EventCategory, EventOccurrence } from "@/gql/graphql";
import { PIG_NAMES, PigName, randomElement } from "@/lib/common";
export type {
EventFragment,
@ -51,6 +52,7 @@ const EventFragmentDefinition = graphql(`
... on EventCategory {
name
slug
pig
}
}
occurrences {
@ -181,3 +183,20 @@ export function getClosestOccurrence(event: EventFragment) {
return futureOccurrences[0];
}
export function getEventPig(event: EventFragment): string | null {
if (event.pig === "" || typeof event.pig !== "string") {
return null;
}
if (PIG_NAMES.includes(event.pig)) {
return event.pig;
}
if (event.pig === "automatic") {
const categoryPigs = event.categories
?.map((category) => category.pig)
.filter((pig) => PIG_NAMES.includes(pig));
const chosenPig = randomElement(categoryPigs ?? []);
return typeof chosenPig === "string" ? chosenPig : null;
}
return null;
}