Skip to content

Commit 02bcce9

Browse files
authored
Merge pull request #27 from TryShape/implement-search
Implementing Search
2 parents f48b883 + 5f5bf9f commit 02bcce9

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

components/core/App.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import Loader from "react-loader-spinner";
1414
import { ShapeList, Header } from '..';
1515

1616
const App = (props) => {
17-
const [data, setData] = useState([]);
18-
const [loading, setLoading] = useState(true);
17+
const [data, setData] = useState([]); // shapes
18+
const [loading, setLoading] = useState(true); // shapes loading
19+
20+
const [searchTerm, setSearchTerm] = useState(""); // search
21+
1922
const { user } = props;
2023

2124
useEffect(async () => {
@@ -85,11 +88,11 @@ const App = (props) => {
8588
setLoading(false);
8689
}, [user]);
8790

88-
89-
9091
return (
9192
<>
92-
<Header {...props} />
93+
<Header {...props}
94+
searchTerm={searchTerm}
95+
setSearchTerm={setSearchTerm}/>
9396
{loading ? (
9497
<Loader
9598
style={{margin: '20% auto auto 42%'}}
@@ -99,7 +102,7 @@ const App = (props) => {
99102
width={300}
100103
/>
101104
) : (
102-
<ShapeList {...props} data={ data } />
105+
<ShapeList {...props} data={ data } searchTerm={searchTerm} />
103106
)}
104107
</>
105108
);

components/utils/Header.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,15 @@ const LoginBar = styled.div`
126126

127127

128128

129-
const Header = ({ setOpen, user, setUser }) => {
130-
// console.log(user);
129+
const Header = ({
130+
setOpen,
131+
user,
132+
setUser,
133+
searchTerm,
134+
setSearchTerm
135+
}) => {
136+
137+
const [searchterm, setSearchterm] = useState('');
131138
// sign out function
132139
const signOut = () => {
133140
auth()
@@ -149,13 +156,16 @@ const Header = ({ setOpen, user, setUser }) => {
149156
<div className="sr-only">TryShape</div>
150157
</Logo>
151158
</Link>
159+
152160
<NavbarSearchInputContainer>
153161
<NavbarSearchInput>
154162
<NavbarSearchInputText id="basic-addon1"><FiSearch color='white' size='18px' /></NavbarSearchInputText>
155163
<NavbarSearchInputControl
156164
placeholder="Search a shape"
157165
aria-label="Search a shape"
158166
aria-describedby="basic-addon1"
167+
value={searchTerm}
168+
onChange={(e) => setSearchTerm(e.target.value)}
159169
/>
160170
<CloseIcon color='#ffffff' size='24px' />
161171
</NavbarSearchInput>
@@ -165,6 +175,7 @@ const Header = ({ setOpen, user, setUser }) => {
165175
<Dropdown.Item href="#/action-3">Newly Added</Dropdown.Item>
166176
</DropdownButton>
167177
</NavbarSearchInputContainer>
178+
168179
{(user.email || user.displayName) ? (
169180
<>
170181
<LoginBar>

components/utils/ShapeList.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useEffect } from "react";
22

33
// Bootstrap
44
import Container from 'react-bootstrap/Container'
@@ -191,12 +191,27 @@ const LikeFilledIcon = styled(BsFillHeartFill)`
191191
cursor: pointer;
192192
`;
193193

194-
const ShapeList = ({ setOpen, user, data }) => {
194+
const ShapeList = ({ setOpen, user, data, searchTerm }) => {
195+
196+
const filterShape = (shapes, searchTerm) => {
197+
if (!searchTerm) {
198+
return shapes;
199+
}
200+
return shapes.filter((shape) => {
201+
const shapeName = shape.name.toLowerCase();
202+
return shapeName.includes(searchTerm.toLowerCase());
203+
})
204+
}
195205

196206
const [shapes, setShapes] = useState(data);
207+
const [filteredShape, setFilteredShape] = useState(shapes);
197208
const [showExportModal, setShowExportModal] = useState(false);
198209
const [shapeToExport, setShapeToExport] = useState();
199210

211+
useEffect(() =>{
212+
setFilteredShape(filterShape(shapes, searchTerm));
213+
}, [searchTerm, shapes]);
214+
200215
const handleSwicth = (shapeName) => {
201216

202217
let modifiedShapes = shapes.map((shape, index) => {
@@ -312,6 +327,7 @@ const ShapeList = ({ setOpen, user, data }) => {
312327
return shape;
313328
});
314329
setShapes(...[modifiedShapes]);
330+
//setData(...[modifiedShapes]);
315331
}
316332
};
317333

@@ -324,7 +340,7 @@ const ShapeList = ({ setOpen, user, data }) => {
324340
setShow={ setShowExportModal }
325341
shape = { shapeToExport } /> }
326342

327-
{shapes.map((shape, index) => (
343+
{filteredShape.map((shape, index) => (
328344
<React.Fragment key={index}>
329345
<ShapeCard>
330346
<ShapeCardBody>

pages/_app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ export default function App({ Component, pageProps }) {
3333
}
3434
});
3535

36+
console.log('pageProps', pageProps);
37+
3638
return (
39+
3740
<>
3841
<Toaster position="top-right" reverseOrder={false} />
3942
<Component {...pageProps} {...props} />

0 commit comments

Comments
 (0)