Skip to content

Commit fe94b5b

Browse files
committed
Fetch all provider product opps by default
1 parent 13d6385 commit fe94b5b

7 files changed

Lines changed: 93 additions & 61 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import styles from './Sidebar.module.scss'
33

44
export default function Opportunity({
55
title,
6+
provider,
67
start,
78
end
89
}) {
910
return (
1011
<div className={styles.opportunityPreview}>
1112
<h1>{title}</h1>
13+
<h2>{provider}</h2>
1214
<div className={styles.previewStartDate}>
1315
{formatToFriendlyString(start) ?? 'no start date given'}
1416
</div>

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ function mouseOutStyleChange(e) {
1616

1717
return (
1818
<div className={styles.opportunityList}>
19-
{(opportunities||[]).map((opp, index) => {
20-
const { start, end } = parseStacDatetime(opp.properties.datetime);
21-
return <Opportunity
22-
key={index}
23-
title={opp.id}
24-
start={start}
25-
end={end}
26-
// onMouseEnter={(e) => {
27-
// mouseInStyleChange(e);
28-
// setHoveredOpportunity(index)}}
29-
// onMouseLeave={(e) => {
30-
// mouseOutStyleChange(e);
31-
// setHoveredOpportunity(null)}}
32-
// onClick={(e) => setSelectedOpportunity(index)}
33-
/>
19+
{Object.entries(opportunities||{}).map(([provider, opps]) => {
20+
return opps && opps.map((opp, index) => {
21+
const { start, end } = parseStacDatetime(opp.properties.datetime);
22+
return <Opportunity
23+
key={index}
24+
title={opp.id}
25+
provider={provider}
26+
start={start}
27+
end={end}
28+
// onMouseEnter={(e) => {
29+
// mouseInStyleChange(e);
30+
// setHoveredOpportunity(index)}}
31+
// onMouseLeave={(e) => {
32+
// mouseOutStyleChange(e);
33+
// setHoveredOpportunity(null)}}
34+
// onClick={(e) => setSelectedOpportunity(index)}
35+
/>
36+
})
3437
})
3538
}
3639
</div>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import styles from "./Sidebar.module.scss";
1010
import { useAppContext } from 'src/context/appContext';
1111
import OpportunityList from './OpportunityList';
1212
import { useEffect, useState } from 'react';
13+
import { ALL_PROVIDERS as providers } from 'src/utils/constants';
1314

1415
export default function Sidebar(props) {
1516
const {
@@ -22,8 +23,7 @@ export default function Sidebar(props) {
2223
isLoadingProducts,
2324
isErrorProducts,
2425
userParams,
25-
setUserParams,
26-
providers
26+
setUserParams
2727
} = useAppContext();
2828

2929
function providerSelectHandler(e) {

demo/app/src/context/appContext.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export default function AppProvider({ children }) {
2121
today,
2222
new Date(new Date(today).setDate(today.getDate() + 3)),
2323
],
24-
provider: 'earthsearch'
24+
provider: null,
25+
product: null
2526
});
2627
const [hoveredOpportunity, setHoveredOpportunity] = useState(null);
2728
const [selectedOpportunity, setSelectedOpportunity] = useState(null);
@@ -32,27 +33,14 @@ export default function AppProvider({ children }) {
3233
params: {
3334
"geometry": userParams.geometry,
3435
"datetime": formatToISOString(userParams.dateRange),
35-
"product_id": userParams.provider === 'earthsearch' ? 'sentinel-2-l1c' : null
36+
"product_id": userParams.product
3637
},
3738
provider: userParams.provider
3839
} : null;
3940
}, [userParams])
4041

41-
const { isLoading: isLoadingOpps, data: opportunities, error: errorOpps } = useGetOpportunities(postParams);
4242
const { isLoading: isLoadingProducts, data: products, error: errorProducts } = useGetProducts();
43-
const providers = [{
44-
id: 'earthsearch',
45-
name: 'EarthSearch'
46-
}, {
47-
id: 'blacksky',
48-
name: 'BlackSky'
49-
}, {
50-
id: 'planet',
51-
name: 'Planet'
52-
}, {
53-
id: 'umbra',
54-
name: 'Umbra'
55-
}]
43+
const { isLoading: isLoadingOpps, data: opportunities, error: errorOpps } = useGetOpportunities(products, postParams);
5644

5745
const app = {
5846
userParams,
@@ -69,8 +57,7 @@ export default function AppProvider({ children }) {
6957
setHoveredOpportunity,
7058
openFilters,
7159
setOpenFilters,
72-
setHoveredOpportunity,
73-
providers
60+
setHoveredOpportunity
7461
}
7562

7663
return (

demo/app/src/hooks/useGetOpportunities.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
11
import { useEffect, useState } from "react";
22
import useLocalStorage from "./useLocalStorage";
3+
import { ALL_PROVIDERS } from "src/utils/constants";
34

4-
const useGetOpportunities = (postParams) => {
5+
function fetchOpportunity(token, provider, params){
6+
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+
});
15+
};
16+
17+
const useGetOpportunities = (products, postParams) => {
518
const {params, provider} = !!postParams && postParams;
619
const [data, setData] = useState(null);
720
const [isLoading, setIsLoading] = useState(false);
821
const [error, setError] = useState("");
922
const { userToken } = useLocalStorage();
1023

24+
function fetchAllProviderProducts(p){
25+
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}});
27+
});
28+
};
29+
1130
useEffect(() => {
1231
if (params) {
1332
setIsLoading(true);
1433
setError(false);
15-
fetch("/api/opportunities", {
16-
method: "POST",
17-
headers: {
18-
"Content-type": "application/json",
19-
"Backend": provider,
20-
'Authorization': `Bearer ${userToken}`
21-
},
22-
body: JSON.stringify(params)
23-
})
24-
.then((res) => res.json())
25-
.then((data) => {
26-
setData(data.features)
27-
setIsLoading(false)
28-
}).catch(e => {
29-
setError(e);
30-
setIsLoading(false);
31-
});
34+
// By default fetch all provider product opportunities
35+
if(!provider && !params["product_id"]){
36+
const allProvidersOpportunities = ALL_PROVIDERS.reduce((all, p) => {
37+
const promises = fetchAllProviderProducts(p.id);
38+
return promises ? [...all, ...promises] : all;
39+
}, []);
40+
41+
Promise.all(allProvidersOpportunities).then(results => {
42+
setData(Object.fromEntries(Object.values(results).map(value => {
43+
return [value.provider, value.data.features]
44+
})));
45+
setIsLoading(false);
46+
}).catch(e => {
47+
setError(e);
48+
setIsLoading(false);
49+
});
50+
}
3251
}
3352
}, [params, provider]);
3453

demo/app/src/hooks/useGetProducts.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
import { useEffect, useState } from "react";
22
import useLocalStorage from "./useLocalStorage";
3+
import { ALL_PROVIDERS } from "src/utils/constants";
34

45
const useGetProducts = () => {
5-
const [data, setData] = useState(null);
6+
const [data, setData] = useState({});
67
const [isLoading, setIsLoading] = useState(true);
78
const [error, setError] = useState("");
89
const { userToken } = useLocalStorage();
910

1011
useEffect(() => {
11-
fetch('/api/products', {headers: new Headers({
12-
'Backend': 'earthsearch',
13-
'Authorization': `Bearer ${userToken}`
14-
})})
15-
.then((res) => res.json())
16-
.then((data) => {
17-
setData(data['products'])
12+
const allProductRequests = ALL_PROVIDERS.map(async provider => {
13+
return fetch('/api/products', {headers: new Headers({
14+
'Backend': provider.id,
15+
'Authorization': `Bearer ${userToken}`
16+
})})
17+
.then(async res => await res.json())
18+
.then(data => { return {'provider': provider.id, 'data': data}})
19+
});
20+
21+
Promise.all(allProductRequests).then((results) => {
22+
setData(Object.fromEntries(Object.values(results).map(value => {
23+
return [value.provider, value.data.products]
24+
})));
1825
setIsLoading(false)
1926
}).catch(e => setError(e));
27+
2028
}, []);
2129

2230
return { data, isLoading, error };

demo/app/src/utils/constants.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const ALL_PROVIDERS = [{
2+
id: 'earthsearch',
3+
name: 'EarthSearch'
4+
}, {
5+
id: 'blacksky',
6+
name: 'BlackSky'
7+
}, {
8+
id: 'planet',
9+
name: 'Planet'
10+
}, {
11+
id: 'umbra',
12+
name: 'Umbra'
13+
}];

0 commit comments

Comments
 (0)