mirror of
https://github.com/acid-info/logos-press-engine.git
synced 2025-02-24 07:08:12 +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 = '' },
|
query: { q = '', tags: tagsString = '', type: typeString = '' },
|
||||||
} = req
|
} = req
|
||||||
|
|
||||||
|
const query = Array.isArray(q) ? q.join(' ').trim() : q.trim()
|
||||||
|
|
||||||
const tags =
|
const tags =
|
||||||
typeof tagsString === 'string'
|
typeof tagsString === 'string'
|
||||||
? tagsString
|
? tagsString
|
||||||
@ -43,7 +45,7 @@ export default async function handler(
|
|||||||
if (postTypes.length > 0) {
|
if (postTypes.length > 0) {
|
||||||
const response = await strapiApi.searchPosts({
|
const response = await strapiApi.searchPosts({
|
||||||
tags,
|
tags,
|
||||||
query: Array.isArray(q) ? q.join(' ').trim() : q.trim(),
|
query,
|
||||||
types: postTypes as LPE.PostType[],
|
types: postTypes as LPE.PostType[],
|
||||||
limit: 15,
|
limit: 15,
|
||||||
skip: 0,
|
skip: 0,
|
||||||
@ -52,6 +54,14 @@ export default async function handler(
|
|||||||
result.posts.push(...(response.data ?? []))
|
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({
|
res.status(200).json({
|
||||||
data: result,
|
data: result,
|
||||||
})
|
})
|
||||||
|
@ -80,6 +80,7 @@ export const postTransformer: Transformer<
|
|||||||
slug,
|
slug,
|
||||||
modifiedAt: publishedAt,
|
modifiedAt: publishedAt,
|
||||||
createdAt: publishedAt,
|
createdAt: publishedAt,
|
||||||
|
publishedAt,
|
||||||
coverImage,
|
coverImage,
|
||||||
tags,
|
tags,
|
||||||
content,
|
content,
|
||||||
|
@ -165,6 +165,7 @@ export namespace LPE {
|
|||||||
|
|
||||||
createdAt: string | null
|
createdAt: string | null
|
||||||
modifiedAt: string | null
|
modifiedAt: string | null
|
||||||
|
publishedAt: string | null
|
||||||
type: typeof LPE.PostTypes.Article
|
type: typeof LPE.PostTypes.Article
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user