Skip to content

Commit

Permalink
feat: added search feature to filter requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jadeallencook committed Nov 28, 2023
1 parent 25eab4f commit 9d0395e
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 12 deletions.
1 change: 1 addition & 0 deletions interfaces/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface FiltersInterface {
driver: DriverKeyType | '';
stage: StageKeyType | '';
coordinator: string;
string: string;
}
1 change: 1 addition & 0 deletions src/contexts/DashboardContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const defaultFilters: FiltersInterface = {
driver: '',
stage: 'submitted',
coordinator: '',
string: '',
};

export const log: (message: string) => void = (message) =>
Expand Down
41 changes: 41 additions & 0 deletions src/forms/AdvancedSearchForm/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import styled, { StyledComponent } from 'styled-components';
import { Button, Form, InputGroup } from 'react-bootstrap';
import { FiltersInterface } from '@interfaces/filters';
import style from './style';
import { useContext, useState } from 'react';
import DashboardContext from '../../contexts/DashboardContext';

const AdvancedSearchForm: StyledComponent = styled(
({
className,
filters,
setFilters,
}: {
className: string;
filters: FiltersInterface;
setFilters: (value: FiltersInterface) => void;
}) => {
const [term, setTerm] = useState<string>('');
const handleSearch = (e) => {
setTerm(e.target.value);
setFilters({ ...filters, string: e.target.value });
};

return (
<Form className={className}>
<InputGroup>
<Form.Control
type="text"
name="driver"
value={term}
size="sm"
placeholder="Search by name or email"
onChange={handleSearch}
/>
</InputGroup>
</Form>
);
}
)(style);

export default AdvancedSearchForm;
23 changes: 23 additions & 0 deletions src/forms/AdvancedSearchForm/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const style = () => ({
marginBottom: '15px',
'select, button, .form-check-input': {
cursor: 'pointer',
},
'.form-check-input': {
marginTop: 0,
},
'.form-check-input:checked': {
backgroundColor: 'var(--primary)',
borderColor: 'var(--primary)',
},
'.input-group-text': {
backgroundColor: 'var(--bs-gray-600)',
border: 'none',
},
'button.search-btn': {
backgroundColor: 'var(--black) !important',
border: 'none',
},
});

export default style;
3 changes: 0 additions & 3 deletions src/forms/FilterForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ const FilterForm: StyledComponent = styled(
});
}}
/>
<Button className="search-btn" size="sm" onClick={null}>
More Filters
</Button>
<Button size="sm" onClick={() => fetchRequests()}>
Refresh
</Button>
Expand Down
4 changes: 0 additions & 4 deletions src/forms/FilterForm/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ const style = () => ({
backgroundColor: 'var(--bs-gray-600)',
border: 'none',
},
'button.search-btn': {
backgroundColor: 'var(--black) !important',
border: 'none',
},
});

export default style;
5 changes: 5 additions & 0 deletions src/pages/Dashboard/AidRequestsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useContext, useState } from 'react';
import DashboardContext from '../../contexts/DashboardContext';
import Loading from '@components/Loading';
import filterRequests from '@utils/filter-requests';
import AdvancedSearchForm from '@forms/AdvancedSearchForm';

const AidRequestsPage = () => {
const { requests, isLoading, requestFilters, setRequestFilters, uid } =
Expand All @@ -24,6 +25,10 @@ const AidRequestsPage = () => {
filters={requestFilters}
setFilters={setRequestFilters}
/>
<AdvancedSearchForm
filters={requestFilters}
setFilters={setRequestFilters}
/>
{isLoading ? (
<Loading />
) : (
Expand Down
1 change: 1 addition & 0 deletions src/pages/Dashboard/DonationsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const DonationsPage = () => {
driver: '',
stage: 'submitted',
coordinator: '',
string: '',
});
const handler = (id?: string, shouldRefetch?: boolean): void => {
setSelected(id);
Expand Down
6 changes: 3 additions & 3 deletions src/pages/RequestAidPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Container } from 'react-bootstrap';

const RequestAidPage = () => (
<Container>
{/* <RequestAidForm /> */}
<h1>Request Aid Form</h1>
<RequestAidForm />
{/* <h1>Request Aid Form</h1>
<p>
Due to an overwhelming response and limited volunteer capacity, we
have temporarily taken down the request aid form. We want to ensure
Expand All @@ -21,7 +21,7 @@ const RequestAidPage = () => (
<a href="mailto:support@communalists.org?subject=Mutual Aid Volunteer Request">
support@communalists.org
</a>
</p>
</p> */}
</Container>
);

Expand Down
9 changes: 7 additions & 2 deletions src/utils/filter-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import RequestAidInterface from '@interfaces/request-aid';
const filterRequests = (
requests: { [key: string]: RequestAidInterface },
filters: FiltersInterface
) =>
Object.entries(requests).reduce((object, [id, request]) => {
) => {
return Object.entries(requests).reduce((object, [id, request]) => {
const { stage, location, language, driver, coordinator } = filters;
const string = filters.string?.toLowerCase().trim();
return stage !== request.stage ||
(location && location !== request.location) ||
(language && language !== request.language) ||
(driver && driver !== request.driver) ||
(string &&
request.name.toLowerCase().trim().indexOf(string) &&
request.email.toLowerCase().trim().indexOf(string)) ||
(coordinator && coordinator !== request.coordinator)
? object
: {
...object,
[id]: request,
};
}, {});
};

export default filterRequests;

0 comments on commit 9d0395e

Please # to comment.