Skip to content

Commit

Permalink
feat(components): implement location feature
Browse files Browse the repository at this point in the history
implement location feature to search bar

resolves #79
  • Loading branch information
darinetag committed Nov 9, 2023
1 parent f614d42 commit d7af056
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
84 changes: 71 additions & 13 deletions src/components/searching/SearchLocation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,78 @@ import { FaLocationDot } from "react-icons/fa6";
const SearchLocation = ({ setResults }) => {
const [input, setInput] = useState("");

const locationStates = [
"Adrar",
"Ain Defla",
"Ain Temouchent",
"Algiers",
"Annaba",
"Batna",
"Bechar",
"Bejaia",
"Biskra",
"Blida",
"Bordj Bou Arreridj",
"Bouira",
"Boumerdes",
"Chlef",
"Constantine",
"Djelfa",
"El Bayadh",
"El Oued",
"El Tarf",
"Ghardaia",
"Guelma",
"Illizi",
"Jijel",
"Khenchela",
"Laghouat",
"M'Sila",
"Mascara",
"Medea",
"Mila",
"Mostaganem",
"Naama",
"Oran",
"Ouargla",
"Oum El Bouaghi",
"Relizane",
"Saida",
"Setif",
"Sidi Bel Abbes",
"Skikda",
"Souk Ahras",
"Tamanrasset",
"Tebessa",
"Tiaret",
"Tindouf",
"Tipaza",
"Tissemsilt",
"Tizi Ouzou",
"Tlemcen",
"Timimoun",
"Bordj Badji Mokhtar",
"Ouled Djellal",
"Béni Abbès",
"Ain Salah",
"Ain Guezzam",
"Touggourt",
"Djanet",
"El M'Ghair",
"El Menia",
];

const fetchData = (value) => {
fetch("https://jsonplaceholder.typicode.com/users")
.then((response) => response.json())
.then((json) => {
const results = json.filter((user) => {
return (
value &&
user &&
user.name &&
user.name.toLowerCase().includes(value)
);
});
setResults(results);
});
const filteredLocations = locationStates.filter((location) =>
location.toLowerCase().includes(value.toLowerCase()),
);

setResults(
filteredLocations.map((location, index) => ({
id: index,
name: location,
})),
);
};

const handleChange = (value) => {
Expand Down
14 changes: 12 additions & 2 deletions src/components/searching/SearchResult.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { useState } from "react";

const SearchResult = ({ result }) => {
const [selected, setSelected] = useState(false);

const handleClick = () => {
setSelected(!selected);
};

return (
<div
className='p-1 search-result'
onClick={(e) => alert(`you clicked on ${result.name}`)}
className={`p-1 search-result cursor-pointer ${
selected ? "selected" : ""
}`}
onClick={handleClick}
>
{result.name}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/searching/SearchResultsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SearchResult from "./SearchResult";

const SearchResultsList = ({ results }) => {
return (
<div className='hidden:w-3/6 bg-white flex flex-col shadow-lg rounded-2xl mt-4 h-60 overflow-y-auto'>
<div className='w-auto bg-white flex flex-col shadow-lg rounded-2xl mt-4 h-60 overflow-y-auto'>
{results.map((result, id) => {
return <SearchResult result={result} key={id} />;
})}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/search/LocationSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const LocationSearch = () => {
<div className='bg-[#eee] h-96 w-full'>
<div className='flex flex-col items-center pt-24 w-2/6 m-auto'>
<SearchLocation setResults={setResults} />
<SearchResultsList results={results}/>
<SearchResultsList results={results} />
</div>
</div>
);
Expand Down

0 comments on commit d7af056

Please sign in to comment.