Add version selector to /[locale]/search: new /api/bible/versions endpoint; fetch versions per locale; refetch books on version change; pass version to search API; localized label.
This commit is contained in:
@@ -69,6 +69,8 @@ export default function SearchPage() {
|
||||
const [searchHistory, setSearchHistory] = useState<string[]>([])
|
||||
const [filters, setFilters] = useState<SearchFilter>({ testament: 'all', bookKeys: [], exactMatch: false })
|
||||
const [booksData, setBooksData] = useState<BookOption[]>([])
|
||||
const [versions, setVersions] = useState<Array<{ id: string; name: string; abbreviation: string; isDefault: boolean }>>([])
|
||||
const [selectedVersion, setSelectedVersion] = useState<string>('')
|
||||
|
||||
const oldTestamentBooks = booksData.filter(b => b.orderNum <= 39)
|
||||
const newTestamentBooks = booksData.filter(b => b.orderNum > 39)
|
||||
@@ -84,8 +86,26 @@ export default function SearchPage() {
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
// Fetch available versions for locale
|
||||
fetch(`/api/bible/versions?locale=${locale}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
const list = (data.versions || []) as Array<{ id: string; name: string; abbreviation: string; isDefault: boolean }>
|
||||
setVersions(list)
|
||||
const def = list.find(v => v.isDefault) || list[0]
|
||||
setSelectedVersion(def?.abbreviation || '')
|
||||
})
|
||||
.catch(() => {
|
||||
setVersions([])
|
||||
setSelectedVersion('')
|
||||
})
|
||||
}, [locale])
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedVersion && versions.length === 0) return
|
||||
// Fetch available books for current locale/version
|
||||
fetch(`/api/bible/books?locale=${locale}`)
|
||||
const qs = new URLSearchParams({ locale, ...(selectedVersion ? { version: selectedVersion } : {}) })
|
||||
fetch(`/api/bible/books?${qs}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
const mapped: BookOption[] = (data.books || []).map((b: any) => ({
|
||||
@@ -98,7 +118,7 @@ export default function SearchPage() {
|
||||
setBooksData(mapped)
|
||||
})
|
||||
.catch(() => setBooksData([]))
|
||||
}, [locale])
|
||||
}, [locale, selectedVersion, versions.length])
|
||||
|
||||
const handleSearch = async () => {
|
||||
if (!searchQuery.trim()) return
|
||||
@@ -117,6 +137,7 @@ export default function SearchPage() {
|
||||
exactMatch: filters.exactMatch.toString(),
|
||||
bookKeys: filters.bookKeys.join(','),
|
||||
locale,
|
||||
...(selectedVersion ? { version: selectedVersion } : {}),
|
||||
})
|
||||
|
||||
const response = await fetch(`/api/search/verses?${params}`)
|
||||
@@ -231,6 +252,21 @@ export default function SearchPage() {
|
||||
</Select>
|
||||
</FormControl>
|
||||
|
||||
<FormControl fullWidth sx={{ mb: 2 }}>
|
||||
<InputLabel>{t('filters.version')}</InputLabel>
|
||||
<Select
|
||||
value={selectedVersion}
|
||||
label={t('filters.version')}
|
||||
onChange={(e) => setSelectedVersion(e.target.value as string)}
|
||||
>
|
||||
{versions.map(v => (
|
||||
<MenuItem key={v.abbreviation} value={v.abbreviation}>
|
||||
{v.name} ({v.abbreviation})
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
</FormControl>
|
||||
|
||||
<Accordion>
|
||||
<AccordionSummary expandIcon={<ExpandMore />}>
|
||||
<Typography variant="body2">{t('filters.specificBooks')}</Typography>
|
||||
|
||||
Reference in New Issue
Block a user