Skip to content

Commit 5f5bf9f

Browse files
committed
feat: Implemented the search shape functionality
1 parent 9a8ed65 commit 5f5bf9f

4 files changed

Lines changed: 40 additions & 22 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: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ const LoginBar = styled.div`
126126

127127

128128

129-
const Header = ({ setOpen, user, setUser }) => {
129+
const Header = ({
130+
setOpen,
131+
user,
132+
setUser,
133+
searchTerm,
134+
setSearchTerm
135+
}) => {
130136

131137
const [searchterm, setSearchterm] = useState('');
132138
// sign out function
@@ -143,18 +149,6 @@ const Header = ({ setOpen, user, setUser }) => {
143149
});
144150
};
145151

146-
const handleSearchTerm = event => {
147-
if (event.key === 'Enter') {
148-
search();
149-
} else {
150-
setSearchterm(event.target.value);
151-
}
152-
}
153-
154-
const search = event => {
155-
console.log('Performing Search using', searchterm);
156-
}
157-
158152
return (
159153
<AppHeader>
160154
<Link href="/">
@@ -170,6 +164,8 @@ const Header = ({ setOpen, user, setUser }) => {
170164
placeholder="Search a shape"
171165
aria-label="Search a shape"
172166
aria-describedby="basic-addon1"
167+
value={searchTerm}
168+
onChange={(e) => setSearchTerm(e.target.value)}
173169
/>
174170
<CloseIcon color='#ffffff' size='24px' />
175171
</NavbarSearchInput>

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)