Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pap Laci 2nd házi #14

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.error {
border: 5px solid red;
}
14 changes: 11 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import './App.css';
import React from "react";
import Converter from './components/Converter';

function App() {
const convertCountRef = React.useRef(0);

return (
<div className="App">
</div>
<div>
<Converter label="BTC" exchangeRate={995} convertCount={convertCountRef} />
<br/>
<Converter label="ETH" exchangeRate={1.2} convertCount={convertCountRef} />
</div>

);
}

export default App;
export default App;
28 changes: 28 additions & 0 deletions src/components/Amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import '../App.css';
import React, { useContext} from 'react';
import ThemeContext from '../context/ThemeContext';


export default function Amount({label, value, onChange, isReadOnly, isRedBorderAdded}) {

const theme = useContext(ThemeContext);

return (
<div style={{ background: theme.background, color: theme.foreground }}>
<div className={(isRedBorderAdded && !isReadOnly) ? "error" : "App"}>
<label>{label}:
<input type="number" value={value} readOnly={isReadOnly} onChange=
{
(event) => {
onChange(event.target)
}
}
>
</input>
</label>
</div>
</div>


);
}
75 changes: 75 additions & 0 deletions src/components/Converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import '../App.css';
import React, { useEffect, useContext, useState, useRef } from 'react';
import Amount from './Amount';
import ThemeContext from '../context/ThemeContext';

const defaultEuroValue=0;
const defaultConvertedValue=0;
const themes = {
light: {
foreground: "#000000",
background: "#eeeeee"
},
dark: {
foreground: "#ffffff",
background: "#222222"
}
};
var isRedBorderAdded = false;
var currentTheme = themes.light;

export default function Converter({label, exchangeRate, convertCount}) {

const [mystate, setMyState] = React.useState({euroValue: defaultEuroValue, convertedValue: defaultConvertedValue, currentTheme: themes.light});
const {euroValue, convertedValue, currentTheme} = mystate;
const titleRef = React.useRef("Convert 0 euros to " + label);

const myOnChange = (element) => {
let calcEur = element.value;
let calcConvertedValue = element.value / exchangeRate;
let theme=currentTheme;

if (convertCount.current == 5) {
alert('Convert without limits by becoming a premium user');
} else {

if (element.value >= 0){
isRedBorderAdded = false;
} else {
isRedBorderAdded = true;
}

if (theme==themes.light){
theme = themes.dark;
} else {
theme=themes.light;
}

convertCount.current=convertCount.current+1;

titleRef.current="Convert " + calcEur + " euros to " + label;

setMyState({
euroValue: calcEur,
convertedValue: calcConvertedValue,
currentTheme: theme
});

}
}

return (
<ThemeContext.Provider value={currentTheme}>
<div>
<div ref={titleRef}>
Title: {titleRef.current}
</div>
<Amount label="Euros" value={euroValue} onChange={myOnChange} isRedBorderAdded={isRedBorderAdded} />
<label>{label}:
{convertedValue}
</label>
</div>
</ThemeContext.Provider>

);
}
5 changes: 5 additions & 0 deletions src/context/ThemeContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

const ThemeContext = React.createContext({});

export default ThemeContext;