Skip to content

Commit

Permalink
Merge pull request #14 from elric97/palvitgarg99/development
Browse files Browse the repository at this point in the history
Improved search operation & route failure correction
  • Loading branch information
palvitgarg99 authored Nov 28, 2021
2 parents f9c6202 + bba6a9a commit e278e91
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
29 changes: 26 additions & 3 deletions backend/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,30 @@
@app.route('/products', methods=['GET', 'POST', 'DELETE', 'PATCH'])
def products():
if request.method == 'GET':
data = product_records.find()
data = product_records.find(
{"$or" :
[{
"name" :
{
"$regex" : request.args.get("query"),
'$options' : 'i'
}
},
{
"description" :
{
"$regex" : request.args.get("query"),
'$options' : 'i'
}
},
{
"tags" :
{
"$regex" : request.args.get("query"),
'$options' : 'i'
}
}]
})
return dumps(data)

data = request.get_json()
Expand Down Expand Up @@ -75,9 +98,9 @@ def products():


@app.route('/<productname>/getFeature', methods=['GET', 'POST'])
def get_feature(product_name):
def get_feature(productname):
if request.method == 'GET':
data = product_records.find({"name": product_name})
data = product_records.find({"name": productname},{"features":1})
return dumps(data)


Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Feature hunt</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
13 changes: 8 additions & 5 deletions src/Components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const Home = ({query}) => {
const [sortBy, setSortBy] = useState('timestamp');
const [products, setProducts] = useState([]);
useEffect(() => {
Service.get('products').then(products => setProducts(products));
}, []);
Service.get('products', "query=" + query).then(products => setProducts(products));
}, [query]);

return (
<div className="container">
<div className="child">
Expand All @@ -36,9 +37,11 @@ const Home = ({query}) => {
</div>
</div>
</div>
{products.map((p, index) => { p['index'] = index; return p; }).filter(p => query ? p.tags.includes(query.toLowerCase()) || p.name.toLowerCase().includes(query.toLowerCase()) : true).sort((p1, p2) => p2[sortBy] - p1[sortBy]).map(
(product) => <ProductTile key={product.id} products={products} index={product.index} setProducts={setProducts} />
, setProducts)}
{products
.map((p, index) => { p['index'] = index; return p; })
.sort((p1, p2) => p2[sortBy] - p1[sortBy])
.map((product) => <ProductTile key={product.id} products={products} index={product.index} setProducts={setProducts} />, setProducts)
}
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/Components/ProductTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ProductTile = ({ products, index, setProducts }) => {
setProducts(products.map((product) => product.id === products[index].id ? updatedProduct : product));
};
const goTo = (product) => () => {
history.push(`/${product}`);
history.push(`/${product}/getFeature`);
};
const capitalizeFirstLetter = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
Expand Down
3 changes: 2 additions & 1 deletion src/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const postRequestOptionsBuilder = (method, body, headers) => {

const get = async (path, params) => {
path = sanitizePath(path);
if (params) path += new URLSearchParams(params);

if (params) path += "?" + new URLSearchParams(params);
const response = await fetch(baseUrl + path);
const data = await response.json();
if (response.status >= 400 && response.status < 600) {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/ProductTile.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe("Test ProductTile", () => {

fireEvent.click(nav);
expect(history.length).toBe(3); // after clicking on something, history.length + 1
expect(history.location.pathname).toBe("/feature-hunt");
expect(history.location.pathname).toBe("/feature-hunt/getFeature");

fireEvent.click(nav2);
expect(history.length).toBe(4); // after clicking on something, history.length + 1
Expand Down

0 comments on commit e278e91

Please sign in to comment.