Skip to content

Commit de8d7dd

Browse files
committed
feat: begin using TaxonObserved in the UI
1 parent 6c65430 commit de8d7dd

File tree

9 files changed

+110
-15
lines changed

9 files changed

+110
-15
lines changed

ui/src/data-services/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const API_ROUTES = {
1919
SESSIONS: 'events',
2020
SITES: 'deployments/sites',
2121
SPECIES: 'taxa',
22+
TAXA_OBSERVED: 'taxa/observed',
2223
STORAGE: 'storage',
2324
SUMMARY: 'status/summary',
2425
USERS: 'users',
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { API_ROUTES } from 'data-services/constants'
2+
import { FetchParams } from 'data-services/types'
3+
import { getFetchUrl } from 'data-services/utils'
4+
import { useMemo } from 'react'
5+
import { ServerSpecies, Species } from '../../models/species'
6+
import { useAuthorizedQuery } from '../auth/useAuthorizedQuery'
7+
8+
const convertServerRecord = (record: ServerSpecies) => new Species(record)
9+
10+
export const useSpeciesObserved = (
11+
params?: FetchParams
12+
): {
13+
species?: Species[]
14+
total: number
15+
isLoading: boolean
16+
isFetching: boolean
17+
error?: unknown
18+
} => {
19+
const fetchUrl = getFetchUrl({ collection: API_ROUTES.TAXA_OBSERVED, params })
20+
21+
const { data, isLoading, isFetching, error } = useAuthorizedQuery<{
22+
results: ServerSpecies[]
23+
count: number
24+
}>({
25+
queryKey: [API_ROUTES.TAXA_OBSERVED, params],
26+
url: fetchUrl,
27+
})
28+
29+
const species = useMemo(() => data?.results.map(convertServerRecord), [data])
30+
31+
return {
32+
species,
33+
total: data?.count ?? 0,
34+
isLoading,
35+
isFetching,
36+
error,
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { API_ROUTES, API_URL } from 'data-services/constants'
2+
3+
import {
4+
ServerSpeciesDetails,
5+
SpeciesDetails,
6+
} from 'data-services/models/species-details'
7+
import { useMemo } from 'react'
8+
import { useAuthorizedQuery } from '../auth/useAuthorizedQuery'
9+
10+
const convertServerRecord = (record: ServerSpeciesDetails) =>
11+
new SpeciesDetails(record)
12+
13+
export const useSpeciesObservedDetails = (
14+
id: string
15+
): {
16+
species?: SpeciesDetails
17+
isLoading: boolean
18+
isFetching: boolean
19+
error?: unknown
20+
} => {
21+
const { data, isLoading, isFetching, error } =
22+
useAuthorizedQuery<SpeciesDetails>({
23+
queryKey: [API_ROUTES.TAXA_OBSERVED, id],
24+
url: `${API_URL}/${API_ROUTES.TAXA_OBSERVED}/${id}/`,
25+
})
26+
27+
const species = useMemo(
28+
() => (data ? convertServerRecord(data) : undefined),
29+
[data]
30+
)
31+
32+
return {
33+
species,
34+
isLoading,
35+
isFetching,
36+
error,
37+
}
38+
}

ui/src/data-services/models/species.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Taxon } from './taxa'
1+
import { TaxonObserved } from './taxa-observed'
22

33
export type ServerSpecies = any // TODO: Update this type
44

5-
export class Species extends Taxon {
5+
export class Species extends TaxonObserved {
66
protected readonly _species: ServerSpecies
77
private readonly _images: { src: string }[] = []
88

@@ -34,7 +34,7 @@ export class Species extends Taxon {
3434
}
3535

3636
get trainingImagesUrl(): string {
37-
return `https://www.gbif.org/occurrence/gallery?advanced=1&verbatim_scientific_name=${this.name}`
37+
return `https://www.gbif.org/occurrence/gallery?advanced=1&verbatim_scientific_name=${this.taxon.name}`
3838
}
3939

4040
get score(): number {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ServerTaxon, Taxon } from './taxa'
2+
3+
export type ServerTaxonObserved = {
4+
id: string
5+
taxon: ServerTaxon
6+
detections_count: number
7+
occurrences_count: number
8+
best_determinations_score: number
9+
occurrence_images: string[]
10+
last_detected: string
11+
}
12+
13+
export class TaxonObserved {
14+
readonly id: string
15+
readonly taxon: Taxon
16+
17+
public constructor(taxonObserved: ServerTaxonObserved) {
18+
this.id = taxonObserved.id
19+
this.taxon = new Taxon(taxonObserved.taxon)
20+
}
21+
}

ui/src/data-services/models/taxa.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type ServerTaxon = {
66
parents?: ServerTaxon[]
77
}
88

9-
const SORTED_RANKS = [
9+
export const SORTED_RANKS = [
1010
'Unknown',
1111
'ORDER',
1212
'SUBORDER',
@@ -34,9 +34,6 @@ export class Taxon {
3434

3535
if (taxon.parents) {
3636
this.ranks = taxon.parents
37-
} else if (taxon.parent?.parents) {
38-
// TODO: Update this when species list is returning parents similar to other endpoints
39-
this.ranks = [taxon.parent, ...taxon.parent.parents]
4037
} else {
4138
this.ranks = []
4239
}

ui/src/pages/species-details/species-details.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const SpeciesDetails = ({ species }: { species: Species }) => {
4545
value: species.numOccurrences || 'View all',
4646
to: getAppRoute({
4747
to: APP_ROUTES.OCCURRENCES({ projectId: projectId as string }),
48-
filters: { determination: species.id },
48+
filters: { determination: species.taxon.id },
4949
}),
5050
},
5151
{
@@ -59,7 +59,7 @@ export const SpeciesDetails = ({ species }: { species: Species }) => {
5959
<div className={styles.wrapper}>
6060
<div className={styles.header}>
6161
<TaxonInfo
62-
taxon={species}
62+
taxon={species.taxon}
6363
size={TaxonInfoSize.Large}
6464
getLink={(id: string) =>
6565
getAppRoute({

ui/src/pages/species/species-columns.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const columns: (projectId: string) => TableColumn<Species>[] = (
5151
})}
5252
>
5353
<BasicTableCell>
54-
<TaxonInfo compact taxon={item} />
54+
<TaxonInfo compact taxon={item.taxon} />
5555
</BasicTableCell>
5656
</Link>
5757
),

ui/src/pages/species/species.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useSpecies } from 'data-services/hooks/species/useSpecies'
2-
import { useSpeciesDetails } from 'data-services/hooks/species/useSpeciesDetails'
1+
import { useSpeciesObserved } from 'data-services/hooks/species-observed/useSpeciesObserved'
2+
import { useSpeciesObservedDetails } from 'data-services/hooks/species-observed/useSpeciesObservedDetails'
33
import * as Dialog from 'design-system/components/dialog/dialog'
44
import { IconType } from 'design-system/components/icon/icon'
55
import { PageFooter } from 'design-system/components/page-footer/page-footer'
@@ -28,7 +28,7 @@ export const Species = () => {
2828
const { sort, setSort } = useSort({ field: 'name', order: 'asc' })
2929
const { pagination, setPage } = usePagination()
3030
const { filters } = useFilters()
31-
const { species, total, isLoading, isFetching, error } = useSpecies({
31+
const { species, total, isLoading, isFetching, error } = useSpeciesObserved({
3232
projectId,
3333
sort,
3434
pagination,
@@ -101,10 +101,10 @@ const SpeciesDetailsDialog = ({ id }: { id: string }) => {
101101
const navigate = useNavigate()
102102
const { projectId } = useParams()
103103
const { setDetailBreadcrumb } = useContext(BreadcrumbContext)
104-
const { species, isLoading, error } = useSpeciesDetails(id, projectId)
104+
const { species, isLoading, error } = useSpeciesObservedDetails(id)
105105

106106
useEffect(() => {
107-
setDetailBreadcrumb(species ? { title: species.name } : undefined)
107+
setDetailBreadcrumb(species ? { title: species.taxon.name } : undefined)
108108

109109
return () => {
110110
setDetailBreadcrumb(undefined)

0 commit comments

Comments
 (0)