From 42936273bf4a174fe79c0838937370540b2da31f Mon Sep 17 00:00:00 2001 From: Lightling Date: Sat, 29 Jun 2024 13:33:54 -0400 Subject: [PATCH] re-implement amendVariantsWithDefaults --- libs/types/src/content/templates/gallery-list.d.ts | 8 ++------ projects/frontend/src/utilities/fetch.ts | 8 ++++++-- .../frontend/src/views/gallery/gallery-list.vue | 9 +++++---- .../src/views/gallery/gallery-utilities.ts | 14 ++++++++------ .../frontend/src/views/gallery/gallery-view.vue | 7 ++++--- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/libs/types/src/content/templates/gallery-list.d.ts b/libs/types/src/content/templates/gallery-list.d.ts index 66e360f..2d32a45 100644 --- a/libs/types/src/content/templates/gallery-list.d.ts +++ b/libs/types/src/content/templates/gallery-list.d.ts @@ -49,13 +49,13 @@ export type GalleryEntryInheritedProperties = { * and will be displayed with the tile */ warning?: string -} +} & ListWithEntries /** * Defines an entry in a gallery that can be displayed in a tiled manner * and can be clicked by a visitor to display its variants or the entry itself if there are none */ -export type GalleryEntryProperties = GalleryEntryInheritedProperties & { +export type GalleryEntry = GalleryEntryInheritedProperties & { /** * the url to the thumbnail to show for the entry in the gallery tile */ @@ -66,10 +66,6 @@ export type GalleryEntryProperties = GalleryEntryInheritedProperties & { url?: string } -export type GalleryEntry = - & GalleryEntryProperties - & ListWithEntries - /** * Defines the model of the `GalleryList` template */ diff --git a/projects/frontend/src/utilities/fetch.ts b/projects/frontend/src/utilities/fetch.ts index f7e891c..dced840 100644 --- a/projects/frontend/src/utilities/fetch.ts +++ b/projects/frontend/src/utilities/fetch.ts @@ -186,7 +186,7 @@ export const fetchConfigByIdFromList = async (list: ListWithEntries | List export const fetchNestedConfigs = async ( list: ListWithNestedEntries, idsByDepth: string[], - proccessCallback?: (t: T) => T, + proccessCallback?: (parent: ListEntriesWithNestedEntries, child: ListEntriesWithNestedEntries) => ListEntriesWithNestedEntries, ): Promise> => { /* DOCUMENT STRUCTURE: ...listConfig @@ -213,7 +213,11 @@ export const fetchNestedConfigs = async ( let idsToLoop = [ ...idsByDepth ] let current = await fetchConfigByIdFromList(list, idsToLoop.shift()!) as ListEntriesWithNestedEntries if (idsToLoop.length > 0) { - resolve(await fetchNestedConfigs(current as any, idsToLoop)) + let next = await fetchNestedConfigs(current as any, idsToLoop) as ListEntriesWithNestedEntries + if (!!proccessCallback) { + next = proccessCallback(current, next) + } + resolve(next) } else { resolve(current) } diff --git a/projects/frontend/src/views/gallery/gallery-list.vue b/projects/frontend/src/views/gallery/gallery-list.vue index 7e4cf0d..ac6ca38 100644 --- a/projects/frontend/src/views/gallery/gallery-list.vue +++ b/projects/frontend/src/views/gallery/gallery-list.vue @@ -7,7 +7,7 @@ import type { } from '@goldenwere/mackenzii-types' import { type RouteRecordRaw, useRouter } from 'vue-router' -import { amendVariantsWithDefaults } from './gallery-utilities' +import { amendVariantsWithDefaults, _amendVariantWithDefaults } from './gallery-utilities' import { fetchAndParseYaml, fetchNestedConfigs, fetchConfigsFromList, storage } from 'src/utilities/fetch' import { getCurrentRoute } from 'src/utilities/vuetils' import { useRouteStore } from 'src/routes' @@ -62,8 +62,9 @@ const onDisplayEntries = async () => { resetTags() galleryReady.value = false if (!!variants) { - const value = await fetchNestedConfigs(config, variants) - entries.value = (await fetchConfigsFromList(value)).entries + const value = await fetchNestedConfigs(config, variants, _amendVariantWithDefaults) + const children = (await fetchConfigsFromList(value)).entries + entries.value = amendVariantsWithDefaults(value, children) } else { const value = await fetchConfigsFromList(config) entries.value = value.entries @@ -136,7 +137,7 @@ const onHideWarningsToggled = (event: Event) => { }) } else { Object.keys(entries.value).forEach(entryId => { - entries.value[entryId].isHidden = !entries.value[entryId].tags?.some(own => tagsToggled.includes(own)) + entries.value[entryId].isHidden = !entries.value[entryId].tags?.some((own: string) => tagsToggled.includes(own)) }) } } diff --git a/projects/frontend/src/views/gallery/gallery-utilities.ts b/projects/frontend/src/views/gallery/gallery-utilities.ts index cc94245..208156f 100644 --- a/projects/frontend/src/views/gallery/gallery-utilities.ts +++ b/projects/frontend/src/views/gallery/gallery-utilities.ts @@ -13,13 +13,15 @@ export const getTitleFromEntryOrId = (entry: GalleryEntry, id: string) => ( : id ) -export const amendVariantsWithDefaults = (entry: GalleryEntry) => { - // const variants = deepCopy(entry.variants) - // if (!!variants) { - // Object.keys(variants).forEach(id => _amendVariantWithDefaults(entry, variants[id])) - // } +export const amendVariantsWithDefaults = (parent: GalleryEntryInheritedProperties, children: GalleryEntryInheritedProperties) => { + const _children = deepCopy(children) + if (!!_children) { + Object.keys(children).forEach(id => { + _children[id] = _amendVariantWithDefaults(parent, children[id]) + }) + } - // return variants + return _children } export const _amendVariantWithDefaults = (parent: GalleryEntryInheritedProperties, variant: GalleryEntryInheritedProperties) => { diff --git a/projects/frontend/src/views/gallery/gallery-view.vue b/projects/frontend/src/views/gallery/gallery-view.vue index 722c378..0dd904b 100644 --- a/projects/frontend/src/views/gallery/gallery-view.vue +++ b/projects/frontend/src/views/gallery/gallery-view.vue @@ -7,7 +7,7 @@ import type { RoutedWindow, } from '@goldenwere/mackenzii-types' -import { amendVariantsWithDefaults, getTitleFromEntryOrId } from './gallery-utilities' +import { _amendVariantWithDefaults, getTitleFromEntryOrId } from './gallery-utilities' import { fetchAndParseYaml, fetchConfigByIdFromList, fetchNestedConfigs } from 'src/utilities/fetch' import { getCurrentRoute } from 'src/utilities/vuetils' import { useRouteStore } from 'src/routes' @@ -46,8 +46,9 @@ onMounted(async () => { config.value = listConfig let ids = props.variants let viewId = ids.pop()! - let entries = await fetchNestedConfigs(listConfig, ids) - entry.value = await fetchConfigByIdFromList(entries, viewId) + let entries = await fetchNestedConfigs(listConfig, ids, _amendVariantWithDefaults) + let finalEntry = await fetchConfigByIdFromList(entries, viewId) + entry.value = _amendVariantWithDefaults(entries, finalEntry) id.value = props.variants[props.variants.length - 1] title.value = getTitleFromEntryOrId(entry.value, id.value) document.title = routeSubConfig.fullTitle?.replace('$ENTRY', title.value)