web: colocate graphql fragments, unmask where needed, more idiomatic client-preset use
This commit is contained in:
+18
-68
@@ -97,72 +97,28 @@ const LeafBlocksFragmentDefinition = graphql(`
|
||||
blockType
|
||||
field
|
||||
... on RichTextBlock {
|
||||
rawValue
|
||||
value
|
||||
...RichTextBlock
|
||||
}
|
||||
... on ImageWithTextBlock {
|
||||
image {
|
||||
...Image
|
||||
}
|
||||
imageFormat
|
||||
text
|
||||
...ImageWithTextBlock
|
||||
}
|
||||
... on ImageSliderBlock {
|
||||
images {
|
||||
... on ImageSliderItemBlock {
|
||||
image {
|
||||
...Image
|
||||
}
|
||||
text
|
||||
}
|
||||
}
|
||||
...ImageSliderBlock
|
||||
}
|
||||
... on HorizontalRuleBlock {
|
||||
color
|
||||
...HorizontalRuleBlock
|
||||
}
|
||||
... on FeaturedBlock {
|
||||
title
|
||||
featuredBlockText: text
|
||||
linkText
|
||||
imagePosition
|
||||
backgroundColor
|
||||
featuredPage {
|
||||
contentType
|
||||
pageType
|
||||
url
|
||||
... on EventPage {
|
||||
featuredImage {
|
||||
...Image
|
||||
}
|
||||
}
|
||||
... on NewsPage {
|
||||
featuredImage {
|
||||
...Image
|
||||
}
|
||||
}
|
||||
}
|
||||
featuredImageOverride {
|
||||
...Image
|
||||
}
|
||||
...FeaturedBlock
|
||||
}
|
||||
... on ContactListBlock {
|
||||
items {
|
||||
blockType
|
||||
... on ContactEntityBlock {
|
||||
contactEntity {
|
||||
...ContactEntity
|
||||
}
|
||||
}
|
||||
}
|
||||
...ContactListBlock
|
||||
}
|
||||
... on EmbedBlock {
|
||||
url
|
||||
embed
|
||||
rawEmbed
|
||||
...EmbedBlock
|
||||
}
|
||||
... on FactBoxBlock {
|
||||
backgroundColor
|
||||
factBoxBody: body
|
||||
...FactBoxBlock
|
||||
}
|
||||
}
|
||||
`);
|
||||
@@ -171,15 +127,13 @@ const OneLevelOfBlocksFragmentDefinition = graphql(`
|
||||
fragment OneLevelOfBlocks on StreamFieldInterface {
|
||||
...LeafBlocks
|
||||
... on AccordionBlock {
|
||||
heading
|
||||
...AccordionBlock
|
||||
body {
|
||||
...LeafBlocks
|
||||
}
|
||||
}
|
||||
... on PageSectionBlock {
|
||||
title
|
||||
backgroundColor
|
||||
icon
|
||||
...PageSectionBlock
|
||||
body {
|
||||
...LeafBlocks
|
||||
}
|
||||
@@ -189,39 +143,35 @@ const OneLevelOfBlocksFragmentDefinition = graphql(`
|
||||
|
||||
const BlockFragmentDefinition = graphql(`
|
||||
fragment Blocks on StreamFieldInterface {
|
||||
...OneLevelOfBlocks
|
||||
... on AccordionBlock {
|
||||
heading
|
||||
...AccordionBlock
|
||||
body {
|
||||
...OneLevelOfBlocks
|
||||
}
|
||||
}
|
||||
... on PageSectionBlock {
|
||||
title
|
||||
backgroundColor
|
||||
icon
|
||||
...PageSectionBlock
|
||||
body {
|
||||
...OneLevelOfBlocks
|
||||
}
|
||||
}
|
||||
... on ContactSectionBlock {
|
||||
title
|
||||
text
|
||||
...ContactSectionBlock
|
||||
blocks {
|
||||
...OneLevelOfBlocks
|
||||
... on ContactSubsectionBlock {
|
||||
title
|
||||
text
|
||||
...ContactSubsectionBlock
|
||||
blocks {
|
||||
...OneLevelOfBlocks
|
||||
}
|
||||
}
|
||||
...OneLevelOfBlocks
|
||||
}
|
||||
}
|
||||
...OneLevelOfBlocks
|
||||
}
|
||||
`);
|
||||
|
||||
const ImageFragmentDefinition = graphql(`
|
||||
export const ImageFragmentDefinition = graphql(`
|
||||
fragment Image on CustomImage {
|
||||
id
|
||||
url
|
||||
@@ -232,7 +182,7 @@ const ImageFragmentDefinition = graphql(`
|
||||
}
|
||||
`);
|
||||
|
||||
const ContactEntityFragmentDefinition = graphql(`
|
||||
export const ContactEntityFragmentDefinition = graphql(`
|
||||
fragment ContactEntity on ContactEntity {
|
||||
id
|
||||
name
|
||||
|
||||
+44
-27
@@ -8,21 +8,48 @@ import {
|
||||
parseISO,
|
||||
} from "date-fns";
|
||||
import { toLocalTime, formatDate, compareDates } from "./date";
|
||||
import { graphql } from "@/gql";
|
||||
import { EventFragment, EventOccurrence } from "@/gql/graphql";
|
||||
import { graphql, unmaskFragment } from "@/gql";
|
||||
import {
|
||||
type EventCategoryFragment,
|
||||
type EventFragment,
|
||||
type EventOrganizerFragment,
|
||||
} from "@/gql/graphql";
|
||||
import { PIG_NAMES, randomElement } from "@/lib/common";
|
||||
|
||||
export type {
|
||||
EventFragment,
|
||||
EventCategory,
|
||||
EventOccurrence,
|
||||
EventOrganizer,
|
||||
} from "@/gql/graphql";
|
||||
export type EventOccurrence = EventFragment["occurrences"][number];
|
||||
export type EventCategory = EventCategoryFragment;
|
||||
export type EventOrganizer = EventOrganizerFragment;
|
||||
export type { EventFragment };
|
||||
|
||||
export type SingularEvent = EventFragment & {
|
||||
occurrence: EventOccurrence;
|
||||
};
|
||||
|
||||
export const EventCategoryFragmentDefinition = graphql(`
|
||||
fragment EventCategory on EventCategory {
|
||||
__typename
|
||||
name
|
||||
slug
|
||||
pig
|
||||
showInFilters
|
||||
}
|
||||
`);
|
||||
|
||||
export const EventOrganizerFragmentDefinition = graphql(`
|
||||
fragment EventOrganizer on EventOrganizer {
|
||||
__typename
|
||||
id
|
||||
name
|
||||
slug
|
||||
externalUrl
|
||||
association {
|
||||
... on AssociationPage {
|
||||
url
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
const EventFragmentDefinition = graphql(`
|
||||
fragment Event on EventPage {
|
||||
__typename
|
||||
@@ -48,9 +75,7 @@ const EventFragmentDefinition = graphql(`
|
||||
priceStudent
|
||||
categories {
|
||||
... on EventCategory {
|
||||
name
|
||||
slug
|
||||
pig
|
||||
...EventCategory
|
||||
}
|
||||
}
|
||||
occurrences(limit: 5000) {
|
||||
@@ -72,15 +97,7 @@ const EventFragmentDefinition = graphql(`
|
||||
}
|
||||
organizers {
|
||||
... on EventOrganizer {
|
||||
id
|
||||
name
|
||||
slug
|
||||
externalUrl
|
||||
association {
|
||||
... on AssociationPage {
|
||||
url
|
||||
}
|
||||
}
|
||||
...EventOrganizer
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,16 +142,12 @@ export const eventsOverviewQuery = graphql(`
|
||||
}
|
||||
eventCategories: eventCategories(limit: 5000) {
|
||||
... on EventCategory {
|
||||
name
|
||||
slug
|
||||
showInFilters
|
||||
...EventCategory
|
||||
}
|
||||
}
|
||||
eventOrganizers: eventOrganizers(limit: 5000) {
|
||||
... on EventOrganizer {
|
||||
id
|
||||
name
|
||||
slug
|
||||
...EventOrganizer
|
||||
}
|
||||
}
|
||||
venues: pages(contentType: "venues.VenuePage") {
|
||||
@@ -270,7 +283,11 @@ export function getEventPig(event: EventFragment): string | null {
|
||||
return event.pig;
|
||||
}
|
||||
if (event.pig === "automatic") {
|
||||
const categoryPigs = event.categories
|
||||
const categories = unmaskFragment(
|
||||
EventCategoryFragmentDefinition,
|
||||
event.categories
|
||||
);
|
||||
const categoryPigs = categories
|
||||
?.map((category) => category.pig)
|
||||
.filter((pig) => PIG_NAMES.includes(pig));
|
||||
const chosenPig = randomElement(categoryPigs ?? []);
|
||||
|
||||
+28
-23
@@ -1,19 +1,18 @@
|
||||
import {
|
||||
startOfToday,
|
||||
isAfter,
|
||||
getISODay,
|
||||
parseISO,
|
||||
isSameDay,
|
||||
compareDesc,
|
||||
getISODay,
|
||||
isAfter,
|
||||
isSameDay,
|
||||
parseISO,
|
||||
startOfToday,
|
||||
} from "date-fns";
|
||||
|
||||
import { graphql } from "@/gql";
|
||||
import {
|
||||
OpeningHoursRangeBlock,
|
||||
OpeningHoursSet,
|
||||
OpeningHoursWeekBlock,
|
||||
} from "@/gql/graphql";
|
||||
import { getClient } from "@/app/client";
|
||||
import { graphql, unmaskFragment } from "@/gql";
|
||||
import type {
|
||||
OpeningHoursRangeBlockFragment as OpeningHoursRangeBlock,
|
||||
OpeningHoursSetFragment as OpeningHoursSet,
|
||||
} from "@/gql/graphql";
|
||||
|
||||
const MISSING_OPENING_HOURS = {
|
||||
name: "Åpningstider mangler",
|
||||
@@ -45,7 +44,7 @@ const WEEKDAYS_NORWEGIAN = [
|
||||
const openingHoursQuery = graphql(`
|
||||
query openingHoursSets {
|
||||
openingHoursSets {
|
||||
...OpeningHoursSetFragment
|
||||
...OpeningHoursSet
|
||||
}
|
||||
}
|
||||
`);
|
||||
@@ -79,7 +78,7 @@ export async function getOpeningHours() {
|
||||
}
|
||||
// pick the set that msot recently took effect
|
||||
return validSets.sort((a, b) =>
|
||||
compareDesc(a.effectiveFrom, b.effectiveFrom)
|
||||
compareDesc(a.effectiveFrom, b.effectiveFrom),
|
||||
)[0];
|
||||
}
|
||||
|
||||
@@ -93,7 +92,7 @@ type OpeningHoursGroup = {
|
||||
type OpeningHoursPerDay = Record<string, OpeningHoursRangeBlock>;
|
||||
|
||||
export function groupOpeningHours(
|
||||
week: OpeningHoursPerDay
|
||||
week: OpeningHoursPerDay,
|
||||
): OpeningHoursGroup[] {
|
||||
const grouped: OpeningHoursGroup[] = [];
|
||||
let previous: string | null = null;
|
||||
@@ -132,7 +131,7 @@ export type PrettyOpeningHours = {
|
||||
};
|
||||
|
||||
function formatGroupedHours(
|
||||
grouped: OpeningHoursGroup[]
|
||||
grouped: OpeningHoursGroup[],
|
||||
): PrettyOpeningHours[] {
|
||||
return grouped.map((group) => {
|
||||
const startDayIndex = WEEKDAYS.indexOf(group.days[0]);
|
||||
@@ -160,19 +159,26 @@ function formatGroupedHours(
|
||||
|
||||
export function getOpeningHoursForFunction(
|
||||
openingHours: OpeningHoursSet,
|
||||
name: string
|
||||
name: string,
|
||||
) {
|
||||
const item = openingHours.items?.find((x) => x?.function === name);
|
||||
if (!item || !Array.isArray(item?.week) || item?.week.length !== 1) {
|
||||
return;
|
||||
}
|
||||
const week = item.week[0] as OpeningHoursWeekBlock;
|
||||
const maskedWeek = item.week[0];
|
||||
if (maskedWeek?.__typename !== "OpeningHoursWeekBlock") {
|
||||
return;
|
||||
}
|
||||
const week = unmaskFragment(
|
||||
OpeningHoursWeekBlockFragmentDefinition,
|
||||
maskedWeek,
|
||||
);
|
||||
return week;
|
||||
}
|
||||
|
||||
export function getPrettyOpeningHoursForFunction(
|
||||
openingHours: OpeningHoursSet,
|
||||
name: string
|
||||
name: string,
|
||||
) {
|
||||
const week = getOpeningHoursForFunction(openingHours, name);
|
||||
if (!week) {
|
||||
@@ -194,7 +200,7 @@ export function getPrettyOpeningHoursForFunction(
|
||||
|
||||
export function getTodaysOpeningHoursForFunction(
|
||||
openingHours: OpeningHoursSet,
|
||||
name: string
|
||||
name: string,
|
||||
): string {
|
||||
const week: any = getOpeningHoursForFunction(openingHours, name);
|
||||
if (!week) {
|
||||
@@ -213,7 +219,7 @@ export function getTodaysOpeningHoursForFunction(
|
||||
}
|
||||
|
||||
const OpeningHoursSetFragmentDefinition = graphql(`
|
||||
fragment OpeningHoursSetFragment on OpeningHoursSet {
|
||||
fragment OpeningHoursSet on OpeningHoursSet {
|
||||
name
|
||||
effectiveFrom
|
||||
effectiveTo
|
||||
@@ -222,8 +228,7 @@ const OpeningHoursSetFragmentDefinition = graphql(`
|
||||
id
|
||||
function
|
||||
week {
|
||||
id
|
||||
blockType
|
||||
__typename
|
||||
... on OpeningHoursWeekBlock {
|
||||
...OpeningHoursWeekBlock
|
||||
}
|
||||
@@ -240,7 +245,7 @@ const OpeningHoursRangeBlockFragmentDefinition = graphql(`
|
||||
}
|
||||
`);
|
||||
|
||||
const OpeningHoursWeekBlockFragmentDefinition = graphql(`
|
||||
export const OpeningHoursWeekBlockFragmentDefinition = graphql(`
|
||||
fragment OpeningHoursWeekBlock on OpeningHoursWeekBlock {
|
||||
monday {
|
||||
... on OpeningHoursRangeBlock {
|
||||
|
||||
Reference in New Issue
Block a user