Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43aeb40b20 |
+35
-57
@@ -1,67 +1,45 @@
|
|||||||
import React, { useState, useReducer, useRef, memo } from 'react'
|
import { useMutation, QueryClientProvider, QueryClient } from '@tanstack/react-query'
|
||||||
|
import PostAddForm from './components/PostAddForm'
|
||||||
|
|
||||||
function tasksReducer(state, action) {
|
/*
|
||||||
switch(action.type) {
|
const mutation = useMutation({
|
||||||
case 'ADD_TASK':
|
mutationFn: newUser => {
|
||||||
return [...state, action.payload]
|
fetch('https://jsonplaceholder.typicode.com/users', {
|
||||||
case 'TOGGLE_TASK':
|
method: 'GET',
|
||||||
return state.map(task => task.id === action.payload ? {...task, complite: !task.complite} : task)
|
headers: {'Content-type':'application/json'},
|
||||||
case 'REMOVE_TASK':
|
body: JSON.stringify(newUser),
|
||||||
return state.filter(task => task.id !== action.payload)
|
}).then(res => res.json())
|
||||||
default:
|
|
||||||
return state
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
|
||||||
|
const createQueryClient = () => {
|
||||||
|
new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
staleTime: 5 * 60*1000,
|
||||||
|
retry: 1,
|
||||||
|
gcTime: 10*60*1000,
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
retry:0,
|
||||||
|
onError: error => {console.error('Mutation error:',error.message)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const TaskList = memo(function TaskList({tasks, onToggle, onRemove}) {
|
|
||||||
console.log("Rendred")
|
|
||||||
return (
|
|
||||||
<ul>
|
|
||||||
{tasks.map(({id, text, complite}) => (
|
|
||||||
<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 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>
|
{/* Добавьте тестовый контент для проверки */}
|
||||||
<input ref={inputRef} value={input} onChange={e => setInput(e.target.value)} />
|
<h1>Hello, React!</h1>
|
||||||
<button onClick={addTask}>Добавить</button>
|
<QueryClientProvider client={createQueryClient}>
|
||||||
{error && <p style={{color: 'red'}}>{error}</p>}
|
<PostAddForm />
|
||||||
|
</QueryClientProvider>
|
||||||
|
|
||||||
<TaskList tasks={tasks}
|
</>
|
||||||
onToggle={id => dispatch({type: 'TOGGLE_TASK', payload: id})}
|
|
||||||
onRemove={id => dispatch({type: 'REMOVE_TASK', payload: id})} />
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user