Skip to content

Commit

Permalink
feat(@dpc-sdp/ripple-tide-search): got filter form working correctly …
Browse files Browse the repository at this point in the history
…with URL
  • Loading branch information
jeffdowdle committed Jul 13, 2023
1 parent 295d3d1 commit a489370
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
30 changes: 10 additions & 20 deletions packages/ripple-tide-search/components/TideSearchFilters.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<template>
<RplForm
id="tide-search-filter-form"
:modelValue="filterFormModel"
class="rpl-u-margin-t-6"
@input="handleUpdateFilters"
@submit="handleFilterSubmit"
>
<div class="rpl-grid rpl-grid--no-row-gap tide-search-filters">
Expand All @@ -16,7 +14,13 @@
:is="filter.component"
:id="filter.id"
:name="filter.id"
:modelValue="filterFormValues[filter.id]"
v-bind="filter.props"
:options="
filter.props.dynamicOptions?.length
? filter.props.dynamicOptions
: filter.props.options
"
></component>
</div>
</div>
Expand All @@ -32,7 +36,6 @@

<script setup lang="ts">
import { FilterFormModel } from 'ripple-tide-search/types'
import { ref, toRaw, watch } from 'vue'
type CollectionFilter = {
component: string
Expand All @@ -51,29 +54,16 @@ const emit = defineEmits<{
(e: 'reset'): void
}>()
const props = withDefaults(defineProps<Props>(), {
withDefaults(defineProps<Props>(), {
submitLabel: 'Apply search filters',
resetLabel: 'Clear search filters'
})
const filterFormModel = ref(toRaw(props.filterFormValues.value))
const handleUpdateFilters = (values) => {
filterFormModel.value = values
}
watch(
() => props.filterFormValues,
(newValues) => {
filterFormModel.value = newValues
}
)
function handleFilterReset() {
const handleFilterReset = () => {
emit('reset')
}
function handleFilterSubmit() {
emit('submit', filterFormModel.value)
const handleFilterSubmit = (formValues) => {
emit('submit', formValues.data)
}
</script>
18 changes: 10 additions & 8 deletions packages/ripple-tide-search/components/TideSearchListingPage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { useRuntimeConfig, useFetch, useRoute, ref } from '#imports'
import { useRuntimeConfig, useFetch, useRoute, ref, toRaw } from '#imports'
import useTideSearch from './../composables/useTideSearch'
import type {
TideSearchListingPage,
Expand Down Expand Up @@ -105,27 +105,29 @@ onAggregationUpdateHook.value = (aggs) => {
Object.keys(aggs).forEach((key) => {
uiFilters.value.forEach((uiFilter, idx) => {
if (uiFilter.id === key) {
const getOptions = () => {
const getDynamicOptions = () => {
const mappedOptions = aggs[key].map((item) => ({
id: item,
label: item,
value: item
}))
if (uiFilters.value[idx].props.hasOwnProperty('options')) {
return [...uiFilters.value[idx].props.options, ...mappedOptions]
} else if (mappedOptions.length > 0) {
return mappedOptions
return [
...toRaw(uiFilters.value[idx].props.options),
...mappedOptions
]
}
return []
return mappedOptions
}
uiFilters.value[idx] = {
...uiFilters.value[idx],
props: {
...uiFilters.value[idx].props,
timestamp: updateTimestamp,
options: getOptions()
dynamicOptions: getDynamicOptions()
}
}
}
Expand All @@ -143,7 +145,7 @@ const handleFilterSubmit = (form) => {
}
const handleFilterReset = () => {
filterForm.value = []
filterForm.value = {}
submitSearch()
}
Expand Down
13 changes: 11 additions & 2 deletions packages/ripple-tide-search/composables/useTideSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default (
{
[`${itm.filter.type}`]: {
// ES8 appears to require keyword suffix due to change in indexing
[`${itm.filter.value}.keyword`]: filterVal
[`${itm.filter.value}`]: filterVal
}
}
]
Expand Down Expand Up @@ -293,9 +293,18 @@ export default (
const formValues = Object.keys(newRoute.query)
.filter((key) => userFilterConfig.some((filter) => filter.id === key))
.reduce((obj, key) => {
let parsedValue = newRoute.query[key]
const filterConfig = userFilterConfig.find(
(filter) => filter.id === key
)

if (filterConfig.component === 'TideSearchFilterDropdown') {
parsedValue = Array.isArray(parsedValue) ? parsedValue : [parsedValue]
}

return {
...obj,
[key]: newRoute.query[key]
[key]: parsedValue
}
}, {})

Expand Down

0 comments on commit a489370

Please sign in to comment.