Search filters now backed by DB per locale/version: fetch books from /api/bible/books; send bookKeys + locale to API; API constrains by versionId and bookKey/name; keeps testament filter.
This commit is contained in:
@@ -35,7 +35,6 @@ import {
|
||||
} from '@mui/icons-material'
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useTranslations, useLocale } from 'next-intl'
|
||||
import { translateBookName } from '@/lib/book-translations'
|
||||
|
||||
interface SearchResult {
|
||||
id: string
|
||||
@@ -48,10 +47,18 @@ interface SearchResult {
|
||||
|
||||
interface SearchFilter {
|
||||
testament: 'all' | 'old' | 'new'
|
||||
books: string[]
|
||||
bookKeys: string[]
|
||||
exactMatch: boolean
|
||||
}
|
||||
|
||||
interface BookOption {
|
||||
id: string
|
||||
name: string
|
||||
bookKey: string
|
||||
orderNum: number
|
||||
testament: string
|
||||
}
|
||||
|
||||
export default function SearchPage() {
|
||||
const theme = useTheme()
|
||||
const t = useTranslations('pages.search')
|
||||
@@ -60,23 +67,11 @@ export default function SearchPage() {
|
||||
const [results, setResults] = useState<SearchResult[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [searchHistory, setSearchHistory] = useState<string[]>([])
|
||||
const [filters, setFilters] = useState<SearchFilter>({
|
||||
testament: 'all',
|
||||
books: [],
|
||||
exactMatch: false,
|
||||
})
|
||||
const [filters, setFilters] = useState<SearchFilter>({ testament: 'all', bookKeys: [], exactMatch: false })
|
||||
const [booksData, setBooksData] = useState<BookOption[]>([])
|
||||
|
||||
const oldTestamentBooks = [
|
||||
'Geneza', 'Exodul', 'Leviticul', 'Numerii', 'Deuteronomul',
|
||||
'Iosua', 'Judecătorii', 'Rut', '1 Samuel', '2 Samuel',
|
||||
'Psalmii', 'Proverbele', 'Isaia', 'Ieremia', 'Daniel'
|
||||
]
|
||||
|
||||
const newTestamentBooks = [
|
||||
'Matei', 'Marcu', 'Luca', 'Ioan', 'Faptele Apostolilor',
|
||||
'Romani', '1 Corinteni', '2 Corinteni', 'Galateni', 'Efeseni',
|
||||
'Filipeni', 'Coloseni', 'Evrei', 'Iacob', '1 Petru', 'Apocalipsa'
|
||||
]
|
||||
const oldTestamentBooks = booksData.filter(b => b.orderNum <= 39)
|
||||
const newTestamentBooks = booksData.filter(b => b.orderNum > 39)
|
||||
|
||||
const popularSearches: string[] = t.raw('popular.items')
|
||||
|
||||
@@ -88,6 +83,23 @@ export default function SearchPage() {
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
// Fetch available books for current locale/version
|
||||
fetch(`/api/bible/books?locale=${locale}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
const mapped: BookOption[] = (data.books || []).map((b: any) => ({
|
||||
id: b.id,
|
||||
name: b.name,
|
||||
bookKey: b.bookKey,
|
||||
orderNum: b.orderNum,
|
||||
testament: b.testament,
|
||||
}))
|
||||
setBooksData(mapped)
|
||||
})
|
||||
.catch(() => setBooksData([]))
|
||||
}, [locale])
|
||||
|
||||
const handleSearch = async () => {
|
||||
if (!searchQuery.trim()) return
|
||||
|
||||
@@ -103,7 +115,8 @@ export default function SearchPage() {
|
||||
q: searchQuery,
|
||||
testament: filters.testament,
|
||||
exactMatch: filters.exactMatch.toString(),
|
||||
books: filters.books.join(','),
|
||||
bookKeys: filters.bookKeys.join(','),
|
||||
locale,
|
||||
})
|
||||
|
||||
const response = await fetch(`/api/search/verses?${params}`)
|
||||
@@ -228,15 +241,16 @@ export default function SearchPage() {
|
||||
.concat(filters.testament === 'new' || filters.testament === 'all' ? newTestamentBooks : [])
|
||||
.map((book) => (
|
||||
<Chip
|
||||
key={book}
|
||||
label={translateBookName(book, locale)}
|
||||
key={book.bookKey}
|
||||
label={book.name}
|
||||
size="small"
|
||||
variant={filters.books.includes(book) ? 'filled' : 'outlined'}
|
||||
variant={filters.bookKeys.includes(book.bookKey) ? 'filled' : 'outlined'}
|
||||
onClick={() => {
|
||||
const newBooks = filters.books.includes(book)
|
||||
? filters.books.filter(b => b !== book)
|
||||
: [...filters.books, book]
|
||||
setFilters({ ...filters, books: newBooks })
|
||||
const exists = filters.bookKeys.includes(book.bookKey)
|
||||
const newBookKeys = exists
|
||||
? filters.bookKeys.filter(b => b !== book.bookKey)
|
||||
: [...filters.bookKeys, book.bookKey]
|
||||
setFilters({ ...filters, bookKeys: newBookKeys })
|
||||
}}
|
||||
sx={{ mb: 0.5, mr: 0.5 }}
|
||||
/>
|
||||
@@ -330,10 +344,10 @@ export default function SearchPage() {
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
{filters.books.length > 0 && (
|
||||
{filters.bookKeys.length > 0 && (
|
||||
<Box sx={{ mt: 2 }}>
|
||||
<Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
|
||||
{t('searchIn', { books: filters.books.map(b => translateBookName(b, locale)).join(', ') })}
|
||||
{t('searchIn', { books: filters.bookKeys.map(k => booksData.find(b => b.bookKey === k)?.name || k).join(', ') })}
|
||||
</Typography>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
@@ -10,6 +10,9 @@ export async function GET(request: NextRequest) {
|
||||
const testament = searchParams.get('testament') || 'all'
|
||||
const exactMatch = searchParams.get('exactMatch') === 'true'
|
||||
const books = searchParams.get('books')?.split(',').filter(Boolean) || []
|
||||
const bookKeys = searchParams.get('bookKeys')?.split(',').filter(Boolean) || []
|
||||
const locale = (searchParams.get('locale') || 'ro').toLowerCase()
|
||||
const versionAbbr = searchParams.get('version') || undefined
|
||||
|
||||
if (!query) {
|
||||
return NextResponse.json(
|
||||
@@ -22,6 +25,28 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
// Resolve Bible version to constrain search to selected language/version
|
||||
let bibleVersion: any
|
||||
if (versionAbbr) {
|
||||
bibleVersion = await prisma.bibleVersion.findFirst({
|
||||
where: { abbreviation: versionAbbr, language: { in: [locale, locale.toLowerCase(), locale.toUpperCase()] } }
|
||||
})
|
||||
} else {
|
||||
bibleVersion = await prisma.bibleVersion.findFirst({
|
||||
where: { language: { in: [locale, locale.toLowerCase(), locale.toUpperCase()] }, isDefault: true }
|
||||
})
|
||||
if (!bibleVersion) {
|
||||
bibleVersion = await prisma.bibleVersion.findFirst({
|
||||
where: { language: { in: [locale, locale.toLowerCase(), locale.toUpperCase()] } },
|
||||
orderBy: { createdAt: 'asc' }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (!bibleVersion) {
|
||||
return NextResponse.json({ success: true, results: [], total: 0 })
|
||||
}
|
||||
|
||||
// Build search conditions
|
||||
const searchConditions: any = {}
|
||||
|
||||
@@ -39,51 +64,57 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
// Testament filter
|
||||
let testamentFilter = {}
|
||||
// Testament filter (by orderNum threshold)
|
||||
let testamentFilter: any = {}
|
||||
if (testament === 'old') {
|
||||
// Old Testament books (approximate book IDs 1-39)
|
||||
testamentFilter = {
|
||||
chapter: {
|
||||
book: {
|
||||
orderNum: {
|
||||
lte: 39
|
||||
}
|
||||
versionId: bibleVersion.id,
|
||||
orderNum: { lte: 39 }
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (testament === 'new') {
|
||||
// New Testament books (approximate book IDs 40+)
|
||||
testamentFilter = {
|
||||
chapter: {
|
||||
book: {
|
||||
orderNum: {
|
||||
gt: 39
|
||||
}
|
||||
versionId: bibleVersion.id,
|
||||
orderNum: { gt: 39 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Books filter
|
||||
let booksFilter = {}
|
||||
if (books.length > 0) {
|
||||
let booksFilter: any = {}
|
||||
if (bookKeys.length > 0) {
|
||||
booksFilter = {
|
||||
chapter: {
|
||||
book: {
|
||||
name: {
|
||||
in: books
|
||||
}
|
||||
versionId: bibleVersion.id,
|
||||
bookKey: { in: bookKeys }
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (books.length > 0) {
|
||||
booksFilter = {
|
||||
chapter: {
|
||||
book: {
|
||||
versionId: bibleVersion.id,
|
||||
name: { in: books }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Combine all filters
|
||||
const baseVersionScope = { chapter: { book: { versionId: bibleVersion.id } } }
|
||||
const whereCondition = {
|
||||
...searchConditions,
|
||||
...baseVersionScope,
|
||||
...testamentFilter,
|
||||
...booksFilter
|
||||
...booksFilter,
|
||||
}
|
||||
|
||||
// Search verses
|
||||
|
||||
Reference in New Issue
Block a user