forked from rupali-codes/LinksHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseFilterDB.ts
33 lines (25 loc) · 841 Bytes
/
useFilterDB.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { database } from 'database/data'
import { useRouter } from 'next/router'
import { IData } from 'types'
import { useResults } from './ResultsContext'
const useFilterDB = (subcategory: string) => {
const router = useRouter()
const { results } = useResults()
let pageCategory = subcategory
if (typeof window !== 'undefined') {
pageCategory = router.asPath.slice(1)
}
// This filters trough the DB with the subcategory which results in an array of arrays
const filterSubCat = database?.map((item) =>{
return item?.filter((cat: IData) => cat.subcategory === subcategory)
})
// This filters out an empty array from the filterSubCat
const filterDB = filterSubCat?.filter((item) => item.length !== 0)
return {
filterSubCat,
filterDB,
results,
pageCategory
}
}
export default useFilterDB