Skip to content

Commit

Permalink
fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
Abirami committed Oct 17, 2024
1 parent 3affd36 commit 29d660a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 86 deletions.
53 changes: 0 additions & 53 deletions src/client/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,4 @@ import './plant-search.js'
import './pest-search.js'
import './country-search.js'

function compareNames(a, b) {
if (a.text < b.text) {
return -1
}
if (a.text > b.text) {
return 1
}
return 0
}

export function timerFunction(query, finalArray, populateResults) {
return setTimeout(async () => {
try {
if (query.length > 2) {
return populateResults(finalArray.sort(compareNames))
}
} catch (error) {
// TypeError: Failed to fetch
// console.log('Error fetching suggestions:', error)
}
}, 1000)
}

export function inputValueTemplate(asd) {
return asd?.text
}

export function suggestionTemplate(asd, regexValue) {
const inputElementCustom =
document.getElementsByClassName('custom-hint-class')
inputElementCustom[0]?.setAttribute('aria-label', 'autocomplete__hint')
inputElementCustom[0]?.setAttribute('id', 'autocomplete__hint')
let template
if (regexValue?.length > 0) {
template =
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.text?.replace(
new RegExp(regexValue, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
} else {
template =
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.replace(
new RegExp(asd, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
}
return template
}

initAll()
58 changes: 47 additions & 11 deletions src/client/assets/javascripts/country-search.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import accessibleAutocomplete from './accessible-autocomplete.min.js'
import {
timerFunction,
inputValueTemplate,
suggestionTemplate
} from './application.js'

let finalArray = []
let timer
Expand All @@ -16,10 +11,29 @@ async function fetchSuggestions(query, populateResults) {
defaultcountryCode?.setAttribute('value', null)
const apiUrl = '/search/countries?searchQuery=' + query
await clearTimeout(timer)
const response = await fetch(apiUrl)
const responseJSON = await response.json()
await renderSuggestions(responseJSON, query)
timer = timerFunction(query, finalArray, populateResults)
function compareNames(a, b) {
if (a.text < b.text) {
return -1
}
if (a.text > b.text) {
return 1
}
return 0
}

timer = setTimeout(async () => {
try {
if (query.length > 2) {
const response = await fetch(apiUrl)
const responseJSON = await response.json()
await renderSuggestions(responseJSON, query)
return populateResults(finalArray.sort(compareNames))
}
} catch (error) {
// TypeError: Failed to fetch
// console.log('Error fetching suggestions:', error)
}
}, 1000)
}
let regexValue, suggestions
async function renderSuggestions(json, query) {
Expand Down Expand Up @@ -82,10 +96,32 @@ if (document.querySelector('#my-autocomplete-country-container')) {
showNoOptionsFound: false,
templates: {
inputValue: function (asd) {
return inputValueTemplate(asd)
return asd?.text
},
suggestion: function (asd) {
return suggestionTemplate(asd, regexValue)
const inputElementCustom =
document.getElementsByClassName('custom-hint-class')
inputElementCustom[0]?.setAttribute('aria-label', 'autocomplete__hint')
inputElementCustom[0]?.setAttribute('id', 'autocomplete__hint')
if (regexValue?.length > 0) {
return (
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.text?.replace(
new RegExp(regexValue, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
)
} else {
return (
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.replace(
new RegExp(asd, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
)
}
}
},
onConfirm
Expand Down
59 changes: 48 additions & 11 deletions src/client/assets/javascripts/pest-search.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import accessibleAutocomplete from './accessible-autocomplete.min.js'
import {
timerFunction,
inputValueTemplate,
suggestionTemplate
} from './application.js'

let finalArray = []
let timer

Expand All @@ -15,10 +11,29 @@ async function fetchSuggestions(query, populateResults) {
defaultcslref?.setAttribute('value', null)
const apiUrl = '/search/pests?searchQuery=' + query
await clearTimeout(timer)
const response = await fetch(apiUrl)
const responseJSON = await response.json()
await renderSuggestions(responseJSON, query)
timer = timerFunction(query, finalArray, populateResults)
function compareNames(a, b) {
if (a.text < b.text) {
return -1
}
if (a.text > b.text) {
return 1
}
return 0
}

timer = setTimeout(async () => {
try {
if (query.length > 2) {
const response = await fetch(apiUrl)
const responseJSON = await response.json()
await renderSuggestions(responseJSON, query)
return populateResults(finalArray.sort(compareNames))
}
} catch (error) {
// TypeError: Failed to fetch
// console.log('Error fetching suggestions:', error)
}
}, 1000)
}
let regexValue
async function renderSuggestions(json, query) {
Expand Down Expand Up @@ -326,10 +341,32 @@ if (document.querySelector('#my-autocomplete-pest-container')) {
showNoOptionsFound: false,
templates: {
inputValue: function (asd) {
return inputValueTemplate(asd)
return asd?.text
},
suggestion: function (asd) {
return suggestionTemplate(asd, regexValue)
const inputElementCustom =
document.getElementsByClassName('custom-hint-class')
inputElementCustom[0]?.setAttribute('aria-label', 'autocomplete__hint')
inputElementCustom[0]?.setAttribute('id', 'autocomplete__hint')
if (regexValue?.length > 0) {
return (
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.text?.replace(
new RegExp(regexValue, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
)
} else {
return (
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.replace(
new RegExp(asd, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
)
}
}
},
onConfirm
Expand Down
59 changes: 48 additions & 11 deletions src/client/assets/javascripts/plant-search.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import accessibleAutocomplete from './accessible-autocomplete.min.js'
import {
timerFunction,
inputValueTemplate,
suggestionTemplate
} from './application.js'

let finalArray = []
let timer
Expand All @@ -15,10 +10,30 @@ async function fetchSuggestions(query, populateResults) {
defaulthostref?.setAttribute('value', '')
const apiUrl = '/search/plants?searchQuery=' + query
await clearTimeout(timer)
const response = await fetch(apiUrl)
const responseJSON = await response.json()
await renderSuggestions(responseJSON, query)
timer = timerFunction(query, finalArray, populateResults)
function compareNames(a, b) {
if (a.text < b.text) {
return -1
}
if (a.text > b.text) {
return 1
}
return 0
}

timer = setTimeout(async () => {
try {
if (query.length > 2) {
const response = await fetch(apiUrl)
const responseJSON = await response.json()

await renderSuggestions(responseJSON, query)
return populateResults(finalArray.sort(compareNames))
}
} catch (error) {
// TypeError: Failed to fetch
// console.log('Error fetching suggestions:', error)
}
}, 1000)
}
let regexValue
async function renderSuggestions(json, query) {
Expand Down Expand Up @@ -316,10 +331,32 @@ if (document.querySelector('#my-autocomplete-container')) {
showNoOptionsFound: false,
templates: {
inputValue: function (asd) {
return inputValueTemplate(asd)
return asd?.text
},
suggestion: function (asd) {
return suggestionTemplate(asd, regexValue)
const inputElementCustom =
document.getElementsByClassName('custom-hint-class')
inputElementCustom[0]?.setAttribute('aria-label', 'autocomplete__hint')
inputElementCustom[0]?.setAttribute('id', 'autocomplete__hint')
if (regexValue?.length > 0) {
return (
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.text?.replace(
new RegExp(regexValue, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
)
} else {
return (
'<div class="suggestions"><span class="name" id="resultName">' +
asd?.replace(
new RegExp(asd, 'gi'),
(match) => `<strong>${match}</strong>`
) +
'</span></div>'
)
}
}
},
onConfirm
Expand Down

0 comments on commit 29d660a

Please sign in to comment.