-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add useSearchQueryParams and useInfiniteSearch
- Loading branch information
1 parent
4d45f3c
commit 3d506a0
Showing
8 changed files
with
1,465 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,130 @@ | ||
import { | ||
ResourceTypeEnum, | ||
LearningResourcesSearchRetrieveDepartmentEnum as DepartmentEnum, | ||
LearningResourcesSearchRetrieveLevelEnum as LevelEnum, | ||
LearningResourcesSearchRetrievePlatformEnum as PlatformEnum, | ||
LearningResourcesSearchRetrieveOfferedByEnum as OfferedByEnum, | ||
LearningResourcesSearchRetrieveSortbyEnum as SortByEnum, | ||
// ContentFile Search Enums | ||
ContentFileSearchRetrieveSortbyEnum as ContentFileSortEnum | ||
} from "../open_api_generated" | ||
|
||
type Endpoint = "resources" | "content_files" | ||
|
||
type SearchParams = { | ||
endpoint: Endpoint | ||
activeFacets: { | ||
resource_type?: ResourceTypeEnum[] | ||
department?: DepartmentEnum[] | ||
course_feature?: string[] | ||
content_feature_type?: string[] | ||
level?: LevelEnum[] | ||
platform?: PlatformEnum[] | ||
offered_by?: OfferedByEnum[] | ||
topic?: string[] | ||
certification?: boolean | ||
professional?: boolean | ||
} | ||
sort?: SortByEnum | ContentFileSortEnum | ||
queryText: string | ||
} | ||
|
||
type FacetName = keyof SearchParams["activeFacets"] | ||
|
||
const withinEnum = | ||
<T>(allowed: T[]) => | ||
(value: string) => | ||
(allowed as string[]).includes(value) | ||
|
||
type EndpointParamConfig = { | ||
sort: { | ||
alias: string | ||
isValid: (value: string) => boolean | ||
} | ||
facets: { | ||
[K in keyof SearchParams["activeFacets"]]?: { | ||
alias: string | ||
isValid: (value: string) => boolean | ||
isBoolean?: boolean | ||
} | ||
} | ||
} | ||
|
||
const searchParamConfig: Record<Endpoint, EndpointParamConfig> = { | ||
resources: { | ||
sort: { | ||
alias: "s", | ||
isValid: withinEnum(Object.values(SortByEnum)) | ||
}, | ||
facets: { | ||
resource_type: { | ||
alias: "r", | ||
isValid: withinEnum(Object.values(ResourceTypeEnum)) | ||
}, | ||
department: { | ||
alias: "d", | ||
isValid: withinEnum(Object.values(DepartmentEnum)) | ||
}, | ||
level: { | ||
alias: "l", | ||
isValid: withinEnum(Object.values(LevelEnum)) | ||
}, | ||
platform: { | ||
alias: "p", | ||
isValid: withinEnum(Object.values(PlatformEnum)) | ||
}, | ||
offered_by: { | ||
alias: "o", | ||
isValid: withinEnum(Object.values(OfferedByEnum)) | ||
}, | ||
topic: { | ||
alias: "t", | ||
isValid: (value: string) => value.length > 0 | ||
}, | ||
certification: { | ||
alias: "c", | ||
isValid: (value: string) => value === "true", | ||
isBoolean: true | ||
}, | ||
professional: { | ||
alias: "pr", | ||
isValid: (value: string) => value === "true", | ||
isBoolean: true | ||
}, | ||
course_feature: { | ||
alias: "cf", | ||
isValid: (value: string) => value.length > 0 | ||
} | ||
} | ||
}, | ||
content_files: { | ||
sort: { | ||
alias: "s", | ||
isValid: withinEnum(Object.values(ContentFileSortEnum)) | ||
}, | ||
facets: { | ||
offered_by: { | ||
alias: "o", | ||
isValid: withinEnum(Object.values(OfferedByEnum)) | ||
}, | ||
platform: { | ||
alias: "p", | ||
isValid: withinEnum(Object.values(PlatformEnum)) | ||
}, | ||
content_feature_type: { | ||
alias: "cf", | ||
isValid: (value: string) => value.length > 0 | ||
}, | ||
topic: { | ||
alias: "t", | ||
isValid: (value: string) => value.length > 0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
const QUERY_TEXT_ALIAS = "q" | ||
const ENDPOINT_ALIAS = "e" | ||
|
||
export type { SearchParams, Endpoint, FacetName } | ||
export { searchParamConfig, QUERY_TEXT_ALIAS, ENDPOINT_ALIAS } |
This file contains 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,13 @@ | ||
export { default as useSearchQueryParams } from "./useSearchQueryParams" | ||
export type { | ||
UseSearchQueryParamsProps, | ||
UseSearchQueryParamsResult | ||
} from "./useSearchQueryParams" | ||
|
||
export type { SearchParams, Endpoint, FacetName } from "./configs" | ||
|
||
export { default as useInfiniteSearch } from "./useInfiniteSearch" | ||
export type { | ||
UseInfiniteSearchProps, | ||
UseInfiniteSearchResult | ||
} from "./useInfiniteSearch" |
Oops, something went wrong.