Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] moved search function and other small changes for filtering #79

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions components/FilterBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import { FilterContainerStyles } from './styles';
export const FilterBar = ({
filters,
onFilterChange,
selectedFilters,
setSelectedFilters,
}: {
filters: FilterType[];
onFilterChange: (filter: FilterType) => void;
selectedFilters: Filters;
setSelectedFilters: React.Dispatch<React.SetStateAction<Filters>>;
}) => {
const [activeFilter, setActiveFilter] = useState<FilterType | null>(null);

const [selectedFilters, setSelectedFilters] = useState<Filters>({
status: [],
technology: [],
projectSize: { min: 0, max: 0 },
location: [],
});

const handleButtonClick = (filter: FilterType) => {
setActiveFilter(activeFilter?.id === filter.id ? null : filter);
onFilterChange(filter);
Expand Down
31 changes: 26 additions & 5 deletions components/MapViewScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react';
import {
LocationIcon,
ProjectSizeIcon,
Expand All @@ -7,7 +8,7 @@ import {
import { FilterBar } from '@/components/FilterBar';
import Map from '@/components/Map';
import { SearchBar } from '@/components/SearchBar';
import { FilterType } from '@/types/schema';
import { Filters, FilterType } from '@/types/schema';
import { Project } from '../../types/schema';
import ProjectsListingModal from '../ProjectsListingModal';

Expand Down Expand Up @@ -43,17 +44,37 @@ export default function MapViewScreen({
},
];

const [searchTerm, setSearchTerm] = useState('');
const [selectedFilters, setSelectedFilters] = useState<Filters>({
status: [],
technology: [],
projectSize: { min: 0, max: 0 },
location: [],
});

const handleFilterChange = (filter: FilterType) => {
console.log(filter);
};

useEffect(() => {
let filtered: Project[] = [];
filtered =
projects?.filter(project =>
project.project_name.toLowerCase().includes(searchTerm.toLowerCase()),
) ?? null;

setFilteredProjects(filtered);
}, [projects, searchTerm, setFilteredProjects]);

return (
<>
<SearchBar
allProjects={projects}
setFilteredProjects={setFilteredProjects}
<SearchBar searchTerm={searchTerm} setSearchTerm={setSearchTerm} />
<FilterBar
filters={filters}
onFilterChange={handleFilterChange}
selectedFilters={selectedFilters}
setSelectedFilters={setSelectedFilters}
/>
<FilterBar filters={filters} onFilterChange={handleFilterChange} />
<Map projects={projects} />
<ProjectsListingModal projects={filteredProjects} />
</>
Expand Down
25 changes: 7 additions & 18 deletions components/SearchBar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use client';

import React, { useEffect, useState } from 'react';
import React from 'react';
import { SearchExit, SearchIcon } from '@/assets/SearchBar-Icons/icons';
import { Project } from '@/types/schema';
import {
SearchBarBackgroundStyles,
SearchBarDiv,
Expand All @@ -12,22 +11,12 @@ import {
} from './styles';

export const SearchBar = ({
allProjects,
setFilteredProjects,
searchTerm,
setSearchTerm,
}: {
allProjects: Project[];
setFilteredProjects: React.Dispatch<React.SetStateAction<Project[] | []>>;
searchTerm: string | null;
setSearchTerm: React.Dispatch<React.SetStateAction<string>>;
}) => {
const [searchTerm, setSearchTerm] = useState('');

useEffect(() => {
const filteredProjects =
allProjects?.filter(project =>
project.project_name.toLowerCase().includes(searchTerm.toLowerCase()),
) ?? null;
setFilteredProjects(filteredProjects);
}, [allProjects, searchTerm, setFilteredProjects]);

const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(event.target.value);
};
Expand All @@ -45,11 +34,11 @@ export const SearchBar = ({
type="text"
placeholder="Search for a project"
onChange={handleSearchChange}
value={searchTerm}
value={searchTerm + ''}
/>
<SearchExitButton
onClick={handleExit}
$isZero={searchTerm.length === 0}
$isZero={searchTerm?.length === 0}
>
<SearchExit />
</SearchExitButton>
Expand Down
Loading