Skip to content

Commit 1c88113

Browse files
committed
Search by provider or product
1 parent fe94b5b commit 1c88113

3 files changed

Lines changed: 96 additions & 27 deletions

File tree

demo/app/src/components/Sidebar/Sidebar.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,46 @@ export default function Sidebar(props) {
2929
function providerSelectHandler(e) {
3030
setUserParams({
3131
...userParams,
32-
provider: e.target.value
32+
provider: e.target.value,
33+
product: 'all'
34+
})
35+
}
36+
37+
function productSelectHandler(e) {
38+
setUserParams({
39+
...userParams,
40+
product: e.target.value
3341
})
3442
}
3543

3644
function isProviderSelected(provider) {
3745
return provider === userParams.provider;
3846
}
3947

48+
function isProductSelected(product) {
49+
return product === userParams.product;
50+
}
51+
52+
function getProductOptions(products){
53+
return products && products.filter(product => product).map(({ title, id }) => {
54+
return (
55+
<option
56+
key={id}
57+
value={id}
58+
selected={isProductSelected(id)}
59+
>
60+
{title}
61+
</option>
62+
);
63+
});
64+
}
65+
66+
function displayProducts() {
67+
return userParams.provider !== 'all' ?
68+
getProductOptions(products[userParams.provider])
69+
: getProductOptions(Object.values(products).flat())
70+
}
71+
4072
const filterButtonClass = openFilters
4173
? styles.filterButtonOpen
4274
: styles.filterButtonClosed
@@ -78,6 +110,13 @@ export default function Sidebar(props) {
78110
<div>
79111
<span>Providers: </span>
80112
<select onChange={providerSelectHandler}>
113+
<option
114+
value='all'
115+
key='all'
116+
selected
117+
>
118+
All
119+
</option>
81120
{providers.map((provider) => (
82121
<option
83122
value={provider.id}
@@ -91,17 +130,16 @@ export default function Sidebar(props) {
91130
</div>
92131
<div>
93132
<div className={styles.blockLabel}>Products: </div>
94-
{products.length && (
95-
<select>
96-
{products.map(({ title, id }) => {
97-
return (
98-
<option key={id} value={id}>
99-
{title}
100-
</option>
101-
);
102-
})}
133+
<select onChange={productSelectHandler}>
134+
<option
135+
value='all'
136+
key='all'
137+
selected
138+
>
139+
All
140+
</option>
141+
{displayProducts()}
103142
</select>
104-
)}
105143
</div>
106144
</div>
107145
</div>

demo/app/src/context/appContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export default function AppProvider({ children }) {
2121
today,
2222
new Date(new Date(today).setDate(today.getDate() + 3)),
2323
],
24-
provider: null,
25-
product: null
24+
provider: 'all',
25+
product: 'all'
2626
});
2727
const [hoveredOpportunity, setHoveredOpportunity] = useState(null);
2828
const [selectedOpportunity, setSelectedOpportunity] = useState(null);

demo/app/src/hooks/useGetOpportunities.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { ALL_PROVIDERS } from "src/utils/constants";
44

55
function fetchOpportunity(token, provider, params){
66
return fetch("/api/opportunities", {
7-
method: "POST",
8-
headers: {
9-
"Content-type": "application/json",
10-
"Backend": provider,
11-
'Authorization': `Bearer ${token}`
12-
},
13-
body: JSON.stringify(params)
14-
});
7+
method: "POST",
8+
headers: {
9+
"Content-type": "application/json",
10+
"Backend": provider,
11+
'Authorization': `Bearer ${token}`
12+
},
13+
body: JSON.stringify(params)
14+
});
1515
};
1616

1717
const useGetOpportunities = (products, postParams) => {
@@ -21,9 +21,25 @@ const useGetOpportunities = (products, postParams) => {
2121
const [error, setError] = useState("");
2222
const { userToken } = useLocalStorage();
2323

24+
function setAsyncResultsData(promises){
25+
Promise.all(promises).then(results => {
26+
setData(Object.fromEntries(Object.values(results).map(value => {
27+
return [value.provider, value.data.features]
28+
})));
29+
setIsLoading(false);
30+
}).catch(e => {
31+
setError(e);
32+
setIsLoading(false);
33+
});
34+
}
35+
36+
async function fetchByProduct(p, productId){
37+
return fetchOpportunity(userToken, p, Object.assign(params, {"product_id": productId})).then(async res => await res.json()).then(data => { return {'provider': p, 'data': data}});
38+
}
39+
2440
function fetchAllProviderProducts(p){
2541
return products[p] && products[p].map(async product => {
26-
return fetchOpportunity(userToken, p, Object.assign(params, {"product_id": product.id})).then(async res => await res.json()).then(data => { return {'provider': p, 'data': data}});
42+
return fetchByProduct(p, product.id);
2743
});
2844
};
2945

@@ -32,16 +48,31 @@ const useGetOpportunities = (products, postParams) => {
3248
setIsLoading(true);
3349
setError(false);
3450
// By default fetch all provider product opportunities
35-
if(!provider && !params["product_id"]){
51+
if(provider === 'all' && params["product_id"] === 'all'){
3652
const allProvidersOpportunities = ALL_PROVIDERS.reduce((all, p) => {
3753
const promises = fetchAllProviderProducts(p.id);
3854
return promises ? [...all, ...promises] : all;
3955
}, []);
4056

41-
Promise.all(allProvidersOpportunities).then(results => {
42-
setData(Object.fromEntries(Object.values(results).map(value => {
43-
return [value.provider, value.data.features]
44-
})));
57+
setAsyncResultsData(allProvidersOpportunities);
58+
}
59+
else if(provider === 'all'){
60+
const productOpportunities = ALL_PROVIDERS.reduce((all, p) => {
61+
const promise = fetchByProduct(p.id, params["product_id"]);
62+
return promise ? [...all, promise] : all;
63+
}, []);
64+
65+
setAsyncResultsData(productOpportunities);
66+
}
67+
else if(params["product_id"] === 'all'){
68+
const providerOpportunities = fetchAllProviderProducts(provider)
69+
setAsyncResultsData(providerOpportunities);
70+
}
71+
else{
72+
fetchOpportunity(userToken, provider, params)
73+
.then(async res => await res.json())
74+
.then(data => {
75+
setData({[provider]: data.features});
4576
setIsLoading(false);
4677
}).catch(e => {
4778
setError(e);

0 commit comments

Comments
 (0)