123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              noscript Your browser does not support JavaScript!
#root

            
          
!
            
              
$header-title: lighten(black, 40)
$header-symbol: lighten(DodgerBlue, 0)
$fieldset-border: darken(DodgerBlue, 20)
$legend: lighten(DodgerBlue, 0)
$input-border: lighten(black, 50)
$input-background: lighten(black, 96)
$info: lighten(black, 50)
$footer: lighten(black, 40)
$footer-link: lighten(DodgerBlue, 0)

*
	box-sizing: border-box

body
	font-family: sans-serif

header
	display: flex
	justify-content: center
	align-items: flex-end
	height: 160px

	h1
		color: $header-title
		font-weight: normal
		font-size: 34px
		letter-spacing: 0.01em
		padding: 0 0.3em 0.1em 0.3em
		border-style: hidden
		border-color: black
		border-width: 0 0 2px 0
		cursor: default
		&:hover
			.symbol
				color: $header-symbol

main
	display: flex
	flex-direction: column
	align-items: center
	
	fieldset
		border-style: double
		border-color: $fieldset-border
		max-width: 400px
		padding: 0.3em 3em
		text-align: center
		&:nth-of-type(1)
			margin-top: 20px
			padding-top: 2em
			border-width: 4px 4px 0 4px 
		&:nth-of-type(2)
			padding-bottom: 2em
			margin-bottom: 20px
			border-width: 0 4px 4px 4px 
	
		legend
			display: none
			border-style: solid
			border-color: $fieldset-border
			border-width: 0 2px
			color: $legend
			padding: 0.5em 0.8em 0.3em 0.8em
			text-align: left
		
		input
			border-width: 0 0 2px 0
			border-color: $input-border
			background-color: $input-background
			width: 200px
			font-size: 24px
			font-family: sans-serif
			font-weight: lighter
			&:focus
				outline: none
		
		select
			background: $legend
			color: white
			appearance: none
			border: none
			border-radius: 2px
			font-size: 16px
			font-family: sans-serif
			font-weight: lighter
			margin-left: 1em
			padding: 0.2em 0.1em 0.1em 0.3em
			&:focus
				outline: none
				outline-style: none
			&:-moz-focusring
				color: transparent
				text-shadow: 0 0 0 #000
			
	
.conversion-info
	display: flex
	justify-content: center
	align-items: center
	width: 100%
	margin-top: 80px
	margin-bottom: 80px
	
	p
		text-transform: lowercase
		font-weight: lighter
		margin: 0
		padding: 0 1em
		color: $info

footer
	display: flex
	position: fixed
	bottom: 0
	width: 100%
	height: 40px
	justify-content: center
	align-items: center
	background-color: rgba(white, 0.9)

	p
		color: $footer
		font-family: sans-serif
		font-weight: lighter
		font-size: 16px
		
		a
			color: $footer-link
			text-decoration: none

noscript
	display: flex
	position: relative
	width: 100%
	height: 100vh
	justify-content: center
	align-items: center
	font-family: sans-serif
	font-weight: lighter
	font-size: 24px
	color: crimson
            
          
!
            
              
/* --------------------
 * Fetching data test
 * --------------------
 */

// ------------------------------
/*fetch('https://api.fixer.io/latest')
	.then( data => data.json() )
	.then( data => { 
		const currencies = [];
		//console.log(data.base);
		currencies.push(data.base);
		currencies.push(...Object.entries(data.rates).map(currency => currency[0]));
		currencies.sort();
		//console.log(currencies);
	})
	.catch( err => console.log(err) );*/


/* --------------------
 * Global functions
 * --------------------
 */

// ------------------------------
function multiplay( variable, coefficient ) {
	return ( Math.round( (variable * coefficient) * 1000000 ) / 1000000 ).toString();
}

// ------------------------------
function tryConvert( value, rate ) {
	const variable = parseFloat(value.replace(',', '.'));
	const coefficient = parseFloat(rate);
	return Number.isNaN(variable) ? '' : (multiplay(variable, coefficient)).replace('.', ',');
}


/* --------------------
 * Footer
 * --------------------
 */

// ------------------------------
class Footer extends React.Component {
	render() {
		const fixerUrl = 'http://fixer.io';
		return (
			<footer>
				<p>data provided by <a href={fixerUrl} target='_blank'>Fixer.io</a></p>
			</footer>
		);
	}
}


/* --------------------
 * Main - Converter
 * --------------------
 */

// ------------------------------
class CurrencyInput extends React.Component {
	
	constructor(props) {
		super(props);
		this.handleInputClick = this.handleInputClick.bind(this);
		this.handleInputChange = this.handleInputChange.bind(this);
		this.handleCurrencyChange= this.handleCurrencyChange.bind(this);
	}
	
	handleInputClick(e) {
		this.props.onInputClick(this.props.selected, e.target.value);
	}
	
	handleInputChange(e) {
		this.props.onInputChange(e.target.value);
	}
	
	handleCurrencyChange(e) {
		this.props.onCurrencyChange(e.target.value, this.props.position);
	}
	
	render() {
		const { currencies, selected, value, position } = this.props;
		return(
			<fieldset>
				<legend>{selected}</legend>
				<input 
					value={value}
					onChange={this.handleInputChange}
					onClick={this.handleInputClick} />
				<select onChange={this.handleCurrencyChange}>
					{
						currencies.map(
							currency => {
								if(currency === selected) {
									return(
										<option key={currency} value={currency} selected>{currency}</option>
									);
								} else {
									return(
										<option key={currency} value={currency}>{currency}</option>
									);
								}
							}
						)
					}
				</select>
			</fieldset>
		);
	}
}

// ------------------------------
class Main extends React.Component {
	
	constructor(props) {
		super(props);
		this.state = {
			currencies: [],
			currencyNo1: 'EUR',
			currencyNo2: 'CZK',
			inputCurrency: '?',
			outputCurrency: '?',
			rate: 0,
			value: '',
			date: 'yyyy-mm-dd'
		}
		this.updateConversion = this.updateConversion.bind(this);
		this.updateCurrency = this.updateCurrency.bind(this);
		this.updateValue = this.updateValue.bind(this);
	}
	
	componentDidMount() {
		fetch('https://api.fixer.io/latest')
			.then(data => data.json())
			.then(data => {
				const currencies = [];
				currencies.push(data.base, ...Object.entries(data.rates).map(rates => rates[0]));
				currencies.sort();
				this.setState( { currencies } );
			})
			.catch(err => console.log(err));
  }
	
	updateConversion(inputCurrency, value) {
		const { currencyNo1, currencyNo2 } = this.state;
		const outputCurrency = inputCurrency === currencyNo1 ? currencyNo2 : currencyNo1; 
		fetch(`https://api.fixer.io/latest?base=${inputCurrency}`)
			.then(data => data.json())
			.then(data => {
				this.setState(
					{ 
						inputCurrency, 
						outputCurrency, 
						rate: data.rates[outputCurrency] || 1, 
						date: data.date,
						value
					}
				)
			})
			.catch(err => console.log(err))
	}
	
	updateValue(value) {
		this.setState(
			{ value }
		);
	}
	
	updateCurrency(currency, position) {
		this.setState(
			{ value: '', rate: 0, inputCurrency: '', outputCurrency: '' }
		);
		if(position === 1) {
			this.setState(
				{ currencyNo1: currency }
			);
		}
		if(position === 2) {
			this.setState(
				{ currencyNo2: currency }
			);
		}
	}
	
	render() {
		const { currencies, currencyNo1, currencyNo2, inputCurrency, outputCurrency, rate, date, value } = this.state;
		const newValue = value.replace('.', ',');
		const value1 = currencyNo1 === inputCurrency ? newValue : tryConvert(value, rate);
		const value2 = currencyNo2 === inputCurrency ? newValue : tryConvert(value, rate);
		return(
			<main>
				<CurrencyInput
					position={1}
					currencies={currencies}
					selected={currencyNo1}
					value={value1}
					onInputClick={this.updateConversion}
					onInputChange={this.updateValue}
					onCurrencyChange={this.updateCurrency} />
				<CurrencyInput
					position={2}
					currencies={currencies}
					selected={currencyNo2}
					value={value2}
					onInputClick={this.updateConversion}
					onInputChange={this.updateValue}
					onCurrencyChange={this.updateCurrency} />
				<section className='conversion-info'>
					<p>Rate: {rate}</p>
					<p>Latest update: {date}</p>
				</section>
			</main>
		);
	}
}


/* --------------------
 * Header
 * --------------------
 */

// ------------------------------
class Header extends React.Component {
	render() {
		return(
			<header>
				<h1>Currency Converter <span className='symbol'>&#x21C5;</span></h1>
			</header>
		);
	}
}


/* --------------------
 * App
 * --------------------
 */

// ------------------------------
class App extends React.Component {
	render() {
		return(
			<section className='app'>
				<Header />
				<Main />
				<Footer />
			</section>
		);
	}
}

// ------------------------------
ReactDOM.render(
	<App />,
	document.querySelector('#root')
);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console