Skip to content

Commit

Permalink
combine current and ongoing series
Browse files Browse the repository at this point in the history
  • Loading branch information
jendiamond committed Jan 15, 2025
1 parent 0aed170 commit 22c93cd
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 12 deletions.
72 changes: 68 additions & 4 deletions composables/useEventSeriesListSearchFilter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO Convert this composable into TypeScript

export default function useEventSeriesListSearchFilter() {
return { pastEventSeriesQuery, currentEventSeriesQuery }
return { pastEventSeriesQuery, currentEventSeriesQueryCurrent, currentEventSeriesQueryOngoing }
}

async function pastEventSeriesQuery(
Expand Down Expand Up @@ -58,7 +58,7 @@ async function pastEventSeriesQuery(
return data
}

async function currentEventSeriesQuery(
async function currentEventSeriesQueryCurrent(
currentPage = 1,
documentsPerPage = 10,
sort,
Expand Down Expand Up @@ -97,7 +97,7 @@ async function currentEventSeriesQuery(
should: [
{
term: {
ongoing: true,
ongoing: false,
},
},
{
Expand All @@ -108,7 +108,59 @@ async function currentEventSeriesQuery(
},
},
],
minimum_should_match: 1,
minimum_should_match: 2,
}
},
...parseSort(sort, orderBy),
}),
}
)

const data = await response.json()
return data
}

async function currentEventSeriesQueryOngoing(
currentPage = 1,
documentsPerPage = 10,
sort,
orderBy,
source = ['*'],
) {
const config = useRuntimeConfig()
if (
config.public.esReadKey === '' ||
config.public.esURL === '' ||
config.public.esAlias === ''
)
return

const response = await fetch(
`${config.public.esURL}/${config.public.esAlias}/_search`,
{
headers: {
Authorization: `ApiKey ${config.public.esReadKey}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
from: (currentPage - 1) * documentsPerPage,
size: documentsPerPage,
_source: [...source],
query: {
bool: {
filter: [
{
term: {
'sectionHandle.keyword': 'ftvaEventSeries',
},
},
{
term: {
ongoing: true,
},
}
]
}
},
...parseSort(sort, orderBy),
Expand All @@ -131,3 +183,15 @@ function parseSort(sortField, orderBy = 'asc') {

return parseQuery
}

function parseSort2(sortField, orderBy = 'asc') {
if (!sortField || sortField === '') return {}
const parseQuery = {}
parseQuery.sort = []
parseQuery.sort[0] = {}
parseQuery.sort[0][sortField] = {
order: orderBy
}

return parseQuery
}
105 changes: 97 additions & 8 deletions pages/series/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,111 @@ const parsedEventSeries = computed(() => {
})
// ES FUNCTION
// async function searchES() {
// if (isLoading.value || !hasMore.value) return
// isLoading.value = true
// // COMPOSABLE
// const { currentEventSeriesQueryCurrent, currentEventSeriesQueryOngoing, pastEventSeriesQuery } = useEventSeriesListSearchFilter()
// try {
// let results
// if (currentView.value === 'current') {
// // const currentSeries = results.hits.hits || []
// const currentSeries = await currentEventSeriesQueryCurrent(currentPage.value,
// documentsPerPage,
// 'startDate',
// 'asc',
// ['*'],)
// const ongoingSeries = await currentEventSeriesQueryOngoing(currentPage.value,
// documentsPerPage,
// 'startDate',
// 'asc',
// ['*'],)

Check failure on line 160 in pages/series/index.vue

View workflow job for this annotation

GitHub Actions / eslint

More than 1 blank line not allowed
// // results = await currentEventSeriesQueryCurrent(currentPage.value,
// // documentsPerPage,
// // 'startDate',
// // 'asc',
// // ['*'],)
// // results = ongoingSeries;
// //const eventSeries = currentSeries.concat(ongoingSeries);
// //const eventSeries = await [...currentSeries, ...ongoingSeries]
// results = ongoingSeries;
// } else {
// results = await pastEventSeriesQuery(currentPage.value,
// documentsPerPage,
// 'startDate',
// 'desc',
// ['*'])
// }
// if (results?.hits?.hits?.length > 0) {
// const newSeries = results.hits.hits || []
// if (isMobile.value) {
// mobileSeries.value.push(...newSeries)
// hasMore.value = currentPage.value < Math.ceil(results.hits.total.value / documentsPerPage)
// } else {
// desktopSeries.value = newSeries
// totalPages.value = Math.ceil(results.hits.total.value / documentsPerPage)
// }
// noResultsFound.value = false
// } else {
// noResultsFound.value = true
// if (!isMobile.value) totalPages.value = 0
// hasMore.value = false
// }
// } catch (err) {
// noResultsFound.value = true
// } finally {
// isLoading.value = false
// }
// }
async function searchES() {
if (isLoading.value || !hasMore.value) return
isLoading.value = true
// COMPOSABLE
const { currentEventSeriesQuery, pastEventSeriesQuery } = useEventSeriesListSearchFilter()
const { currentEventSeriesQueryCurrent, currentEventSeriesQueryOngoing, pastEventSeriesQuery } = useEventSeriesListSearchFilter()
try {
let results
if (currentView.value === 'current') {
results = await currentEventSeriesQuery(currentPage.value,
documentsPerPage,
'startDate',
'asc',
['*'],)
const [currentSeriesResult, ongoingSeriesResult] = await Promise.all([
currentEventSeriesQueryCurrent(
currentPage.value,
documentsPerPage,
'startDate',
'asc',
['*']
),
currentEventSeriesQueryOngoing(
currentPage.value,
documentsPerPage,
'startDate',
'asc',
['*']
)
])
// Combine results with current series first, ongoing series last
const currentSeries = currentSeriesResult?.hits?.hits || [];

Check failure on line 231 in pages/series/index.vue

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon
const ongoingSeries = ongoingSeriesResult?.hits?.hits || [];

Check failure on line 232 in pages/series/index.vue

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon
results = {
hits: {
hits: [...currentSeries, ...ongoingSeries],
total: { value: currentSeries.length + ongoingSeries.length },
},
};

Check failure on line 238 in pages/series/index.vue

View workflow job for this annotation

GitHub Actions / eslint

Extra semicolon

Check failure on line 238 in pages/series/index.vue

View workflow job for this annotation

GitHub Actions / eslint

Block must not be padded by blank lines
} else {
results = await pastEventSeriesQuery(currentPage.value,
documentsPerPage,
Expand Down Expand Up @@ -220,7 +309,7 @@ watch(
<SectionPagination
v-if="
totalPages
!== 1"
!== 1"
class="pagination"
:pages="totalPages"
:initial-current-page="currentPage"
Expand Down Expand Up @@ -292,7 +381,7 @@ watch(
<SectionPagination
v-if="
totalPages
!== 1"
!== 1"
class="pagination"
:pages="totalPages"
:initial-current-page="currentPage"
Expand Down

0 comments on commit 22c93cd

Please sign in to comment.