-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from GreenWizard2015/issues/11
Issues/11
- Loading branch information
Showing
10 changed files
with
276 additions
and
41 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 |
---|---|---|
|
@@ -10,6 +10,5 @@ | |
|
||
.hold-to-pour-image { | ||
object-fit: contain; | ||
width: 25%; | ||
height: auto; | ||
} |
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
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
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,60 @@ | ||
.tea-glass { | ||
height: 100%; | ||
position: relative; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
user-select: none; | ||
} | ||
|
||
.tea-container { | ||
position: relative; | ||
height: 100%; | ||
max-width: 100%; | ||
width: fit-content; | ||
display: flex; | ||
padding: 1rem; | ||
} | ||
|
||
.cup-image { | ||
max-height: 30vh; | ||
max-width: 100%; | ||
object-fit: contain; | ||
position: relative; | ||
z-index: 1; | ||
cursor: pointer; | ||
} | ||
|
||
.tea-level { | ||
position: absolute; | ||
left: 0px; | ||
right: 0px; | ||
height: 3px; | ||
background-color: #76b7b2; | ||
z-index: 2; | ||
color: #76b7b2; | ||
} | ||
|
||
.est-tea-level { | ||
position: absolute; | ||
left: 0px; | ||
right: 0px; | ||
height: 3px; | ||
border-bottom: 3px dashed red; | ||
z-index: 3; | ||
text-align: right; | ||
padding-bottom: 2rem; | ||
color: red; | ||
} | ||
|
||
.prev-tea-level { | ||
position: absolute; | ||
left: 0px; | ||
right: 0px; | ||
height: 3px; | ||
border-bottom: 3px dashed black; | ||
z-index: 3; | ||
text-align: center; | ||
padding-bottom: 2rem; | ||
color: black; | ||
} |
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,94 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import './TeaLevel.css'; | ||
import cup from './cup.jpg'; | ||
import { connect } from 'react-redux'; | ||
import { changeEstimatedTeaLevel, changeStartTeaLevel } from '../store/slices/Temp'; | ||
import { changeSpeed } from '../store/slices/UI'; | ||
|
||
const bottomLevel = 20; // where the tea starts (0%) | ||
const maxLevel = 80; // where the tea ends (100%) | ||
|
||
function toRealLevel(level) { | ||
const H = maxLevel - bottomLevel; | ||
return (level / 100 * H) + bottomLevel; | ||
} | ||
|
||
function toPercentage(realLevel) { | ||
const H = maxLevel - bottomLevel; | ||
return (realLevel - bottomLevel) / H * 100; | ||
} | ||
|
||
function TeaLevel({ | ||
lastOperationDuration, speed, startTeaLevel, estimatedTeaLevel, | ||
changeStartTeaLevel, changeEstimatedTeaLevel, changeSpeed, lastTeaLevel, | ||
}) { | ||
const [calcSpeed, setCalcSpeed] = useState(speed); | ||
// update the estimated level if speed or duration changes | ||
useEffect(() => { | ||
const estimatedLevel = startTeaLevel + speed * lastOperationDuration / 1000; | ||
changeEstimatedTeaLevel(estimatedLevel); | ||
}, [lastOperationDuration, speed, startTeaLevel, changeEstimatedTeaLevel]); | ||
|
||
const handleCupClick = (e) => { | ||
const { top, height } = e.target.getBoundingClientRect(); | ||
const clickY = e.clientY; | ||
const clickedPosition = top - clickY + height; | ||
const clickedPercentage = (clickedPosition / height) * 100; | ||
const newLevel = toPercentage(clickedPercentage); | ||
// limit the new level to the range [0, 100] | ||
const level = Math.min(Math.max(newLevel, 0), 100); | ||
changeStartTeaLevel( level ); | ||
// find speed | ||
const newSpeed = (level - lastTeaLevel) / (lastOperationDuration / 1000); | ||
setCalcSpeed(newSpeed); | ||
}; | ||
|
||
function onSpeedSet(e) { | ||
e.preventDefault(); | ||
changeSpeed(calcSpeed); | ||
} | ||
|
||
return ( | ||
<> | ||
<div> | ||
<div className="tea-glass"> | ||
<div className="tea-container"> | ||
<img src={cup} alt="Cup" className="cup-image" draggable="false" | ||
onClick={handleCupClick} | ||
/> | ||
<div className="tea-level" style={{ bottom: `${toRealLevel(startTeaLevel)}%` }}> | ||
{startTeaLevel.toFixed(0)}% | ||
</div> | ||
|
||
<div className="est-tea-level" style={{ bottom: `${toRealLevel(estimatedTeaLevel)}%` }}> | ||
{estimatedTeaLevel.toFixed(0)}% | ||
</div> | ||
|
||
<div className="prev-tea-level" style={{ bottom: `${toRealLevel(lastTeaLevel)}%` }}> | ||
{lastTeaLevel.toFixed(0)}% | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div> | ||
<input | ||
type="number" step="0.01" | ||
value={calcSpeed.toFixed(2)} | ||
onChange={(e) => setCalcSpeed(parseFloat(e.target.value))} | ||
/> | ||
<button onClick={onSpeedSet}>Set Speed</button> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default connect( | ||
state => ({ | ||
lastOperationDuration: state.Temp.lastOperationDuration, | ||
speed: state.UI.speed, | ||
startTeaLevel: state.Temp.startTeaLevel, | ||
estimatedTeaLevel: state.Temp.estimatedTeaLevel, | ||
lastTeaLevel: state.Temp.prevTeaLevel, | ||
}), | ||
{ changeStartTeaLevel, changeEstimatedTeaLevel, changeSpeed } | ||
)(TeaLevel); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.