Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ac5d9220e | |||
| 23db798577 | |||
| c99a045400 |
+65
-53
@@ -1,68 +1,80 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import { useState, createContext, useContext } from 'react'
|
||||||
|
import styles from './App.module.scss'
|
||||||
|
|
||||||
function tasksReducer(state, action) {
|
const UserContext = createContext('')
|
||||||
switch(action.type) {
|
|
||||||
case 'ADD_TASK':
|
function UserProvider({ children }) {
|
||||||
return [...state, action.payload]
|
const [name,setName] = useState('');
|
||||||
case 'TOGGLE_TASK':
|
|
||||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
const toggleName = (newName) => {
|
||||||
case 'REMOVE_TASK':
|
setName(newName);
|
||||||
return state.filter(task => task.id !== action.payload)
|
|
||||||
default:
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
|
||||||
console.log("Rendred")
|
|
||||||
return (
|
return (
|
||||||
<ul>
|
<UserContext.Provider value={{name, toggleName}}>
|
||||||
{tasks.map(({id, text, complite}) => (
|
{children}
|
||||||
<li key={id}
|
</UserContext.Provider>
|
||||||
style={{textDecoration: complite ? 'line-through' : 'none' }}>
|
|
||||||
<input type="checkbox" checked={complite} onChange={()=>onToggle(id)} />
|
|
||||||
{text}
|
|
||||||
<button onClick={()=> onRemove(id)} style={{marginLeft: 10}}>
|
|
||||||
Удалить
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
)
|
)
|
||||||
})
|
|
||||||
|
|
||||||
function App() {
|
|
||||||
const [tasks, dispatch] = useReducer(tasksReducer,[])
|
|
||||||
const [input,setInput] = useState('')
|
|
||||||
const [error,setError] = useState('')
|
|
||||||
const inputRef = useRef(null)
|
|
||||||
|
|
||||||
const addTask = () => {
|
|
||||||
if (input.trim() === '') {
|
|
||||||
setError('Пустой текст задачи')
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
setError('')
|
|
||||||
dispatch({
|
function Form() {
|
||||||
type: 'ADD_TASK',
|
const {name, toggleName} = useContext(UserContext)
|
||||||
payload: {id: Date.now(), text: input.trim(), complite: false }
|
const [newName,setNewName] = useState('');
|
||||||
})
|
const handleSubmit = (event) => {
|
||||||
setInput('')
|
event.preventDefault();
|
||||||
inputRef.current.focus()
|
toggleName(event.target[0].value)
|
||||||
}
|
}
|
||||||
|
const handlerChange = (event) => {
|
||||||
|
setNewName(event.target.value);
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<input type='text' value={newName} onChange={handlerChange} />
|
||||||
|
<button type='submit'>Войти</button>
|
||||||
|
</form>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function ExitButton() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{padding: 20}}>
|
<button onClick={() => toggleName('')}>Выход</button>
|
||||||
<h1>Список дел</h1>
|
)
|
||||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
}
|
||||||
<button onClick={addTask}>Добавить</button>
|
|
||||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
function Toolbar() {
|
||||||
|
const {name, toggleName} = useContext(UserContext)
|
||||||
|
return(
|
||||||
|
<div className={styles.container}>
|
||||||
|
<h3>Панель пользователя</h3>
|
||||||
|
{name && `Привет ${name}`}
|
||||||
|
{name ? <ExitButton /> : <Form /> }
|
||||||
|
|
||||||
<TaskList tasks={tasks}
|
|
||||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
|
||||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Page() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
||||||
|
<Toolbar />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
|
<h1>Hello, React!</h1>
|
||||||
|
<UserProvider>
|
||||||
|
<Page />
|
||||||
|
</UserProvider>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default App
|
export default App
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
.container {
|
||||||
|
height: 500px;
|
||||||
|
border: 1px solid black;
|
||||||
|
border-radius: 10px;
|
||||||
|
background-color: lightblue;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
font-size: 16pt;
|
||||||
|
border: 1px dotted gray;
|
||||||
|
/*margin: 5px 10px;
|
||||||
|
padding: 10px;*/
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user