Add locale to search
This commit is contained in:
parent
221033a1a1
commit
d19c96fc6d
2 changed files with 11 additions and 8 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from 'valtio'
|
||||||
|
|
||||||
import { appState } from '~utils/appState'
|
import { appState } from '~utils/appState'
|
||||||
|
|
@ -24,6 +25,9 @@ interface Props {
|
||||||
const SearchModal = (props: Props) => {
|
const SearchModal = (props: Props) => {
|
||||||
let { grid } = useSnapshot(appState)
|
let { grid } = useSnapshot(appState)
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
|
const locale = router.locale
|
||||||
|
|
||||||
let searchInput = React.createRef<HTMLInputElement>()
|
let searchInput = React.createRef<HTMLInputElement>()
|
||||||
|
|
||||||
const [objects, setObjects] = useState<{[id: number]: GridCharacter | GridWeapon | GridSummon}>()
|
const [objects, setObjects] = useState<{[id: number]: GridCharacter | GridWeapon | GridSummon}>()
|
||||||
|
|
@ -44,7 +48,7 @@ const SearchModal = (props: Props) => {
|
||||||
}, [searchInput])
|
}, [searchInput])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (query.length > 2)
|
if (query.length > 1)
|
||||||
fetchResults()
|
fetchResults()
|
||||||
}, [query])
|
}, [query])
|
||||||
|
|
||||||
|
|
@ -70,7 +74,7 @@ const SearchModal = (props: Props) => {
|
||||||
.filter(filterExclusions)
|
.filter(filterExclusions)
|
||||||
.map((o) => { return (o.object) ? o.object.name.en : undefined }).join(',') : ''
|
.map((o) => { return (o.object) ? o.object.name.en : undefined }).join(',') : ''
|
||||||
|
|
||||||
api.search(props.object, query, excludes)
|
api.search(props.object, query, excludes, locale)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
setResults(response.data)
|
setResults(response.data)
|
||||||
setTotalResults(response.data.length)
|
setTotalResults(response.data.length)
|
||||||
|
|
@ -149,8 +153,8 @@ const SearchModal = (props: Props) => {
|
||||||
|
|
||||||
if (query === '') {
|
if (query === '') {
|
||||||
string = `No ${props.object}`
|
string = `No ${props.object}`
|
||||||
} else if (query.length < 3) {
|
} else if (query.length < 2) {
|
||||||
string = `Type at least 3 characters`
|
string = `Type at least 2 characters`
|
||||||
} else {
|
} else {
|
||||||
string = `No results found for '${query}'`
|
string = `No results found for '${query}'`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import axios, { Axios, AxiosRequestConfig, AxiosResponse } from "axios"
|
import axios, { Axios, AxiosRequestConfig, AxiosResponse } from "axios"
|
||||||
import { appState } from "./appState"
|
|
||||||
|
|
||||||
interface Entity {
|
interface Entity {
|
||||||
name: string
|
name: string
|
||||||
|
|
@ -56,11 +55,11 @@ class Api {
|
||||||
return axios.post(`${ oauthUrl }/token`, object)
|
return axios.post(`${ oauthUrl }/token`, object)
|
||||||
}
|
}
|
||||||
|
|
||||||
search(object: string, query: string, excludes: string) {
|
search(object: string, query: string, excludes: string, locale: string = 'en') {
|
||||||
const resourceUrl = `${this.url}/${name}`
|
const resourceUrl = `${this.url}/${name}`
|
||||||
const url = (excludes.length > 0) ?
|
const url = (excludes.length > 0) ?
|
||||||
`${resourceUrl}search/${object}?query=${query}&excludes=${excludes}` :
|
`${resourceUrl}search/${object}?query=${query}&locale=${locale}&excludes=${excludes}` :
|
||||||
`${resourceUrl}search/${object}?query=${query}`
|
`${resourceUrl}search/${object}?query=${query}&locale=${locale}`
|
||||||
return axios.get(url)
|
return axios.get(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue