2023-03-18 01:40:49 -04:00
|
|
|
import React, { useState, useCallback } from 'react';
|
|
|
|
|
import { debounce } from 'lodash';
|
2023-03-18 14:28:10 -04:00
|
|
|
import { useDispatch } from 'react-redux';
|
2023-03-18 01:40:49 -04:00
|
|
|
import { Search } from 'lucide-react';
|
|
|
|
|
import { setQuery } from '~/store/searchSlice';
|
2023-03-18 14:28:10 -04:00
|
|
|
import { setConvos, refreshConversation } from '~/store/convoSlice';
|
|
|
|
|
import axios from 'axios';
|
2023-03-18 01:40:49 -04:00
|
|
|
|
2023-03-18 14:28:10 -04:00
|
|
|
const fetch = async (q, pageNumber, callback) => {
|
|
|
|
|
const { data } = await axios.get(`/api/search?q=${q}&pageNumber=${pageNumber}`);
|
|
|
|
|
console.log(data);
|
|
|
|
|
callback(data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function SearchBar({ onSuccess, clearSearch }) {
|
2023-03-18 01:40:49 -04:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const [inputValue, setInputValue] = useState('');
|
2023-03-18 14:28:10 -04:00
|
|
|
|
|
|
|
|
// const onSuccess = (data) => {
|
|
|
|
|
// const { conversations, pages, pageNumber } = data;
|
|
|
|
|
// dispatch(setConvos({ convos: conversations, searchFetch: true }));
|
|
|
|
|
// dispatch(setPage(pageNumber));
|
|
|
|
|
// dispatch(setPages(pages));
|
|
|
|
|
// };
|
2023-03-18 01:40:49 -04:00
|
|
|
|
|
|
|
|
const debouncedChangeHandler = useCallback(
|
|
|
|
|
debounce((q) => {
|
|
|
|
|
dispatch(setQuery(q));
|
2023-03-18 14:28:10 -04:00
|
|
|
if (q.length > 0) {
|
|
|
|
|
fetch(q, 1, onSuccess);
|
|
|
|
|
}
|
2023-03-18 01:40:49 -04:00
|
|
|
}, 750),
|
|
|
|
|
[dispatch]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleKeyUp = (e) => {
|
|
|
|
|
const { value } = e.target;
|
|
|
|
|
if (e.keyCode === 8 && value === '') {
|
|
|
|
|
// Value after clearing input: ""
|
|
|
|
|
console.log(`Value after clearing input: "${value}"`);
|
|
|
|
|
dispatch(setQuery(''));
|
2023-03-18 14:28:10 -04:00
|
|
|
clearSearch();
|
2023-03-18 01:40:49 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const changeHandler = (e) => {
|
2023-03-18 14:28:10 -04:00
|
|
|
// if (!search) {
|
|
|
|
|
// console.log('setting page to 1');
|
|
|
|
|
// dispatch(setPage(1));
|
|
|
|
|
// }
|
2023-03-18 01:40:49 -04:00
|
|
|
|
|
|
|
|
let q = e.target.value;
|
|
|
|
|
setInputValue(q);
|
|
|
|
|
q = q.trim();
|
|
|
|
|
|
2023-03-18 14:28:10 -04:00
|
|
|
if (q === '') {
|
2023-03-18 01:40:49 -04:00
|
|
|
dispatch(setQuery(''));
|
2023-03-18 14:28:10 -04:00
|
|
|
// dispatch(setPage(1));
|
|
|
|
|
// dispatch(refreshConversation());
|
|
|
|
|
clearSearch();
|
2023-03-18 01:40:49 -04:00
|
|
|
} else {
|
|
|
|
|
debouncedChangeHandler(q);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex cursor-pointer items-center gap-3 rounded-md py-3 px-3 text-sm text-white transition-colors duration-200 hover:bg-gray-500/10">
|
|
|
|
|
{<Search className="h-4 w-4" />}
|
|
|
|
|
<input
|
|
|
|
|
// ref={inputRef}
|
|
|
|
|
type="text"
|
|
|
|
|
className="m-0 mr-0 w-full border-none bg-transparent p-0 text-sm leading-tight outline-none"
|
|
|
|
|
value={inputValue}
|
|
|
|
|
onChange={changeHandler}
|
|
|
|
|
placeholder="Search messages"
|
|
|
|
|
onKeyUp={handleKeyUp}
|
|
|
|
|
// onBlur={onRename}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|