Skip to content

Commit 07662bc

Browse files
authored
Merge pull request #108 from hydroserver2/178-fix-fetch-metadata
178 fix fetch metadata
2 parents 85efa0d + 2d05321 commit 07662bc

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/composables/__tests__/useMetadata.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let defaultThingId: string | null = 'thing1'
3838
const createDummyComponent = ({ thingId = defaultThingId } = {}) =>
3939
defineComponent({
4040
setup() {
41-
return useMetadata(thingId, api)
41+
return useMetadata(thingId, false, api)
4242
},
4343
template: '<div>{{sensors}}</div>',
4444
})

src/composables/useMetadata.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@ interface Api {
77
fetchMetadataForThingOwner: (id: string) => Promise<DatastreamMetadata>
88
}
99

10-
export function useMetadata(thingId?: string | null, api: Api = defaultApi) {
11-
const metadata = ref<DatastreamMetadata | null>()
10+
/**
11+
* Fetch metadata for a specific thing or its owner.
12+
*
13+
* @param {string | null} [thingId=null] - ID of the thing to fetch metadata for.
14+
* @param {boolean} [forOwner=false] - Fetch metadata for the primary owner of this thing if true.
15+
* @param {Api} [api=defaultApi] - API service for fetching metadata.
16+
*/
17+
export function useMetadata(
18+
thingId?: string | null,
19+
forOwner: boolean = false,
20+
api: Api = defaultApi
21+
) {
22+
const metadata = ref<DatastreamMetadata | null>(null)
1223

1324
const sensors = computed(() => metadata.value?.sensors || [])
1425

@@ -35,7 +46,9 @@ export function useMetadata(thingId?: string | null, api: Api = defaultApi) {
3546

3647
const fetchMetadata = async (id: string) => {
3748
try {
38-
metadata.value = await api.fetchMetadataForThing(id)
49+
metadata.value = forOwner
50+
? await api.fetchMetadataForThingOwner(id)
51+
: await api.fetchMetadataForThing(id)
3952
} catch (error) {
4053
console.error('Error fetching metadata', error)
4154
}

src/pages/DatastreamForm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ const {
355355
processingLevels,
356356
formattedProcessingLevels,
357357
fetchMetadata,
358-
} = useMetadata(thingId)
358+
} = useMetadata(thingId, true)
359359
360360
const handleMetadataUploaded = async (dsKey: string, newId: string) => {
361361
await fetchMetadata(thingId)

0 commit comments

Comments
 (0)