diff --git a/app/src/api/itemsApi.ts b/app/src/api/itemsApi.ts index 7b60142f..7522842a 100644 --- a/app/src/api/itemsApi.ts +++ b/app/src/api/itemsApi.ts @@ -11,6 +11,17 @@ import { directusClient } from './directus' import type { MyCollections } from './directus' import type { ItemsApi } from 'utopia-ui' +// Fields to request when fetching items to include all relational data +const ITEM_FIELDS = [ + '*', + 'secrets.*', + 'to.*', + 'relations.*', + 'user_created.*', + 'markerIcon.*', + { offers: ['*'], needs: ['*'], gallery: ['*.*'] } as any, +] + export class itemsApi implements ItemsApi { collectionName: keyof MyCollections filter: any @@ -43,15 +54,7 @@ export class itemsApi implements ItemsApi { try { const result = await directusClient.request( readItems(this.collectionName as never, { - fields: [ - '*', - 'secrets.*', - 'to.*', - 'relations.*', - 'user_created.*', - 'markerIcon.*', - { offers: ['*'], needs: ['*'], gallery: ['*.*'] } as any, - ], + fields: ITEM_FIELDS, filter: this.filter, limit: -1, }), @@ -70,7 +73,11 @@ export class itemsApi implements ItemsApi { async getItem(id: string): Promise { try { - const result = await directusClient.request(readItem(this.collectionName as never, id)) + const result = await directusClient.request( + readItem(this.collectionName as never, id, { + fields: ITEM_FIELDS, + }), + ) return result as T } catch (error: any) { console.log(error) @@ -82,13 +89,18 @@ export class itemsApi implements ItemsApi { async createItem(item: T & { id?: string }): Promise { try { const result = await directusClient.request( - createItem(this.collectionName, { - ...item, - ...(this.customParameter && this.customParameter), - ...(this.layerId && { layer: this.layerId }), - ...(this.layerId && { layer: this.layerId }), - ...(this.mapId && { map: this.mapId }), - }), + createItem( + this.collectionName, + { + ...item, + ...(this.customParameter && this.customParameter), + ...(this.layerId && { layer: this.layerId }), + ...(this.mapId && { map: this.mapId }), + }, + { + fields: ITEM_FIELDS, + }, + ), ) return result as T } catch (error: any) { @@ -100,7 +112,11 @@ export class itemsApi implements ItemsApi { async updateItem(item: T & { id?: string }): Promise { try { - const result = await directusClient.request(updateItem(this.collectionName, item.id!, item)) + const result = await directusClient.request( + updateItem(this.collectionName, item.id!, item, { + fields: ITEM_FIELDS, + }), + ) return result as T } catch (error: any) { console.log(error)