Skip to content

Commit 20436e1

Browse files
authored
Merge pull request #20 from oslabs-beta/denton/link
Denton/link
2 parents a3d9759 + 9f8eef2 commit 20436e1

File tree

3 files changed

+87
-31
lines changed

3 files changed

+87
-31
lines changed

app/src/components/marketplace/MarketplaceCardContainer.tsx

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
import { Container, Grid } from '@mui/material';
22

33
import MarketplaceCard from './MarketplaceCard';
4-
import React, {useEffect, useState} from 'react';
5-
import axios from 'axios';
4+
import React from 'react';
65

7-
const MarketplaceCardContainer = () => {
86

9-
const [marketplaceProjects, setMarketplaceProjects] = useState([]);
10-
useEffect(() => {
11-
async function marketplaceFetch() {
12-
try {
13-
const response = await axios.get('/getMarketplaceProjects', {
14-
headers: {
15-
'Content-Type': 'application/json',
16-
},
17-
withCredentials: true,
18-
});
7+
const MarketplaceCardContainer = ({displayProjects}) => {
8+
199

20-
setMarketplaceProjects(response.data);
21-
22-
} catch (error) {
23-
console.error('Error fetching MP data:', error);
24-
}
25-
}
26-
marketplaceFetch();
27-
28-
}, []);
29-
3010
return (
3111
<>
3212
<Container>
@@ -36,7 +16,7 @@ const MarketplaceCardContainer = () => {
3616
spacing={{ xs: 2, md: 3 }}
3717
columns={{ xs: 4, sm: 8, md: 12 }}
3818
>
39-
{marketplaceProjects.map((proj, i) => (
19+
{displayProjects.map((proj, i) => (
4020

4121
<Grid item xs={4} sm={4} md={4} key={i}>
4222
<MarketplaceCard proj={proj}/>

app/src/components/marketplace/Searchbar.tsx

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
1-
import React from 'react';
1+
import React, {useEffect, useState} from 'react';
22
import TextField from '@mui/material/TextField';
33

4-
const SearchBar = () => {
4+
const SearchBar = ({marketplaceProjects, updateDisplayProjects }):JSX.Element => {
5+
//passing down all marketplaceProjects
6+
const [searchText, setSearchText] = useState('');
7+
8+
9+
const handleChange = (e) => {
10+
const newText = e.target.value
11+
setSearchText(newText)
12+
13+
}
14+
15+
16+
useEffect(()=>{
17+
18+
if(searchText === ''){
19+
20+
updateDisplayProjects(marketplaceProjects)
21+
22+
}else{
23+
24+
const patternString = '(?:^|[^a-zA-Z])' + searchText.toLowerCase() + '(?:$|[^a-zA-Z])';
25+
//(?: [non-capturing group] means to ignore the items inside the parens in terms of looking for that string order in the target
26+
//^ refers to the literal beginning of a start of a line or string
27+
//| pipe operator is an OR statement
28+
//[^a-zA-Z] [] is grouping characters, the ^ at the beginning means to look for things that are not letters (lowercase or uppercase)
29+
//a-zA-Z letters (lowercase and uppercase) and underscore symbol
30+
//All together: match either the start/end of a line/string or any character that is not a letter.
31+
//Looks for anything that has the searchText in between non-letter characters
32+
const searchResults = marketplaceProjects.reduce((results, curr) => {
33+
if(curr.name.match(patternString) || curr.username.match(patternString))
34+
results.push(curr)
35+
return results;
36+
}, [])
37+
updateDisplayProjects(searchResults)
38+
}
39+
40+
}, [searchText])
41+
42+
543
return (
44+
645
<TextField
746
label="Search"
847
variant="outlined"
948
fullWidth
1049
style={{ maxWidth: 450, height: '10px' }}
50+
value= {searchText}
51+
onChange={handleChange}
1152
/>
53+
1254
);
1355
};
1456

app/src/containers/MarketplaceContainer.tsx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,55 @@
11
import MarketplaceCardContainer from '../components/marketplace/MarketplaceCardContainer';
22
import SearchBar from '../components/marketplace/Searchbar';
3-
import React from 'react';
3+
import React, {useEffect, useState} from 'react';
4+
import axios from 'axios';
45

56
const MarketplaceContainer = () => {
67

8+
const [marketplaceProjects, setMarketplaceProjects] = useState([]);
9+
const [displayProjects, setDisplayProjects] = useState([]);
10+
useEffect(() => {
11+
async function marketplaceFetch() {
12+
try {
13+
const response = await axios.get('/getMarketplaceProjects', {
14+
headers: {
15+
'Content-Type': 'application/json',
16+
},
17+
withCredentials: true,
18+
});
19+
20+
setMarketplaceProjects(response.data);
21+
setDisplayProjects(response.data);
22+
23+
} catch (error) {
24+
console.error('Error fetching MP data:', error);
25+
}
26+
}
27+
marketplaceFetch();
28+
29+
}, []);
30+
31+
const updateDisplayProjects = (searchResults) => {
32+
33+
setDisplayProjects(searchResults);//have to pass this down as a prop so that the setting is done outside of Rendering otherwise callstack issues
34+
35+
};
736

837

938
return (
1039
<div style={containerStyles}>
1140
<div style={contentStyles}>
1241
<h1 style={headingStyles}>Discover components built with ReacType</h1>
1342
<p style={subheadingStyles}>
14-
Browse, save, and customize the latest components built by the
15-
community
43+
Browse, save, and customize the latest components built by the
44+
community
1645
</p>
17-
<SearchBar />
46+
<SearchBar marketplaceProjects = {marketplaceProjects} updateDisplayProjects = {updateDisplayProjects}/>
1847
</div>
19-
<MarketplaceCardContainer />
48+
{displayProjects.length ? <MarketplaceCardContainer displayProjects = {displayProjects} /> :
49+
<h2 style={{textAlign: 'center'}}>
50+
No Results Found!
51+
</h2>
52+
}
2053
</div>
2154
);
2255
};
@@ -28,6 +61,7 @@ const containerStyles: React.CSSProperties = {
2861
color: 'white',
2962
paddingBottom: '15vh',
3063
overflow: 'auto',
64+
3165
};
3266

3367
const contentStyles: React.CSSProperties = {

0 commit comments

Comments
 (0)