From 276cd2d4a38218d81158f291c73457a34707d02c Mon Sep 17 00:00:00 2001 From: ynonp Date: Wed, 22 Jul 2020 15:44:12 +0300 Subject: [PATCH 1/2] comments / rewrite ex5 --- 11/5/src/colorpicker.js | 79 ++++++++++++++++++----------------------- package.json | 1 + 2 files changed, 36 insertions(+), 44 deletions(-) diff --git a/11/5/src/colorpicker.js b/11/5/src/colorpicker.js index 4cc6275..3f1ae67 100644 --- a/11/5/src/colorpicker.js +++ b/11/5/src/colorpicker.js @@ -2,49 +2,40 @@ import React, {useState} from 'react'; import tinycolor from 'tinycolor2'; import '../css/main.css'; - +import _ from 'lodash'; + +function LigherDivs({ color }) { + return _.range(5).map((i) => +
+ ); +} + +function DarkerDivs({ color }) { + return _.range(5).map((i) => +
+ ); +} + +function ColorPicker({ setColor }) { + return ( +
+ +
+ ); +} export default function (props) { - const {defaultColor} = props; - - const [color, setColor] = useState(defaultColor ? defaultColor : ''); - - - function createDivs() { - - let currentColor = tinycolor(color).darken(5*4); - - const divs = []; - for(let i = 0; i < 5; i++){ - console.log(i) - divs.push( -
- ); - } - - divs.push( -
- -
- ); - - currentColor = tinycolor(color).lighten(5); - - for(let i = 0; i < 5; i++){ - console.log(i) - divs.push( -
- ); - } - - return divs; - } - - return( -
- {createDivs()} -
- ) -} \ No newline at end of file + const {defaultColor} = props; + + const [color, setColor] = useState(defaultColor ? defaultColor : ''); + + return( +
+ + + +
+ ) +} diff --git a/package.json b/package.json index 9ea2afe..bb3ca7b 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ }, "devDependencies": { "css-loader": "^3.2.1", + "lodash": "^4.17.19", "mini-css-extract-plugin": "^0.8.0", "tinycolor2": "^1.4.1" } From 363ea4e1badd63a0671353f5a3bfb545fb64e563 Mon Sep 17 00:00:00 2001 From: ynonp Date: Thu, 23 Jul 2020 08:38:40 +0300 Subject: [PATCH 2/2] comments on ex16 --- 11/2/; | 32 ++++++++++++++++++++++++++++++++ 16/1/src/container.js | 9 ++++++++- 16/1/src/play.js | 26 ++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 11/2/; diff --git a/11/2/; b/11/2/; new file mode 100644 index 0000000..472d013 --- /dev/null +++ b/11/2/; @@ -0,0 +1,32 @@ +import React, {useEffect, useState} from 'react'; + +import '../css/main.css'; + +export default function () { + const [lastInputText, setLastInputText] = useState(''); + const [factor, setFactor] = useState(1); + + + function converter(time, factor) { + setLastInputText(time * factor); + } + + const h = + + return ( + <> +
+ + setInput(e.target.value, 3600)}/> +
+
+ + setInput(e.target.value, 60)}/> +
+
+ + setInput(e.target.value, 1)}/> +
+ + ) +} diff --git a/16/1/src/container.js b/16/1/src/container.js index 63a17f7..8162ace 100644 --- a/16/1/src/container.js +++ b/16/1/src/container.js @@ -10,8 +10,15 @@ export default function () { function setPointsPlay(check){ + // ternary is better used inside: + // const newValue = points + (check ? 10 : -5) + // (to save duplication) const newValue = check ? points+10 : points-5; + // I thought you liked ternaries... + // setPoints(newValue > 0 ? newValue : 0) + // but my favorite is: + // setPoints(Math.min(newValue, 0)) if(newValue > 0) setPoints(newValue); else setPoints(0); @@ -35,4 +42,4 @@ export default function () { function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; -} \ No newline at end of file +} diff --git a/16/1/src/play.js b/16/1/src/play.js index bdbada3..c48d18f 100644 --- a/16/1/src/play.js +++ b/16/1/src/play.js @@ -7,11 +7,33 @@ export default function play(props) { const {setPointsPlay, placeRed} = props; + // You're hiding a lot of logic inside the map, + // and that makes the code hard to read + // Better to refactor and extract most of the code to a sub-component + // or to a helper function + return(
{Array(10).fill(null).map( (item, index) => ( -
setPointsPlay(index+1 === placeRed) }>
+
setPointsPlay(index+1 === placeRed) }> +
))}
) -} \ No newline at end of file + + // What I would write: + return( +
+ {Array(10).fill(null).map((item, index) => + +
+ ) +}