File indexing completed on 2025-01-26 05:27:59
0001 import React, { useState ,useContext} from 'react'; 0002 import Autosuggest from 'react-autosuggest'; 0003 import {MetaheaderContext} from '../contexts/MetaheaderContext'; 0004 0005 function renderSuggestion(suggestion) { 0006 return ( 0007 <div className={suggestion.type + ' suggestionsContainer'}> 0008 <div> 0009 {suggestion.type == 'user' ? <img style={{ width: '50px', height: '50px', borderRadius: '999px' }} src={suggestion.image_small} ></img> : <img style={{ width: '50px', height: '50px' }} src={suggestion.image_small} ></img>} 0010 </div> 0011 0012 <div className="description"> 0013 {suggestion.type == 'project' ? ( 0014 <> 0015 <span>{suggestion.title}</span> 0016 <span style={{ 'font-size': '11px', 'color': '#ccc','line-height':'15px' }}>{' by ' + suggestion.username}</span> 0017 <span style={{ 'font-size': '11px', 'color': '#ccc','line-height':'15px' }}>{suggestion.cat_title}</span> 0018 0019 </> 0020 ) : ( 0021 <span>{suggestion.username}</span> 0022 )} 0023 </div> 0024 </div> 0025 ); 0026 } 0027 0028 const renderInputComponent = inputProps => ( 0029 <div className="react-autosuggest__inputContainer"> 0030 <a onClick={inputProps.onSubmit}> 0031 <img className="react-autosuggest__icon" src={inputProps.baseUrlStore + "/theme/flatui/img/icon-search-input-2.png"} /> 0032 </a> 0033 <input {...inputProps} /> 0034 </div> 0035 ); 0036 0037 const SearchForm = () => { 0038 const {state} = useContext(MetaheaderContext); 0039 const [searchText, setSearchText] = useState(''); 0040 const [isShow, setIsShow] = useState(false); 0041 const [isLoading, setIsLoading] = useState(false); 0042 const [value, setValue] = useState(''); 0043 const [suggestions, setSuggestions] = useState([]); 0044 const [selected, setSelected] = useState(); 0045 0046 0047 const loadSuggestions = value => { 0048 const inputLength = value.length; 0049 if (inputLength < 3) return; 0050 setIsLoading(true); 0051 let url = state.baseUrlStore + '/json/search/p/' + value; 0052 if (state.store) { 0053 url += '/s/' + state.store 0054 } 0055 fetch(url, { 0056 mode: 'cors', 0057 credentials: 'include' 0058 }) 0059 .then(response => response.json()) 0060 .then(data => { 0061 setSuggestions(data); 0062 setIsLoading(false); 0063 }); 0064 0065 } 0066 0067 0068 const onSearchFormSubmit = e => { 0069 e.preventDefault(); 0070 if (!selected) { 0071 window.location.href = state.searchbaseurl + value; 0072 } else { 0073 if (selected.type == 'project') { 0074 0075 window.location.href = state.baseUrlStore + '/p/' + selected.project_id; 0076 } else { 0077 0078 window.location.href = state.baseUrlStore + '/u/' + selected.username; 0079 } 0080 } 0081 } 0082 0083 0084 const getSuggestionValue = suggestion => { 0085 setSelected(suggestion); 0086 if (suggestion.type == 'project') { 0087 return suggestion.title; 0088 } else { 0089 return suggestion.username; 0090 } 0091 0092 } 0093 0094 0095 const onHandleChange = (event, { newValue, method }) => { 0096 setValue(newValue); 0097 } 0098 0099 const shouldRenderSuggestions = value => { 0100 return value.trim().length > 2; 0101 } 0102 0103 const onSuggestionsFetchRequested = ({ value }) => { 0104 loadSuggestions(value); 0105 } 0106 0107 const onSuggestionsClearRequested = () => { 0108 setSuggestions([]); 0109 } 0110 0111 const onSuggestionSelected = (event, { suggestion, suggestionValue, suggestionIndex, sectionIndex, method }) => { 0112 if (suggestion.type == 'project') { 0113 window.location.href = state.baseUrlStore + '/p/' + suggestion.project_id; 0114 } else { 0115 window.location.href = state.baseUrlStore + '/u/' + suggestion.username; 0116 } 0117 } 0118 0119 const renderSectionTitle = section => { 0120 return ( 0121 <strong>{section.title}</strong> 0122 ); 0123 } 0124 0125 const getSectionSuggestions = section => { 0126 return section.values; 0127 } 0128 0129 const inputProps = { 0130 placeholder: "", 0131 value, 0132 onChange: onHandleChange, 0133 onSubmit: onSearchFormSubmit, 0134 baseUrlStore: state.baseUrlStore 0135 }; 0136 0137 return ( 0138 <div id="site-header-search-form" className={isShow ? 'open' : ''}> 0139 <form id="search-form" onSubmit={onSearchFormSubmit}> 0140 <div className="autosuggest"> 0141 <Autosuggest 0142 multiSection={true} 0143 suggestions={suggestions} 0144 onSuggestionsFetchRequested={onSuggestionsFetchRequested} 0145 onSuggestionsClearRequested={onSuggestionsClearRequested} 0146 shouldRenderSuggestions={shouldRenderSuggestions} 0147 onSuggestionSelected={onSuggestionSelected} 0148 getSuggestionValue={getSuggestionValue} 0149 renderSuggestion={renderSuggestion} 0150 inputProps={inputProps} 0151 renderInputComponent={renderInputComponent} 0152 renderSectionTitle={renderSectionTitle} 0153 getSectionSuggestions={getSectionSuggestions} 0154 0155 /> 0156 0157 <div className="react-autosuggest_status"> 0158 {status} 0159 </div> 0160 </div> 0161 </form> 0162 </div> 0163 ) 0164 } 0165 export default SearchForm 0166