App.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import userEvent from '@testing-library/user-event'
  2. import React, { Component, useState, useRef, useEffect } from 'react'
  3. import './App.css'
  4. // import Header from './components/Header';
  5. // import Navbar from './components/Navbar';
  6. // import Profile from './components/Profile';
  7. const Input = () => {
  8. const [value, setValue] = useState('asdf')
  9. return (
  10. <>
  11. <input value={value} onChange={e => setValue(e.target.value)}/>
  12. <p>{value.toUpperCase()}</p>
  13. </>
  14. )
  15. }
  16. const Spoiler = ({children, header:Header}) => {
  17. const [show, setShow] = useState(false)
  18. return (
  19. <>
  20. <Header onClick={() => setShow(!show)}/>
  21. {show && children}
  22. </>
  23. )
  24. }
  25. const RangeInput = ({min, max}) => {
  26. const [value, setValue] = useState("notRed")
  27. return (
  28. <>
  29. <input type="text" value={value} style = {{backgroundColor: (value.length < min) || (value.length > max) ? 'red' : 'white' }} onChange={e => {
  30. setValue(e.target.value);
  31. }}/>
  32. </>
  33. )
  34. }
  35. const PasswordConfirm = ({min}) => {
  36. const [firstPass, setValue] = useState("123");
  37. const [secondPass, setsecValue] = useState("123");
  38. const [color, setColorValue] = useState("red");
  39. const checkPassword = (newVal, confirmVal) => {
  40. return (newVal.length > min && confirmVal.length > min && newVal == confirmVal) ? "green" : "red";
  41. }
  42. return (
  43. <>
  44. <h3 style = {{color: color}} >{color}</h3>
  45. <input type="password" value = {firstPass} style = {{backgroundColor: color}} onChange = {(e) => {
  46. setValue(e.target.value);
  47. setColorValue(checkPassword(e.target.value, secondPass));
  48. }}
  49. />
  50. <input type="password"value = {secondPass} style = {{backgroundColor: color}} onChange = {(e) => {
  51. setsecValue(e.target.value);
  52. setColorValue(checkPassword(e.target.value, firstPass));
  53. }}/>
  54. </>
  55. )
  56. }
  57. const Timer = ({sec}) => {
  58. var h = Math.floor(sec / 3600);
  59. var m = Math.floor(sec % 3600 / 60);
  60. var s = Math.floor(sec % 3600 % 60);
  61. const [count, changeCount] = useState(`${h}: ${m}: ${s}`);
  62. const [lock, closer] = useState(true);
  63. const [onOff, turning] = useState(false);
  64. const countRef = useRef(onOff);
  65. return (
  66. <>
  67. <h3>Timer {count}</h3>
  68. <button onClick = {() => {
  69. turning(!onOff);
  70. countRef.current = onOff;
  71. lock && (() => {
  72. setInterval((() => {
  73. if(sec && countRef.current == false) {
  74. sec -=1;
  75. h = Math.floor(sec / 3600);
  76. m = Math.floor(sec % 3600 / 60);
  77. s = Math.floor(sec % 3600 % 60);
  78. changeCount(`${h}: ${m}: ${s}`);
  79. } else {
  80. return
  81. }
  82. }), 1000);
  83. })();
  84. closer(false);
  85. }}>Start/pause</button>
  86. </>
  87. )
  88. }
  89. const TimerControl = ({}) => {
  90. const [hours, setHours] = useState("1");
  91. const [minutes, setMinutes] = useState("1");
  92. const [sec, setSec] = useState("1");
  93. const [buttOn, turnOnOff] = useState(true);
  94. const [countDownSown, show] = useState(false);
  95. const checkNumbers = (newVal, confirmVal) => {
  96. return (confirmVal.includes(".") || newVal.includes(".") || newVal < 0 || +confirmVal < 0 || +newVal > 60 || +confirmVal > 60) ? turnOnOff(false) : turnOnOff(true);
  97. }
  98. return (
  99. <>
  100. <h3>Timer Control</h3>
  101. <div>
  102. <input type = "number" id = "hours" value = {hours} onChange={(e) => {
  103. setHours(e.target.value)}}/>
  104. <input type = "number" id = "minutes" value = {minutes} style = {{backgroundColor: (minutes < 0) || (minutes > 59) || minutes.includes(".") ? 'red' : 'white' }} onChange={(e) => {
  105. setMinutes(e.target.value);
  106. checkNumbers(e.target.value, sec)}}/>
  107. <input type = "number" id = "sec" value = {sec} style = {{backgroundColor: (sec < 0) || (sec > 59) || (sec.includes(".")) ? 'red' : 'white' }} onChange={(e) => {
  108. setSec(e.target.value);
  109. checkNumbers(e.target.value, minutes)}}/>
  110. <button disabled={!buttOn} onClick={ () => show(true) }>Start/pause</button>
  111. <div id = "timer">
  112. { countDownSown && <Timer sec={((+hours * 60 * 60) + (+minutes * 60) + +sec)} /> }
  113. </div>
  114. </div>
  115. </>
  116. )
  117. }
  118. const SecondsTimer = ({seconds}) => <h2>{seconds}</h2>;
  119. const TimerContainer = ({seconds, refresh, render}) => {
  120. const Render = render;
  121. const [count, setCount] = useState(300);
  122. useEffect(() => {
  123. var firstState = Date.now()
  124. var secCounter;
  125. var interval = setInterval(() => {
  126. secCounter = seconds - (Math.floor((Date.now()/1000) - firstState/1000));
  127. setCount(secCounter);
  128. if (secCounter < 0) {
  129. setCount(0)
  130. return clearInterval(interval)
  131. }
  132. }, refresh);
  133. }, []);
  134. return (
  135. <>
  136. <Render seconds = {count}/>
  137. </>
  138. )
  139. }
  140. const Timer2 = ({seconds}) => {
  141. var h = Math.floor(seconds / 3600);
  142. var m = Math.floor(seconds % 3600 / 60);
  143. var s = Math.floor(seconds % 3600 % 60);
  144. return (
  145. <>
  146. <h3>Timer {`${h}: ${m}: ${s}`}</h3>
  147. <button onClick = {() => {
  148. }}>Start/pause</button>
  149. </>
  150. )
  151. }
  152. const Watches = ({seconds}) => {
  153. var h = Math.floor(seconds / 3600) * 30;
  154. var m = Math.floor(seconds % 3600 / 60) * (360 / 60);
  155. var s = Math.floor(seconds % 3600 % 60) * (360 / 60);
  156. return (
  157. <>
  158. <div id = "container" style = {{position: "relative"}}>
  159. <img id = "clock" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace.png" style = {{position: "absolute", zIndex :"0"}}/>
  160. <img id = "hours" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace_H.png" style = {{position: "absolute", zIndex :"1", transform: `rotate(${h}deg)`}}/>
  161. <img id = "minutes" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace_M.png" style = {{position: "absolute", zIndex :"2", transform: `rotate(${m}deg)`}}/>
  162. <img id = "secunds" src = "http://draw.asmer.fe.a-level.com.ua/ClockFace/ClockFace_S.png"style = {{position: "absolute", zIndex :"3", transform: `rotate(${s}deg)`}}/>
  163. </div>
  164. </>
  165. )
  166. }
  167. const TimerContainerControl = ({refresh, render}) => {
  168. const [hours, setHours] = useState("1");
  169. const [minutes, setMinutes] = useState("1");
  170. const [sec, setSec] = useState("1");
  171. const [buttOn, turnOnOff] = useState(true);
  172. const [countDownSown, show] = useState(false);
  173. const [msc, letsCount] = useState("0");
  174. const Render = render;
  175. const checkNumbers = (newVal, confirmVal) => {
  176. return (confirmVal.includes(".") || newVal.includes(".") || newVal < 0 || +confirmVal < 0 || +newVal > 60 || +confirmVal > 60) ? turnOnOff(false) : turnOnOff(true);
  177. }
  178. useEffect(() => {
  179. if (!countDownSown) {
  180. return clearInterval(interval)
  181. }
  182. var firstState = Date.now()
  183. var secCounter;
  184. var interval = setInterval(() => {
  185. secCounter = msc + (Math.floor((Date.now()/1000) - firstState/1000));
  186. letsCount(secCounter);
  187. }, refresh);
  188. }, [countDownSown]);
  189. return (
  190. <>
  191. <h3>Timer Control</h3>
  192. <div>
  193. <input type = "number" id = "hours" value = {hours} onChange={(e) => {
  194. setHours(e.target.value)}}/>
  195. <input type = "number" id = "minutes" value = {minutes} style = {{backgroundColor: (minutes < 0) || (minutes > 59) || minutes.includes(".") ? 'red' : 'white' }} onChange={(e) => {
  196. setMinutes(e.target.value);
  197. checkNumbers(e.target.value, sec)}}/>
  198. <input type = "number" id = "sec" value = {sec} style = {{backgroundColor: (sec < 0) || (sec > 59) || (sec.includes(".")) ? 'red' : 'white' }} onChange={(e) => {
  199. setSec(e.target.value);
  200. checkNumbers(e.target.value, minutes)}}/>
  201. <button disabled={!buttOn} onClick={ () => {
  202. show(true);
  203. letsCount(((+hours * 60 * 60) + (+minutes * 60) + +sec))
  204. }}>Start/pause</button>
  205. <div id = "timer">
  206. { countDownSown && <Render seconds={msc} /> }
  207. </div>
  208. </div>
  209. </>
  210. )
  211. }
  212. const App = () => {
  213. return (
  214. <>
  215. <div className = "spoiler">
  216. <Spoiler header={({ ...props }) => <h2 {...props}>Show</h2>}>
  217. <Input/>
  218. <Input />
  219. <Spoiler header={({ onClick }) => <button onClick={onClick}>Show</button>}>
  220. <Input />
  221. <Input />
  222. </Spoiler>
  223. </Spoiler>
  224. </div>
  225. <div id = "rangeInput">
  226. <h2>Range Input</h2>
  227. <RangeInput min={2} max={10} />
  228. </div>
  229. <div id = "passwordConfirm">
  230. <PasswordConfirm min = {3} />
  231. </div>
  232. <div id = "timerControl">
  233. <TimerControl/>
  234. </div>
  235. <div id = "timerContainer">
  236. <h3>Timer Container</h3>
  237. <TimerContainer seconds={1800} refresh={100} render={SecondsTimer}/>
  238. </div>
  239. <div id = "LSD">
  240. <h3>LSD</h3>
  241. <TimerContainer seconds={300} refresh={100} render={Timer2}/>
  242. </div>
  243. <div id = "Watches">
  244. <h3>Watches + containerControl</h3>
  245. <TimerContainerControl refresh={100} render={Watches}/>
  246. </div>
  247. </>
  248. );
  249. }
  250. export default App;