add pigs to event categories, fallback to those
This commit is contained in:
@ -27,6 +27,12 @@ export const PIG_NAMES = [
|
||||
"peek",
|
||||
];
|
||||
|
||||
export function randomElement(array: any[]): any | undefined {
|
||||
return array.length
|
||||
? array[Math.floor(Math.random() * array.length)]
|
||||
: undefined;
|
||||
}
|
||||
|
||||
const BlockFragmentDefinition = graphql(`
|
||||
fragment Blocks on StreamFieldInterface {
|
||||
id
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user