INSERT INTO `cp_filejs` (`id`,`app`,`path`,`modul`,`file_name`,`content`) VALUES ('205','2','u6/driver','discover','js/search.js-','import {\r\n getDiscoverData\r\n} from \\\"./api/discover.js\\\";\r\nimport store from \\\"./store.js\\\";\r\n\r\nvar $ = Dom7;\r\n\r\n// let activeTab = \\\\\\\'all\\\\\\\';\r\nlet lastSearchText = \\\\\\\'\\\\\\\';\r\nlet controller; // To store the current AbortController\r\n\r\nvar searchResultsStore = store.getters.getSearchResults;\r\n\r\n$(document).on(\\\\\\\'page:afterin\\\\\\\', \\\\\\\'.page[data-name=\\\"search\\\"]\\\\\\\', async function (e) {\r\n $(\\\\\\\'#discover-search\\\\\\\').focus();\r\n})\r\n\r\n// event listener for tab change\r\n$(document).on(\\\\\\\'click\\\\\\\', \\\\\\\'.discovery-wrap .tab-link\\\\\\\', async function (e) {\r\n const type = this.getAttribute(\\\\\\\'data-type\\\\\\\')\r\n activeTab = type\r\n})\r\n\r\nfunction debounce(func, delay) {\r\n let timer;\r\n return function (...args) {\r\n clearTimeout(timer);\r\n timer = setTimeout(() => func.apply(this, args), delay);\r\n };\r\n}\r\n\r\nfunction addLoaderToTabs() {\r\n const eventsContainer = document.querySelector(\\\\\\\'#events-results .list ul\\\\\\\');\r\n const usersContainer = document.querySelector(\\\\\\\'#users_vehicles-results .list ul\\\\\\\');\r\n const venuesContainer = document.querySelector(\\\\\\\'#venues-results .list ul\\\\\\\');\r\n const topContainer = document.querySelector(\\\\\\\'#top-results .list\\\\\\\');\r\n\r\n const loader = `\r\n No results found \\\\\\\';\r\n\r\n if (!items || items.length === 0) {\r\n container.innerHTML = noResultsMessage;\r\n return;\r\n }\r\n\r\n // Render each item\r\n items.forEach(item => {\r\n const li = document.createElement(\\\\\\\'li\\\\\\\');\r\n li.innerHTML = renderItem(item);\r\n container.appendChild(li);\r\n });\r\n}\r\n\r\n// Function to render the search results\r\nfunction renderSearchResults(searchResults) {\r\n const eventsContainer = document.querySelector(\\\\\\\'#events-results .list ul\\\\\\\');\r\n const usersContainer = document.querySelector(\\\\\\\'#users_vehicles-results .list ul\\\\\\\');\r\n const venuesContainer = document.querySelector(\\\\\\\'#venues-results .list ul\\\\\\\');\r\n const topContainer = document.querySelector(\\\\\\\'#top-results .list\\\\\\\');\r\n\r\n // Ensure containers are cleared before rendering\r\n eventsContainer.innerHTML = \\\\\\\'\\\\\\\';\r\n usersContainer.innerHTML = \\\\\\\'\\\\\\\';\r\n venuesContainer.innerHTML = \\\\\\\'\\\\\\\';\r\n topContainer.innerHTML = \\\\\\\'\\\\\\\';\r\n\r\n // Render events\r\n if (searchResults.events) {\r\n renderList(eventsContainer, searchResults.events.data, event => `\r\n \r\n
\r\n
`;\r\n\r\n eventsContainer.innerHTML = loader;\r\n usersContainer.innerHTML = loader;\r\n venuesContainer.innerHTML = loader;\r\n topContainer.innerHTML = loader;\r\n}\r\n\r\nconst debouncedSearch = debounce(async function () {\r\n const search = $(this).val().trim();\r\n\r\n if (search.length < 3 || search === lastSearchText) {\r\n return;\r\n }\r\n\r\n lastSearchText = search;\r\n\r\n // Abort the previous request if it\\\\\\\'s still ongoing\r\n if (controller) {\r\n controller.abort();\r\n }\r\n\r\n // Create a new AbortController for the current request\r\n controller = new AbortController();\r\n const signal = controller.signal;\r\n\r\n addLoaderToTabs();\r\n try {\r\n const searchResults = await getDiscoverData(search, \\\\\\\'all\\\\\\\', 1, signal);\r\n store.dispatch(\\\\\\\'setSearchResults\\\\\\\', searchResults);\r\n } catch (error) {\r\n if (error.name === \\\\\\\'AbortError\\\\\\\') {\r\n console.log(\\\\\\\'Fetch aborted\\\\\\\');\r\n } else {\r\n console.error(\\\\\\\'Error fetching search results:\\\\\\\', error);\r\n }\r\n }\r\n}, 300); // Adjust the delay (in milliseconds) as needed\r\n\r\n$(document).on(\\\\\\\'change input paste\\\\\\\', \\\\\\\'#discover-search\\\\\\\', debouncedSearch);\r\n\r\n// Function to render a list of items in the DOM\r\nfunction renderList(container, items, renderItem) {\r\n container.innerHTML = \\\\\\\'\\\\\\\';\r\n\r\n const noResultsMessage = \\\\\\\'\r\n \r\n
\r\n \r\n \r\n
\r\n \r\n
\r\n \r\n `);\r\n }\r\n\r\n // Render users\r\n if (searchResults.users) {\r\n renderList(usersContainer, searchResults.users.data, (user) => {\r\n const userLink = user.type == \\\\\\\'user\\\\\\\' ? \\\\\\\'/profile-view/\\\\\\\' + user.id : \\\\\\\'/profile-garage-vehicle-view/\\\\\\\' + user.id;\r\n let contentText;\r\n\r\n if (user.type == \\\\\\\'user\\\\\\\') {\r\n contentText = `${user.name} (${user.username})`;\r\n }\r\n\r\n if (user.type == \\\\\\\'vehicle\\\\\\\') {\r\n contentText = `${user.name}\\\\\\\'s ${user.meta.make} ${user.meta.model}`;\r\n }\r\n\r\n return `\r\n \r\n ${event.name}
\r\n \r\n \r\n
\r\n \r\n
\r\n `\r\n });\r\n }\r\n\r\n // Render venues\r\n if (searchResults.venues) {\r\n renderList(venuesContainer, searchResults.venues.data, venue => `\r\n \r\n ${contentText}
\r\n \r\n \r\n
\r\n \r\n
\r\n \r\n `);\r\n }\r\n\r\n // Render top results\r\n let hasTopResults = false;\r\n\r\n if (searchResults.top_results) {\r\n if (searchResults.top_results.events && searchResults.top_results.events.length > 0) {\r\n hasTopResults = true;\r\n const eventSubList = document.createElement(\\\\\\\'ul\\\\\\\');\r\n const heading = document.createElement(\\\\\\\'h2\\\\\\\');\r\n\r\n heading.innerHTML = \\\\\\\'Trending Events\\\\\\\';\r\n heading.classList.add(\\\\\\\'section-title\\\\\\\', \\\\\\\'mt-3\\\\\\\', \\\\\\\'mb-2\\\\\\\');\r\n topContainer.appendChild(heading);\r\n topContainer.appendChild(eventSubList);\r\n\r\n renderList(eventSubList, searchResults.top_results.events, event => `\r\n \r\n ${venue.name} - ${venue.venue_location}
\r\n \r\n \r\n
\r\n \r\n
\r\n \r\n `);\r\n }\r\n\r\n if (searchResults.top_results.users && searchResults.top_results.users.length > 0) {\r\n hasTopResults = true;\r\n\r\n const userSubList = document.createElement(\\\\\\\'ul\\\\\\\');\r\n const heading = document.createElement(\\\\\\\'h2\\\\\\\');\r\n\r\n heading.innerHTML = \\\\\\\'Trending Users\\\\\\\';\r\n heading.classList.add(\\\\\\\'section-title\\\\\\\', \\\\\\\'mt-3\\\\\\\', \\\\\\\'mb-2\\\\\\\');\r\n topContainer.appendChild(heading);\r\n topContainer.appendChild(userSubList);\r\n\r\n renderList(userSubList, searchResults.top_results.users, (user) => {\r\n const userLink = user.type == \\\\\\\'user\\\\\\\' ? \\\\\\\'/profile-view/\\\\\\\' + user.id : \\\\\\\'/profile-garage-vehicle-view/\\\\\\\' + user.id;\r\n let contentText;\r\n\r\n if (user.type == \\\\\\\'user\\\\\\\') {\r\n contentText = `${user.name} (${user.username})`;\r\n }\r\n\r\n if (user.type == \\\\\\\'vehicle\\\\\\\') {\r\n contentText = `${user.name}\\\\\\\'s ${user.meta.make} ${user.meta.model}`;\r\n }\r\n\r\n return `\r\n \r\n ${event.name}
\r\n \r\n \r\n
\r\n \r\n
\r\n `\r\n });\r\n }\r\n\r\n if (searchResults.top_results.venues && searchResults.top_results.venues.length > 0) {\r\n hasTopResults = true;\r\n\r\n const venueSubList = document.createElement(\\\\\\\'ul\\\\\\\');\r\n const heading = document.createElement(\\\\\\\'h2\\\\\\\');\r\n\r\n heading.innerHTML = \\\\\\\'Trending Venues\\\\\\\';\r\n heading.classList.add(\\\\\\\'section-title\\\\\\\', \\\\\\\'mt-3\\\\\\\', \\\\\\\'mb-2\\\\\\\');\r\n\r\n topContainer.appendChild(heading);\r\n topContainer.appendChild(venueSubList);\r\n\r\n renderList(venueSubList, searchResults.top_results.venues, venue => `\r\n \r\n ${contentText}
\r\n \r\n \r\n
\r\n \r\n
\r\n \r\n `);\r\n }\r\n }\r\n\r\n if (!hasTopResults) {\r\n topContainer.innerHTML = \\\\\\\'${venue.name} - ${venue.venue_location}
\r\n No top results found
\\\\\\\';\r\n }\r\n}\r\n\r\nsearchResultsStore.onUpdated((results) => {\r\n renderSearchResults(results);\r\n});');