mirror of
https://github.com/acid-info/logos-press-engine.git
synced 2025-02-23 14:48:08 +00:00
fix: sort search results chronologically when no query is provided
This commit is contained in:
parent
f09d8255ad
commit
d1745dcd64
@ -10,6 +10,8 @@ export default async function handler(
|
||||
query: { q = '', tags: tagsString = '', type: typeString = '' },
|
||||
} = req
|
||||
|
||||
const query = Array.isArray(q) ? q.join(' ').trim() : q.trim()
|
||||
|
||||
const tags =
|
||||
typeof tagsString === 'string'
|
||||
? tagsString
|
||||
@ -43,7 +45,7 @@ export default async function handler(
|
||||
if (postTypes.length > 0) {
|
||||
const response = await strapiApi.searchPosts({
|
||||
tags,
|
||||
query: Array.isArray(q) ? q.join(' ').trim() : q.trim(),
|
||||
query,
|
||||
types: postTypes as LPE.PostType[],
|
||||
limit: 15,
|
||||
skip: 0,
|
||||
@ -52,6 +54,14 @@ export default async function handler(
|
||||
result.posts.push(...(response.data ?? []))
|
||||
}
|
||||
|
||||
if (query.trim().length === 0) {
|
||||
result.posts = result.posts.sort(
|
||||
(a, b) =>
|
||||
+new Date((b.data as LPE.Post.Document).publishedAt || 0) -
|
||||
+new Date((a.data as LPE.Post.Document).publishedAt || 0),
|
||||
)
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
data: result,
|
||||
})
|
||||
|
@ -80,6 +80,7 @@ export const postTransformer: Transformer<
|
||||
slug,
|
||||
modifiedAt: publishedAt,
|
||||
createdAt: publishedAt,
|
||||
publishedAt,
|
||||
coverImage,
|
||||
tags,
|
||||
content,
|
||||
|
@ -165,6 +165,7 @@ export namespace LPE {
|
||||
|
||||
createdAt: string | null
|
||||
modifiedAt: string | null
|
||||
publishedAt: string | null
|
||||
type: typeof LPE.PostTypes.Article
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user