Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
150830a
commit af3b1f8
Showing
3 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/1-readable/bad/*.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,117 @@ | ||
import React, { Component } from 'react'; | ||
|
||
class ReusableBad extends Component { | ||
class Inventory extends Component { | ||
constructor() { | ||
super(); | ||
this.state = { | ||
localCurrency: 'usd', | ||
inventory: [ | ||
{ | ||
product: 'Flashlight', | ||
img: '/flashlight.jpg', | ||
desc: 'A really great flashlight', | ||
price: 100, | ||
currency: 'usd', | ||
}, | ||
{ | ||
product: 'Tin can', | ||
img: '/tin_can.jpg', | ||
desc: 'Pretty much what you would expect from a tin can', | ||
price: 32, | ||
currency: 'usd', | ||
}, | ||
{ | ||
product: 'Cardboard Box', | ||
img: '/cardboard_box.png', | ||
desc: 'It holds things', | ||
price: 5, | ||
currency: 'usd', | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
convertCurrency(amount, fromCurrency, toCurrency) { | ||
let currencyConversions = { | ||
usd: { | ||
rupee: 66.78, | ||
yuan: 6.87, | ||
usd: 1, | ||
}, | ||
}; | ||
|
||
let currencySymbols = { | ||
usd: '$', | ||
rupee: '₹', | ||
yuan: '元', | ||
}; | ||
|
||
return currencySymbols[toCurrency] + amount * currencyConversions[fromCurrency][toCurrency]; | ||
} | ||
|
||
onSelectCurrency(e) { | ||
this.setState({ | ||
localCurrency: e.target.value, | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
Reusable Bad | ||
<label htmlFor="currencySelector">Currency:</label> | ||
<select | ||
className="u-full-width" | ||
id="currencySelector" | ||
onChange={this.onSelectCurrency.bind(this)} | ||
value={this.state.localCurrency} | ||
> | ||
<option value="usd">USD</option> | ||
<option value="rupee">Rupee</option> | ||
<option value="yuan">Yuan</option> | ||
</select> | ||
<table style={{ width: '100%' }}> | ||
<tbody> | ||
<tr> | ||
<th> | ||
Product | ||
</th> | ||
|
||
<th> | ||
Image | ||
</th> | ||
|
||
<th> | ||
Description | ||
</th> | ||
|
||
<th> | ||
Price | ||
</th> | ||
</tr> | ||
{this.state.inventory.map((item, idx) => ( | ||
<tr key={idx}> | ||
<td> | ||
{item.product} | ||
</td> | ||
|
||
<td> | ||
<img src={item.img} alt="" /> | ||
</td> | ||
|
||
<td> | ||
{item.desc} | ||
</td> | ||
|
||
<td> | ||
{this.convertCurrency(item.price, item.currency, this.state.localCurrency)} | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ReusableBad; | ||
export default Inventory; |
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