Skip to content

Commit bdc00bc

Browse files
committed
Add lean site summaries for the Sites page
1 parent 647bd7d commit bdc00bc

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

src/api/thingSiteSummaries.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { ThingSiteSummary } from '@/types'
2+
3+
const apiHost = import.meta.env.DEV ? 'http://127.0.0.1:8000' : ''
4+
5+
export async function listThingSiteSummaries(
6+
workspaceId: string
7+
): Promise<ThingSiteSummary[]> {
8+
const query = new URLSearchParams({ workspace_id: workspaceId })
9+
const response = await fetch(
10+
`${apiHost}/api/data/things/site-summaries?${query.toString()}`,
11+
{
12+
credentials: 'include',
13+
headers: {
14+
Accept: 'application/json',
15+
},
16+
}
17+
)
18+
19+
if (!response.ok) {
20+
throw new Error(`Failed to load thing site summaries: ${response.status}`)
21+
}
22+
23+
return (await response.json()) as ThingSiteSummary[]
24+
}

src/components/Maps/OpenLayersMap.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { MapThing, MapThingWithColor } from '@/types'
4747
import {
4848
addColorToMarkers,
4949
generateMarkerContent,
50+
hasThingTags,
5051
isThingMarker,
5152
} from '@/utils/maps/markers'
5253
import OlMap from 'ol/Map'
@@ -187,7 +188,9 @@ const getPopupThing = async (thing: MapThing): Promise<MapThing> => {
187188
async function updateFeatures() {
188189
// 1) Rebuild features
189190
coloredThings.value = props.colorKey
190-
? addColorToMarkers(props.things as Thing[], props.colorKey)
191+
? props.things.every(hasThingTags)
192+
? addColorToMarkers(props.things, props.colorKey)
193+
: props.things
191194
: props.things
192195
193196
const features = coloredThings.value

src/pages/Sites.vue

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ import WorkspaceToolbar from '@/components/Workspace/WorkspaceToolbar.vue'
141141
import hs, {
142142
PermissionResource,
143143
PermissionAction,
144-
Thing,
145144
} from '@hydroserver/client'
145+
import { listThingSiteSummaries } from '@/api/thingSiteSummaries'
146146
import { addColorToMarkers } from '@/utils/maps/markers'
147-
import { ThingWithColor } from '@/types'
147+
import { ThingSiteSummary, ThingSiteSummaryWithColor } from '@/types'
148148
import { Snackbar } from '@/utils/notifications'
149149
import { storeToRefs } from 'pinia'
150150
import { useWorkspaceStore } from '@/store/workspaces'
@@ -162,14 +162,18 @@ const { selectedWorkspace, hasWorkspaces } = storeToRefs(useWorkspaceStore())
162162
const { setWorkspaces } = useWorkspaceStore()
163163
const { hasPermission } = useWorkspacePermissions()
164164
165-
const workspaceThings = ref<Thing[]>([])
165+
const workspaceThings = ref<ThingSiteSummary[]>([])
166166
const useColors = ref(true)
167167
const isFiltered = ref(false)
168168
const isPageLoaded = ref(false)
169169
const filterCriteria = ref({ key: '', values: [] as string[] })
170170
const search = ref()
171171
172-
const matchesFilterCriteria = (thing: Thing, key: string, values: string[]) => {
172+
const matchesFilterCriteria = (
173+
thing: ThingSiteSummary,
174+
key: string,
175+
values: string[]
176+
) => {
173177
if (values.length > 0)
174178
return thing.tags.some(
175179
(tag) => tag.key === key && values.includes(tag.value)
@@ -178,7 +182,7 @@ const matchesFilterCriteria = (thing: Thing, key: string, values: string[]) => {
178182
return thing.tags.some((tag) => tag.key === key)
179183
}
180184
181-
const matchesSearchCriteria = (thing: Thing) => {
185+
const matchesSearchCriteria = (thing: ThingSiteSummary) => {
182186
if (!search.value) return true
183187
const searchLower = search.value.toLowerCase()
184188
return (
@@ -215,7 +219,7 @@ const filteredThings = computed(() => {
215219
})
216220
})
217221
218-
const coloredThings = computed<ThingWithColor[]>(() =>
222+
const coloredThings = computed<ThingSiteSummaryWithColor[]>(() =>
219223
addColorToMarkers(filteredThings.value, filterCriteria.value.key)
220224
)
221225
@@ -250,9 +254,7 @@ const onRowClick = (event: Event, item: any) => {
250254
}
251255
252256
const loadThings = async () => {
253-
workspaceThings.value = await hs.things.listAllItems({
254-
workspace_id: [selectedWorkspace.value!.id],
255-
})
257+
workspaceThings.value = await listThingSiteSummaries(selectedWorkspace.value!.id)
256258
}
257259
258260
onMounted(async () => {
@@ -263,7 +265,7 @@ onMounted(async () => {
263265
setWorkspaces(workspaceRes)
264266
} else {
265267
const [things, workspaceRes] = await Promise.all([
266-
hs.things.listAllItems({ workspace_id: [selectedWorkspace.value.id] }),
268+
listThingSiteSummaries(selectedWorkspace.value.id),
267269
hs.workspaces.listAllItems({ is_associated: true, expand_related: true }),
268270
])
269271
setWorkspaces(workspaceRes)

src/types/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Thing } from '@hydroserver/client'
22

3+
export interface ThingTag {
4+
key: string
5+
value: string
6+
}
7+
38
export interface ThingMarker {
49
id: string
510
workspaceId: string
@@ -10,7 +15,12 @@ export interface ThingMarker {
1015
longitude: number
1116
}
1217

13-
export type MapThing = Thing | ThingMarker
18+
export interface ThingSiteSummary extends ThingMarker {
19+
samplingFeatureCode: string
20+
tags: ThingTag[]
21+
}
22+
23+
export type MapThing = Thing | ThingMarker | ThingSiteSummary
1424

1525
export interface ThingWithColor extends Thing {
1626
color?: {
@@ -21,6 +31,15 @@ export interface ThingWithColor extends Thing {
2131
tagValue?: string
2232
}
2333

34+
export interface ThingSiteSummaryWithColor extends ThingSiteSummary {
35+
color?: {
36+
borderColor: string
37+
background: string
38+
glyphColor: string
39+
}
40+
tagValue?: string
41+
}
42+
2443
export interface ThingMarkerWithColor extends ThingMarker {
2544
color?: {
2645
borderColor: string
@@ -30,4 +49,7 @@ export interface ThingMarkerWithColor extends ThingMarker {
3049
tagValue?: string
3150
}
3251

33-
export type MapThingWithColor = ThingWithColor | ThingMarkerWithColor
52+
export type MapThingWithColor =
53+
| ThingWithColor
54+
| ThingMarkerWithColor
55+
| ThingSiteSummaryWithColor

src/utils/maps/markers.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
import { Thing } from '@hydroserver/client'
2-
import { MapThing, ThingMarker } from '@/types'
2+
import { MapThing, ThingMarker, ThingSiteSummary } from '@/types'
33
import { mapMarkerColors } from '@/utils/materialColors'
44

5-
export const addColorToMarkers = (things: Thing[], key: string) => {
5+
type ColorableThing = Thing | ThingSiteSummary
6+
7+
type ColorizedThing<T extends ColorableThing> = T & {
8+
color?: {
9+
borderColor: string
10+
background: string
11+
glyphColor: string
12+
}
13+
tagValue?: string
14+
}
15+
16+
export function hasThingTags(thing: MapThing): thing is ColorableThing {
17+
return 'tags' in thing && Array.isArray(thing.tags)
18+
}
19+
20+
export const addColorToMarkers = <T extends ColorableThing>(
21+
things: T[],
22+
key: string
23+
): Array<ColorizedThing<T>> => {
624
let colorIndex = 0
725
const colorMap = new Map()
826

0 commit comments

Comments
 (0)