-
Notifications
You must be signed in to change notification settings - Fork 3
Alpha 11 #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielfdsilva
wants to merge
7
commits into
main
Choose a base branch
from
feature/alpha11
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Alpha 11 #24
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b6f2b2
Refactors STAC API context and hooks
danielfdsilva bb2337a
Update test to match changes
danielfdsilva 41c9450
Lint files
danielfdsilva 549a12e
Increments package version to 0.1.0-alpha.11
danielfdsilva 31a1056
Update github actions file
danielfdsilva 615c43a
Update collections pagination system
danielfdsilva 3c6f712
Enables querying collections with query params
danielfdsilva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { createContext } from 'react'; | ||
| import StacApi from '../stac-api'; | ||
| import { CollectionsResponse, Item } from '../types/stac'; | ||
|
|
||
| type StacApiContextType = { | ||
| stacApi?: StacApi; | ||
| collections?: CollectionsResponse; | ||
| setCollections: (collections?: CollectionsResponse) => void; | ||
| getItem: (id: string) => Item | undefined; | ||
| addItem: (id: string, item: Item) => void; | ||
| deleteItem: (id: string) => void; | ||
| }; | ||
|
|
||
| export const StacApiContext = createContext<StacApiContextType>( | ||
| {} as StacApiContextType | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,90 @@ | ||
| import { useCallback, useEffect, useState, useMemo } from 'react'; | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { type ApiError, type LoadingState } from '../types'; | ||
| import type { CollectionsResponse } from '../types/stac'; | ||
| import debounce from '../utils/debounce'; | ||
| import { useStacApiContext } from '../context'; | ||
|
|
||
| type StacCollectionsHook = { | ||
| collections?: CollectionsResponse, | ||
| reload: () => void, | ||
| state: LoadingState | ||
| error?: ApiError | ||
| collections?: CollectionsResponse; | ||
| reload: () => void; | ||
| state: LoadingState; | ||
| error?: ApiError; | ||
| nextPage?: () => void; | ||
| prevPage?: () => void; | ||
| setOffset: (newOffset: number) => void; | ||
| }; | ||
|
|
||
| function useCollections(): StacCollectionsHook { | ||
| export default function useCollections(opts?: { | ||
| limit?: number; | ||
| initialOffset?: number; | ||
| }): StacCollectionsHook { | ||
| const { limit = 10, initialOffset = 0 } = opts || {}; | ||
|
|
||
| const { stacApi, collections, setCollections } = useStacApiContext(); | ||
| const [ state, setState ] = useState<LoadingState>('IDLE'); | ||
| const [ error, setError ] = useState<ApiError>(); | ||
| const [state, setState] = useState<LoadingState>('IDLE'); | ||
| const [error, setError] = useState<ApiError>(); | ||
|
|
||
| const [offset, setOffset] = useState(initialOffset); | ||
|
|
||
| const [hasNext, setHasNext] = useState(false); | ||
| const [hasPrev, setHasPrev] = useState(false); | ||
|
|
||
| const _getCollections = useCallback( | ||
| () => { | ||
| async (offset: number, limit: number) => { | ||
| if (stacApi) { | ||
| setState('LOADING'); | ||
|
|
||
| stacApi.getCollections() | ||
| .then(response => response.json()) | ||
| .then(setCollections) | ||
| .catch((err) => { | ||
| setError(err); | ||
| setCollections(undefined); | ||
| }) | ||
| .finally(() => setState('IDLE')); | ||
| try { | ||
| const res = await stacApi.getCollections({ limit, offset }); | ||
| const data: CollectionsResponse = await res.json(); | ||
|
|
||
| setHasNext(!!data.links?.find((l) => l.rel === 'next')); | ||
| setHasPrev( | ||
| !!data.links?.find((l) => ['prev', 'previous'].includes(l.rel)) | ||
| ); | ||
|
|
||
| setCollections(data); | ||
| } catch (err: any) { | ||
| setError(err); | ||
| setCollections(undefined); | ||
| } finally { | ||
| setState('IDLE'); | ||
| } | ||
| } | ||
| }, | ||
| [setCollections, stacApi] | ||
| ); | ||
| const getCollections = useMemo(() => debounce(_getCollections), [_getCollections]); | ||
|
|
||
| useEffect( | ||
| () => { | ||
| if (stacApi && !error && !collections) { | ||
| getCollections(); | ||
| } | ||
| }, | ||
| [getCollections, stacApi, collections, error] | ||
| const getCollections = useCallback( | ||
| (offset: number, limit: number) => | ||
| debounce(() => _getCollections(offset, limit))(), | ||
| [_getCollections] | ||
| ); | ||
|
|
||
| const nextPage = useCallback(() => { | ||
| setOffset(offset + limit); | ||
| }, [offset, limit]); | ||
|
|
||
| const prevPage = useCallback(() => { | ||
| setOffset(offset - limit); | ||
| }, [offset, limit]); | ||
|
|
||
| useEffect(() => { | ||
| if (stacApi && !error && !collections) { | ||
| getCollections(offset, limit); | ||
| } | ||
| }, [getCollections, stacApi, collections, error, offset, limit]); | ||
|
|
||
| return { | ||
| collections, | ||
| reload: getCollections, | ||
| reload: useCallback( | ||
| () => getCollections(offset, limit), | ||
| [getCollections, offset, limit] | ||
| ), | ||
| nextPage: hasNext ? nextPage : undefined, | ||
| prevPage: hasPrev ? prevPage : undefined, | ||
| setOffset, | ||
| state, | ||
| error, | ||
| error | ||
| }; | ||
| } | ||
|
|
||
| export default useCollections; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the collections response has next and/or previous links specified, why are we using
offsetandlimithere instead of calling the provided links. Also, the collections spec usespagequery params for pagination in collections.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While working with the eoepca api (https://eoapi.apx.develop.eoepca.org/stac/collections?limit=2) it uses offset and limit, so I went with that.
Using the offset and limit allows the user to go to a specific page, or to reset to the first page more easily. There's no "first" link, so to go back we'd need to get the current link and set the offset to 0.
Not sure what the best approach would be. Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I said was not 100% correct. The spec actually says this:
So really, we don't know how individual services implement pagination. Seeing, that STAC React is a generic library, the safest bet is to rely on
nextandprev/previouslinks.Not being able to reliably infer a first or last page link is an obvious flaw, which should probably be added to the STAC API spec. We could try to infer the pagination parameters, but it seems to open a huge can of worms because we really don't know how pagination will work in each instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is unfortunate. I'll check about using the prev/next