From c9d87bc3911747b79f81017d0fd6e5b782d929b1 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 11 Oct 2024 12:10:47 +0000 Subject: [PATCH] get nested property --- src/utils/api.tsx | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/utils/api.tsx b/src/utils/api.tsx index dbacaec..ca5ff07 100644 --- a/src/utils/api.tsx +++ b/src/utils/api.tsx @@ -8,7 +8,7 @@ import type { import { type ReactNode } from "react" import SyncError from "../components/SyncError" -import type { Optional, Required } from "./general" +import { type Optional, type Required, getNestedProperty } from "./general" // ----------------------------------------------------------------------------- // Model Types @@ -238,6 +238,10 @@ export function tagData>( return tags } + function getModelId(result: Result) { + return getNestedProperty(result, id) + } + return (result, error, arg) => { if (!error) { if (arg) { @@ -257,24 +261,19 @@ export function tagData>( } if (result) { - // The result is a model that contains the id field. - if (id in result) { - return tags([(result as Result)[id] as ModelId]) - } - // The result is an array of models that contain the id field. if (Array.isArray(result)) { - return tags(result.map(result => result[id] as ModelId)) + return tags(result.map(getModelId)) + } + + // The result is a model that contains the id field. + if (getModelId(result as Result) !== undefined) { + return tags([getModelId(result as Result)]) } // The result is a list that contains an array of models that contain // the id field. - return tags( - (result as ListResult).data.map( - result => result[id] as ModelId, - ), - true, - ) + return tags((result as ListResult).data.map(getModelId), true) } }