Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions datagouv-components/src/components/Search/GlobalSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,13 @@
<div class="fr-col">
<select
id="sort-search"
v-model="sort"
v-model="effectiveSort"
class="fr-select text-sm shadow-input-blue!"
>
<option :value="undefined">
<option
v-if="!currentTypeConfig?.defaultSort"
:value="undefined"
>
{{ t('Pertinence') }}
</option>
<option
Expand Down Expand Up @@ -489,6 +492,10 @@ const { debounced: qDebounced, flush: flushQ } = useDebouncedRef(q, componentsCo
const qForParams = computed(() => props.hideSearchInput ? q.value : qDebounced.value)
const page = useRouteQuery('page', 1, { transform: Number })
const sort = useRouteQuery<string | undefined>('sort')
const effectiveSort = computed({
get: () => sort.value ?? currentTypeConfig.value?.defaultSort,
set: (value) => { sort.value = value },
})

provide(searchFilterContextKey, {
register(urlParam, entry) {
Expand Down Expand Up @@ -759,7 +766,7 @@ const rssUrl = computed(() => {
}, currentTypeConfig.value ? configKey(currentTypeConfig.value) : undefined)

// Add sort if set
if (sort.value) params.set('sort', sort.value)
if (effectiveSort.value) params.set('sort', effectiveSort.value)

const queryString = params.toString()
const basePath = '/api/1/datasets/recent.atom'
Expand Down
7 changes: 4 additions & 3 deletions datagouv-components/src/composables/useStableQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ export function useStableQueryParams(options: StableQueryParamsOptions) {
if (q.value) {
params.q = q.value
}
if (sort.value) {
const sortToUse = sort.value ?? typeConfig?.defaultSort
if (sortToUse) {
const validSortValues = typeConfig?.sortOptions?.map(o => o.value as string) ?? []
if (validSortValues.includes(sort.value)) {
params.sort = sort.value
if (validSortValues.includes(sortToUse)) {
params.sort = sortToUse
}
}
params.page = page.value
Expand Down
5 changes: 5 additions & 0 deletions datagouv-components/src/types/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ export type DatasetSearchConfig = {
basicFilters?: (keyof DatasetSearchFilters)[]
advancedFilters?: (keyof DatasetSearchFilters)[]
sortOptions?: SortOption<DatasetSearchSort>[]
defaultSort?: DatasetSearchSort
}

export type DataserviceSearchConfig = {
Expand All @@ -314,6 +315,7 @@ export type DataserviceSearchConfig = {
basicFilters?: (keyof DataserviceSearchFilters)[]
advancedFilters?: (keyof DataserviceSearchFilters)[]
sortOptions?: SortOption<DataserviceSearchSort>[]
defaultSort?: DataserviceSearchSort
}

export type ReuseSearchConfig = {
Expand All @@ -325,6 +327,7 @@ export type ReuseSearchConfig = {
basicFilters?: (keyof ReuseSearchFilters)[]
advancedFilters?: (keyof ReuseSearchFilters)[]
sortOptions?: SortOption<ReuseSearchSort>[]
defaultSort?: ReuseSearchSort
}

export type OrganizationSearchConfig = {
Expand All @@ -336,6 +339,7 @@ export type OrganizationSearchConfig = {
basicFilters?: (keyof OrganizationSearchFilters)[]
advancedFilters?: (keyof OrganizationSearchFilters)[]
sortOptions?: SortOption<OrganizationSearchSort>[]
defaultSort?: OrganizationSearchSort
}

export type TopicSearchConfig = {
Expand All @@ -347,6 +351,7 @@ export type TopicSearchConfig = {
basicFilters?: (keyof TopicSearchFilters)[]
advancedFilters?: (keyof TopicSearchFilters)[]
sortOptions?: SortOption<TopicSearchSort>[]
defaultSort?: TopicSearchSort
}

export type SearchTypeConfig = DatasetSearchConfig | DataserviceSearchConfig | ReuseSearchConfig | OrganizationSearchConfig | TopicSearchConfig
Expand Down
Loading