|
1 | | -import { useState } from "react"; |
2 | | -import { useSearchParams } from "react-router-dom"; |
3 | | -import { ArtistAutocompleteQuery, useArtistAutocompleteQuery } from "../generated"; |
4 | | -import { generateGQLSearchQueryVars } from "../helpers/queryCacheHelper"; |
5 | | -import { isEditOrPreviewMode } from "../helpers/urlHelper"; |
| 1 | +import {useEffect, useRef, useState} from "react"; |
| 2 | +import {useSearchParams} from "react-router-dom"; |
| 3 | +import {ArtistAutocompleteQuery, Ranking, useArtistAutocompleteQuery} from "../generated"; |
| 4 | +import {generateGQLSearchQueryVars} from "../helpers/queryCacheHelper"; |
| 5 | +import {getRankingFromSearchParams, isEditOrPreviewMode} from "../helpers/urlHelper"; |
6 | 6 |
|
7 | 7 | type CustomString = string | number | readonly string[] | undefined |
8 | 8 |
|
9 | 9 | function SearchButton({filterValue}: any): JSX.Element { |
| 10 | + const isFirstLoad = useRef(true); |
10 | 11 | const singleKeyUrl = process.env.REACT_APP_CONTENT_GRAPH_GATEWAY_URL as string |
11 | 12 | const ARTIST = "Artist" |
12 | 13 | const [searchParams] = useSearchParams() |
13 | 14 | const [token, setToken] = useState("") |
14 | 15 | const [isShown, setIsShown] = useState(false) |
15 | 16 | const [searchValue, setSearchValue] = useState<CustomString>(searchParams.get("q")?.toString() ?? "") |
| 17 | + const [ranking, setRanking] = useState<Ranking>(getRankingFromSearchParams(searchParams)) |
16 | 18 | const [orderBy] = useState("ASC") |
17 | | - let autocompleteData : ArtistAutocompleteQuery | undefined = undefined |
18 | | - |
| 19 | + |
19 | 20 | let modeEdit = isEditOrPreviewMode() |
20 | | - let variables = generateGQLSearchQueryVars(token, window.location.pathname, searchValue as string | null, orderBy); |
21 | | - const { data : artistAutocompleteData } = useArtistAutocompleteQuery({ endpoint: singleKeyUrl }, variables, { staleTime: 2000, enabled: !modeEdit || !!token }) |
22 | | - autocompleteData = artistAutocompleteData |
23 | | - |
24 | | - function search(event: any, action: string){ |
25 | | - if ((action == "keypress" && event.charCode === 13) || action == "buttonclick") { |
26 | | - window.location.href = `${window.location.origin}/search?q=${searchValue}&f=${filterValue ?? ARTIST}` |
| 21 | + let variables = generateGQLSearchQueryVars(token, window.location.pathname, searchValue as string | null, orderBy, ranking); |
| 22 | + const autocompleteData = useArtistAutocompleteQuery({endpoint: singleKeyUrl}, variables, { |
| 23 | + staleTime: 2000, |
| 24 | + enabled: !modeEdit || !!token |
| 25 | + }).data; |
| 26 | + |
| 27 | + const onSearch = (event: any) => { |
| 28 | + window.location.href = `${window.location.origin}/search?q=${searchValue}&f=${filterValue ?? ARTIST}&r=${ranking}`; |
| 29 | + } |
| 30 | + |
| 31 | + const onPressSearch = (event: any) => { |
| 32 | + if (event.key === 'Enter' || event.keyCode === 13) { |
| 33 | + onSearch(event); |
27 | 34 | } |
28 | 35 | } |
29 | 36 |
|
30 | | - function onValueChange(event: any){ |
| 37 | + const onValueChange = (event: any) => { |
31 | 38 | setSearchValue(event.target.value); |
32 | 39 | event.target.value !== undefined ? setIsShown(true) : setIsShown(false); |
33 | 40 | } |
34 | 41 |
|
35 | | - function onAutoClick(event: any){ |
| 42 | + const onAutoClick = (event: any) => { |
36 | 43 | setSearchValue(event.target.textContent); |
37 | | - window.location.href = `${window.location.origin}/search?q=${event.target.textContent}&f=${filterValue ?? ARTIST}` |
| 44 | + window.location.href = `${window.location.origin}/search?q=${event.target.textContent}&f=${filterValue ?? ARTIST}&r=${ranking}`; |
| 45 | + } |
| 46 | + |
| 47 | + const onChangeSearchType = (event: any) => { |
| 48 | + if (event.target.checked) { |
| 49 | + setRanking(Ranking.Semantic); |
| 50 | + } else { |
| 51 | + setRanking(Ranking.Relevance); |
| 52 | + } |
38 | 53 | } |
39 | 54 |
|
| 55 | + useEffect(() => { |
| 56 | + if (isFirstLoad.current) { |
| 57 | + isFirstLoad.current = false; |
| 58 | + } else { |
| 59 | + if (searchValue) { |
| 60 | + window.location.href = `${window.location.origin}/search?q=${searchValue}&f=${filterValue ?? ARTIST}&r=${ranking}`; |
| 61 | + } |
| 62 | + } |
| 63 | + }, [ranking]); |
| 64 | + |
40 | 65 | return ( |
41 | | - <div> |
42 | | - <div className="nav-table-cell autocomplete"> |
43 | | - <input className="search-input" |
44 | | - type="text" |
45 | | - id="search-input" |
46 | | - placeholder="Search" |
47 | | - onKeyPress={(event) => {search(event, 'keypress')}} |
48 | | - value={searchValue} |
49 | | - onChange={onValueChange} |
50 | | - onFocus={() => setIsShown(true)} |
51 | | - onBlur={() => |
52 | | - setTimeout(() => { |
53 | | - setIsShown(false); |
54 | | - }, 150) |
55 | | - } /> |
56 | | - <a className="search-icon" onClick={(event) => {search(event, 'buttonclick')}}> |
57 | | - <i className="fa fa-search"></i> |
58 | | - </a> |
59 | | - <div className="autocomplete-block" style={isShown ? {display: "inherit"}: {display: "none"}}> |
| 66 | + <div className="d-flex Grid--alignMiddle"> |
| 67 | + <div className="nav-table-cell autocomplete me-3"> |
| 68 | + <input className="search-input" |
| 69 | + type="text" |
| 70 | + id="search-input" |
| 71 | + placeholder="Search" |
| 72 | + onKeyUp={onPressSearch} |
| 73 | + value={searchValue} |
| 74 | + onChange={onValueChange} |
| 75 | + onFocus={() => setIsShown(true)} |
| 76 | + onBlur={() => |
| 77 | + setTimeout(() => { |
| 78 | + setIsShown(false); |
| 79 | + }, 150) |
| 80 | + }/> |
| 81 | + <a className="search-icon" onClick={onSearch}> |
| 82 | + <i className="fa fa-search"></i> |
| 83 | + </a> |
| 84 | + <div className="autocomplete-block" style={isShown ? {display: "inherit"} : {display: "none"}}> |
60 | 85 | { |
61 | 86 | autocompleteData?.ArtistDetailsPage?.autocomplete?.ArtistName?.map((name, idx) => { |
62 | | - return( |
63 | | - <div key={idx} onClick={(event) => onAutoClick(event)}>{name}</div> |
| 87 | + return ( |
| 88 | + <div key={idx} onClick={onAutoClick}>{name}</div> |
64 | 89 | ) |
65 | | - }) |
| 90 | + }) |
66 | 91 | } |
67 | 92 | { |
68 | 93 | autocompleteData?.ArtistDetailsPage?.autocomplete?.StageName?.map((name, idx) => { |
69 | | - return( |
70 | | - <div key={idx} onClick={(event) => onAutoClick(event)}>{name}</div> |
| 94 | + return ( |
| 95 | + <div key={idx} onClick={onAutoClick}>{name}</div> |
71 | 96 | ) |
72 | 97 | }) |
73 | 98 | } |
74 | 99 | </div> |
75 | 100 | </div> |
| 101 | + <div> |
| 102 | + <div className="switch_box"> |
| 103 | + <input type="checkbox" className="switch_input" onChange={onChangeSearchType} |
| 104 | + checked={ranking === Ranking.Semantic}></input> |
| 105 | + <label className="fw-semibold mb-0 ms-2">Semantic Search</label> |
| 106 | + </div> |
| 107 | + </div> |
76 | 108 | </div> |
77 | 109 | ); |
78 | 110 | } |
|
0 commit comments