Created
April 4, 2020 02:32
-
-
Save roguemaps/32df4df7bb1fa6002651525173497ed6 to your computer and use it in GitHub Desktop.
Sample Top 20 pizza topping combos display (simple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useEffect, useState} from 'react'; | |
import findIndex from 'lodash/findIndex'; | |
import orderBy from 'lodash/orderBy'; | |
import './App.css'; | |
function App() { | |
const [toppingComboCounts, setToppings] = useState({ toppingComboCounts: [] }); | |
const sortData = async (pizzaData) => { | |
const toppingsCountObj = await pizzaData.reduce((toppingsCount, currentTopping) => { | |
const foundToppingIndex = findIndex(toppingsCount, { 'combo': currentTopping.toppings }); | |
if (foundToppingIndex < 0) { | |
const toppingObj = { combo: currentTopping.toppings, count: 1 }; | |
toppingsCount.push(toppingObj); | |
} else { | |
toppingsCount[foundToppingIndex].count += 1; | |
} | |
return toppingsCount; | |
}, []); | |
const sortedToppings = orderBy(toppingsCountObj, ['count'], ['desc']); | |
setToppings(sortedToppings); | |
} | |
const generateViewArray = (startIndex, endIndex) => { | |
if (Array.isArray(toppingComboCounts)) { | |
const viewArray = toppingComboCounts.slice(startIndex, endIndex); | |
console.log("viewArray", viewArray); | |
const topCombos = viewArray.map((comboObj, currIndex) => { | |
return ( | |
<div key={currIndex} className="comboDisplay"> | |
<div className="padding-side-10">{comboObj.combo.map((topping, toppingIndex) => { | |
return <div key={`topping-${toppingIndex}`}>{topping},</div> | |
})} | |
</div> | |
<div>{comboObj.count}</div> | |
</div> | |
) | |
}); | |
return topCombos; | |
} | |
} | |
useEffect(() => { | |
const fetchData = async () => { | |
const proxyUrl = ' https://cors-anywhere.herokuapp.com/'; | |
const response = await fetch( | |
proxyUrl + | |
'https://www.olo.com/pizzas.json', | |
{ | |
method: 'GET', | |
mode: 'cors', | |
} | |
); | |
const data = await response.json(); | |
sortData(data); | |
}; | |
try{ | |
fetchData(); | |
} catch(e) { | |
throw new Error("There was a problem fetching data", e); | |
} | |
}, []); | |
return ( | |
<div className="App"> | |
<div class="topComboArea"> | |
{generateViewArray(0, 20)} | |
</div> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment