Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e043df71e |
+55
-67
@@ -1,79 +1,67 @@
|
|||||||
import { useState, createContext, useContext } from 'react'
|
import React, { useState, useReducer, useRef, memo } from 'react'
|
||||||
import styles from './App.module.scss'
|
|
||||||
|
|
||||||
const UserContext = createContext('')
|
function tasksReducer(state, action) {
|
||||||
|
switch(action.type) {
|
||||||
function UserProvider({ children }) {
|
case 'ADD_TASK':
|
||||||
const [name,setName] = useState('');
|
return [...state, action.payload]
|
||||||
|
case 'TOGGLE_TASK':
|
||||||
const toggleName = (newName) => {
|
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
||||||
setName(newName);
|
case 'REMOVE_TASK':
|
||||||
|
return state.filter(task => task.id !== action.payload)
|
||||||
|
default:
|
||||||
|
return state
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
||||||
|
console.log("Rendred")
|
||||||
return (
|
return (
|
||||||
<UserContext.Provider value={{name, toggleName}}>
|
<ul>
|
||||||
{children}
|
{tasks.map(({id, text, complite}) => (
|
||||||
</UserContext.Provider>
|
<li key={id}
|
||||||
|
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 Form() {
|
|
||||||
const {name, toggleName} = useContext(UserContext)
|
|
||||||
const [newName,setNewName] = useState('');
|
|
||||||
const handleSubmit = (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
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 (
|
|
||||||
<button onClick={() => toggleName('')}>Выход</button>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Toolbar() {
|
|
||||||
const {name, toggleName} = useContext(UserContext)
|
|
||||||
return(
|
|
||||||
<div className={styles.container}>
|
|
||||||
<h3>Панель пользователя</h3>
|
|
||||||
{name && `Привет ${name}`}
|
|
||||||
{name ? <ExitButton /> : <Form /> }
|
|
||||||
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function Page() {
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h1 style={{textAlign: 'center'}}>Главная страница</h1>
|
|
||||||
<Toolbar />
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function App() {
|
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({
|
||||||
|
type: 'ADD_TASK',
|
||||||
|
payload: {id: Date.now(), text: input.trim(), complite: false }
|
||||||
|
})
|
||||||
|
setInput('')
|
||||||
|
inputRef.current.focus()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div style={{padding: 20}}>
|
||||||
{/* Добавьте тестовый контент для проверки */}
|
<h1>Список дел</h1>
|
||||||
<h1>Hello, React!</h1>
|
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
||||||
<UserProvider>
|
<button onClick={addTask}>Добавить</button>
|
||||||
<Page />
|
{error && <p style={{color: 'red'}}>{error}</p>}
|
||||||
</UserProvider>
|
|
||||||
</>
|
<TaskList tasks={tasks}
|
||||||
|
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
||||||
|
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
.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