Browse Source

hw timer react-jsx

Mitrofanova Natali 2 years ago
parent
commit
62eb961856

+ 23 - 0
HW18 React jsx/.gitignore

@@ -0,0 +1,23 @@
+# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
+
+# dependencies
+/node_modules
+/.pnp
+.pnp.js
+
+# testing
+/coverage
+
+# production
+/build
+
+# misc
+.DS_Store
+.env.local
+.env.development.local
+.env.test.local
+.env.production.local
+
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*

+ 70 - 0
HW18 React jsx/README.md

@@ -0,0 +1,70 @@
+# Getting Started with Create React App
+
+This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
+
+## Available Scripts
+
+In the project directory, you can run:
+
+### `npm start`
+
+Runs the app in the development mode.\
+Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
+
+The page will reload when you make changes.\
+You may also see any lint errors in the console.
+
+### `npm test`
+
+Launches the test runner in the interactive watch mode.\
+See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
+
+### `npm run build`
+
+Builds the app for production to the `build` folder.\
+It correctly bundles React in production mode and optimizes the build for the best performance.
+
+The build is minified and the filenames include the hashes.\
+Your app is ready to be deployed!
+
+See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
+
+### `npm run eject`
+
+**Note: this is a one-way operation. Once you `eject`, you can't go back!**
+
+If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
+
+Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
+
+You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
+
+## Learn More
+
+You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
+
+To learn React, check out the [React documentation](https://reactjs.org/).
+
+### Code Splitting
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
+
+### Analyzing the Bundle Size
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
+
+### Making a Progressive Web App
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
+
+### Advanced Configuration
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
+
+### Deployment
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
+
+### `npm run build` fails to minify
+
+This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

File diff suppressed because it is too large
+ 11445 - 0
HW18 React jsx/package-lock.json


+ 38 - 0
HW18 React jsx/package.json

@@ -0,0 +1,38 @@
+{
+  "name": "hw-react-jsx",
+  "version": "0.1.0",
+  "private": true,
+  "dependencies": {
+    "@testing-library/jest-dom": "^5.16.1",
+    "@testing-library/react": "^12.1.2",
+    "@testing-library/user-event": "^13.5.0",
+    "react": "^17.0.2",
+    "react-dom": "^17.0.2",
+    "react-scripts": "5.0.0",
+    "web-vitals": "^2.1.4"
+  },
+  "scripts": {
+    "start": "react-scripts start",
+    "build": "react-scripts build",
+    "test": "react-scripts test",
+    "eject": "react-scripts eject"
+  },
+  "eslintConfig": {
+    "extends": [
+      "react-app",
+      "react-app/jest"
+    ]
+  },
+  "browserslist": {
+    "production": [
+      ">0.2%",
+      "not dead",
+      "not op_mini all"
+    ],
+    "development": [
+      "last 1 chrome version",
+      "last 1 firefox version",
+      "last 1 safari version"
+    ]
+  }
+}

+ 13 - 0
HW18 React jsx/public/index.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1" />
+    <title>React App</title>
+  </head>
+  <body>
+    <noscript>You need to enable JavaScript to run this app.</noscript>
+    <div id="root"></div>
+
+  </body>
+</html>

+ 25 - 0
HW18 React jsx/public/manifest.json

@@ -0,0 +1,25 @@
+{
+  "short_name": "React App",
+  "name": "Create React App Sample",
+  "icons": [
+    {
+      "src": "favicon.ico",
+      "sizes": "64x64 32x32 24x24 16x16",
+      "type": "image/x-icon"
+    },
+    {
+      "src": "logo192.png",
+      "type": "image/png",
+      "sizes": "192x192"
+    },
+    {
+      "src": "logo512.png",
+      "type": "image/png",
+      "sizes": "512x512"
+    }
+  ],
+  "start_url": ".",
+  "display": "standalone",
+  "theme_color": "#000000",
+  "background_color": "#ffffff"
+}

+ 40 - 0
HW18 React jsx/src/App.css

@@ -0,0 +1,40 @@
+.App{
+ margin: 20px;
+}
+
+.btn{
+    background-color: papayawhip;
+
+}
+.passwordWrapper{
+    display: flex;
+    flex-direction: column;
+    max-width: 300px;
+    margin: 10px 0;
+
+}
+.passwordWrapper input{
+    margin-top: 10px;
+}
+.clockBlock{
+    position: relative;
+    margin-top: 100px;
+}
+.clockFace{
+    position: absolute;
+    z-index: 1;
+
+}
+.clockHour{
+    position: absolute;
+    z-index: 2;
+
+}
+.clockMin{
+    position: absolute;
+    z-index: 3;
+}
+.clockSec{
+    position: absolute;
+    z-index: 4;
+}

+ 231 - 0
HW18 React jsx/src/App.js

@@ -0,0 +1,231 @@
+import './App.css';
+import {useState, useEffect} from "react";
+import React from "react";
+
+import clockFace from "./img/clockFace.png";
+import clockHour from "./img/clockFace_Hour.png";
+import clockMin from "./img/clockFace_Min.png";
+import clockSec from "./img/clockFace_Sec.png"
+
+const Spoiler = ({header = "+", open = true, children, setIsOpen}) => {
+
+    return (
+        <>
+            <div className={"btn"} onClick={() => setIsOpen(!open)}>
+                {header}
+            </div>
+            {open && children}
+        </>
+    )
+}
+
+const RangeInput = ({min, max}) => {
+    const [isValid, setIsValid] = useState(false)
+    const color = isValid ? "" : "red";
+
+    const inputHandler = (e) => {
+        if (min < e.target.value.length && e.target.value.length < max) {
+            setIsValid(true)
+        } else {
+            setIsValid(false)
+        }
+    }
+    return (
+        <>
+            <input style={{color: color}} onChange={inputHandler}/>
+        </>
+    )
+}
+const PasswordConfirm = ({min}) => {
+    const [isValid2, setIsValid2] = useState(false)
+    const color = isValid2 ? "" : "red";
+
+    const [value1, setStateInput1] = useState("");
+    const [value2, setStateInput2] = useState("");
+
+    const inputHandler1 = (e) => {
+        const value = e.target.value
+        setStateInput1(e.target.value)
+
+        if ((value.length > min && value2.length > min) && (value === value2)) {
+            setIsValid2(true)
+        } else {
+            setIsValid2(false)
+        }
+    }
+    const inputHandler2 = (e) => {
+        const value = e.target.value
+        console.log(value)
+        setStateInput2(e.target.value)
+        if ((value.length > min && value1.length > min) && (value === value1)) {
+            setIsValid2(true)
+        } else {
+            setIsValid2(false)
+        }
+    }
+    return (
+        <div className="passwordWrapper">
+
+            <input style={{color: color}} type='password' onChange={inputHandler1} placeholder={"password"}/>
+            <input style={{color: color}} type='password' onChange={inputHandler2} placeholder={"repeat password"}/>
+        </div>
+    )
+}
+
+
+const Timer = ({sec = 30}) => {
+
+    const [time, setTime] = useState(sec);
+    const [allowTimer, setAllowTimer] = useState(false)
+    let hours = Math.floor(time / 60 / 60)
+    let minutes = Math.floor(time / 60 % 60)
+    let seconds = time % 60
+
+    const buttonHandler = () => {
+        setAllowTimer(!allowTimer)
+    }
+    useEffect(() => {
+        if (allowTimer && time > 0) {
+            const idTimer = setInterval(() => setTime(+time - 1), 1000)
+            return () => clearInterval(idTimer)
+        }
+    }, [time, allowTimer])
+
+    return (
+        <>
+            <div>
+                <button onClick={buttonHandler}>{!allowTimer ? "run" : "stop"}</button>
+            </div>
+            <div>{hours < 10 ? "0" + hours : hours} : {minutes < 10 ? "0" + minutes : minutes} : {seconds < 10 ? "0" + seconds : seconds}</div>
+        </>
+    )
+}
+const TimerControl = () => {
+    const [hoursValue, setHoursValue] = useState(0)
+    const [minutesValue, setMinutesValue] = useState(0)
+    const [secondsValue, setSecondValue] = useState(0)
+    const [isTimerStarted, setTimerStarted] = useState(false)
+
+    const changeValue = (e) => {
+        const value = +e.target.value
+        if (!isNaN(value) && value < 60 && value > 0) {
+
+            switch (e.target.id) {
+
+                case "hours" :
+                    setHoursValue(value)
+                    break
+                case "minute" :
+                    setMinutesValue(value)
+                    break
+                case "seconds" :
+                    setSecondValue(value)
+                    break
+                default :
+                    (console.log("Упс"))
+            }
+        }
+    }
+
+    const buttonHandler = () => {
+        setTimerStarted(!isTimerStarted)
+    }
+
+    return (
+        <>
+            <input id="hours" onChange={changeValue} value={hoursValue}/>
+            <input id="minute" onChange={changeValue} value={minutesValue}/>
+            <input id="seconds" onChange={changeValue} value={secondsValue}/>
+            <button onClick={buttonHandler}>{isTimerStarted ? "Delete timer" : "Creat timer"}</button>
+            {isTimerStarted && <Timer sec={hoursValue * 60 * 60 + minutesValue * 60 + secondsValue}/>}
+        </>
+    )
+}
+
+const SecondsTimer = ({seconds}) => <h2>{seconds} seconds</h2>
+
+const TimerContainer = ({seconds, refresh, render}) => {
+    const [currentTime, setCurrentTime] = useState(performance.now())
+
+    const [time, setTime] = useState(seconds)
+    const Render = render;
+    let hours = Math.floor(time / 60 / 60)
+    let minutes = Math.floor(time / 60 % 60)
+    let sec = Math.round(time) % 60
+
+    const intervalFunc = () => {
+        const currentTimeAfterSecond = performance.now()
+        const realTimeInterval = currentTimeAfterSecond - currentTime
+
+        setTime(+time - (realTimeInterval / 1000))
+        setCurrentTime(currentTimeAfterSecond)
+    }
+
+    useEffect(() => {
+
+        if (time > 0) {
+            const idTimer = setInterval(intervalFunc, refresh)
+            return () => clearInterval(idTimer)
+        }
+    }, [currentTime])
+    return (
+        <>
+            <div>{hours < 10 ? "0" + hours : hours} : {minutes < 10 ? "0" + minutes : minutes} : {sec < 10 ? "0" + sec : sec}</div>
+            <Render seconds={seconds}/>
+            <TimerPresentation hour={hours} min={minutes} sec={sec}/>
+        </>
+    )
+}
+const TimerPresentation = ({hour, min, sec}) => {
+    return (
+        <div className="clockBlock">
+            <img src={clockFace} className="clockFace"/>
+            <img style={{transform: `rotate(${hour * 6}deg)`}} src={clockHour} className="clockHour"/>
+            <img style={{transform: `rotate(${min * 6}deg)`}} src={clockMin} className="clockMin"/>
+            <img style={{transform: `rotate(${sec * 6}deg)`}} src={clockSec} className="clockSec"/>
+
+        </div>
+    )
+}
+
+function App() {
+    const [isOpen, setIsOpen] = useState(true)
+    const [isOpen2, setIsOpen2] = useState(true)
+    return (
+        <div className="App">
+
+            <h4>Task: Spoiler: </h4>
+            <Spoiler header={"<h1>Заголовок</h1>"} open={isOpen} setIsOpen={setIsOpen}>
+                Контент 1
+                <p>
+                    лорем ипсум траливали и тп.
+                </p>
+            </Spoiler>
+            <Spoiler open={isOpen2} setIsOpen={setIsOpen2}>
+                <h2>Контент 2</h2>
+                <p>
+                    лорем ипсум траливали и тп.
+                </p>
+            </Spoiler>
+
+            <h4>Task: RangeInput:</h4>
+            <RangeInput min={2} max={10}/>
+
+            <h4>Task PasswordConfirm:</h4>
+            <PasswordConfirm min={2}/>
+
+            <h4>Task: Timer</h4>
+            <Timer sec={5000}/>
+
+            <h4>Task: TimerControl</h4>
+            <TimerControl/>
+
+            <h4>Task: TimerContainer</h4>
+            <TimerContainer seconds={1810} refresh={1000} render={SecondsTimer}/>
+
+
+        </div>
+    )
+}
+
+export default App;

+ 8 - 0
HW18 React jsx/src/App.test.js

@@ -0,0 +1,8 @@
+import { render, screen } from '@testing-library/react';
+import App from './App';
+
+test('renders learn react link', () => {
+  render(<App />);
+  const linkElement = screen.getByText(/learn react/i);
+  expect(linkElement).toBeInTheDocument();
+});

BIN
HW18 React jsx/src/img/clockFace.png


BIN
HW18 React jsx/src/img/clockFace_Hour.png


BIN
HW18 React jsx/src/img/clockFace_Min.png


BIN
HW18 React jsx/src/img/clockFace_Sec.png


+ 13 - 0
HW18 React jsx/src/index.css

@@ -0,0 +1,13 @@
+body {
+  margin: 0;
+  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
+    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
+    sans-serif;
+  -webkit-font-smoothing: antialiased;
+  -moz-osx-font-smoothing: grayscale;
+}
+
+code {
+  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
+    monospace;
+}

+ 13 - 0
HW18 React jsx/src/index.js

@@ -0,0 +1,13 @@
+import React from 'react';
+import ReactDOM from 'react-dom';
+import './index.css';
+import App from './App';
+
+ReactDOM.render(
+  <React.StrictMode>
+    <App />
+  </React.StrictMode>,
+  document.getElementById('root')
+);
+
+