Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 82fb0d8030 | |||
| cc79d1956b | |||
| 55c4ae7d53 |
Generated
+33
-3
@@ -10,7 +10,8 @@
|
||||
"dependencies": {
|
||||
"immer": "^11.1.8",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0"
|
||||
"react-dom": "^19.1.0",
|
||||
"zustand": "^5.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.25.0",
|
||||
@@ -1368,7 +1369,7 @@
|
||||
"version": "19.1.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.1.6.tgz",
|
||||
"integrity": "sha512-JeG0rEWak0N6Itr6QUx+X60uQmN+5t3j9r/OVDtWzFXKaj6kD1BwJzOksD0FF6iWxZlbE1kB0q9vtnU2ekqa1Q==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"csstype": "^3.0.2"
|
||||
@@ -1620,7 +1621,7 @@
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
|
||||
"integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/debug": {
|
||||
@@ -2805,6 +2806,35 @@
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/zustand": {
|
||||
"version": "5.0.14",
|
||||
"resolved": "https://registry.npmjs.org/zustand/-/zustand-5.0.14.tgz",
|
||||
"integrity": "sha512-/8tAspM5LMPr28b3fwLYrtdj77ECpfZviaP75CMTnwO8ISyaE4GDIG/9rDDYq/cH9D2Xw2A2RXglLInmVBQB/g==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=12.20.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@types/react": ">=18.0.0",
|
||||
"immer": ">=9.0.6",
|
||||
"react": ">=18.0.0",
|
||||
"use-sync-external-store": ">=1.2.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
},
|
||||
"immer": {
|
||||
"optional": true
|
||||
},
|
||||
"react": {
|
||||
"optional": true
|
||||
},
|
||||
"use-sync-external-store": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -12,7 +12,8 @@
|
||||
"dependencies": {
|
||||
"immer": "^11.1.8",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0"
|
||||
"react-dom": "^19.1.0",
|
||||
"zustand": "^5.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.25.0",
|
||||
|
||||
+2
-41
@@ -1,49 +1,10 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import { produce } from 'immer'
|
||||
import Quiz from './components/Quiz'
|
||||
|
||||
function App() {
|
||||
const [tasks,setTasks] = useState([])
|
||||
const [input,setInput] = useState('')
|
||||
|
||||
const addTask = () => {
|
||||
if(!input.trim()) return
|
||||
|
||||
setTasks(currentTasks => produce(currentTasks, draft => {
|
||||
draft.push({
|
||||
id: Date.now(),
|
||||
text: input.trim(),
|
||||
done: false
|
||||
})
|
||||
}))
|
||||
setInput('')
|
||||
}
|
||||
const toggleTask = id => {
|
||||
setTasks(currentTask => produce(currentTask, draft => {
|
||||
const task = draft.find(t => t.id === id)
|
||||
if(task) {
|
||||
task.done = !task.done
|
||||
}
|
||||
}))
|
||||
}
|
||||
const removeTask = id => {
|
||||
setTasks(currentTask => produce(currentTask, draft => {
|
||||
const index = draft.findIndex(t => t.id === id)
|
||||
if (index !== -1)
|
||||
draft.splice(index,1)
|
||||
}))
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<h1>Практика по созданию списка задач</h1>
|
||||
<input value={input} onChange={e=>setInput(e.target.value)} />
|
||||
<button onClick={addTask}>Добавить</button>
|
||||
<ul>{tasks.map(({id,text,done}) => (
|
||||
<li key={id} style={{ textDecoration: done ? 'line-through' : 'none'}}>
|
||||
<input type="checkbox" checked={done} onChange={()=> toggleTask(id)} />
|
||||
{text}
|
||||
<button onClick={() => removeTask(id)}>Удалить</button>
|
||||
</li>
|
||||
))}</ul>
|
||||
<Quiz />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
export function MyComponent() {
|
||||
return (
|
||||
<>
|
||||
<h1>Hello</h1>
|
||||
</>
|
||||
)
|
||||
}
|
||||
export default MyComponent
|
||||
@@ -0,0 +1,31 @@
|
||||
import { useState } from 'react'
|
||||
|
||||
const Question = ({question, onSubmit}) => {
|
||||
const [selectedOption,setSelectedOption] = useState('')
|
||||
const handleSubmit = () => {
|
||||
console.log(selectedOption);
|
||||
if (selectedOption) onSubmit(selectedOption);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2>Вопрос {question.id}: {question.text}</h2>
|
||||
<div>
|
||||
{question.options.map((option, index)=>(
|
||||
<label key={index}>
|
||||
<input
|
||||
type="radio"
|
||||
name={`question-${question.id}`}
|
||||
value={option}
|
||||
checked={selectedOption === option}
|
||||
onChange={() => setSelectedOption(option)} />
|
||||
{option}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<button onClick={handleSubmit} >Следующий вопрос</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Question
|
||||
@@ -0,0 +1,35 @@
|
||||
import useQuizStore from './store/useQuizStore'
|
||||
import Question from './Question'
|
||||
import Result from './Result'
|
||||
|
||||
const Quiz = () => {
|
||||
const {
|
||||
questions,
|
||||
currentQuestion,
|
||||
userAswers,
|
||||
isFinished,
|
||||
submitAnswer,
|
||||
goToNextQuestion,
|
||||
restartQuiz,
|
||||
} = useQuizStore();
|
||||
|
||||
const handleAnswerSubmit = (anwser) => {
|
||||
console.log('Yes')
|
||||
submitAnswer(answer)
|
||||
if (currentQuestion < questions.length - 1 ) {
|
||||
goToNextQuestion()
|
||||
}
|
||||
}
|
||||
if(isFinished) {
|
||||
return (<Result />)
|
||||
}
|
||||
return (
|
||||
<div className="quiz">
|
||||
<Question
|
||||
question={questions[currentQuestion]}
|
||||
onSubmit={handleAnswerSubmit} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Quiz
|
||||
@@ -0,0 +1,9 @@
|
||||
const Result = ({questions, userAswers, onRestart}) => {
|
||||
return (
|
||||
<div>
|
||||
<h2>Результат</h2>
|
||||
<button onClick={onRestart}>Начать заново</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default Result
|
||||
@@ -0,0 +1,36 @@
|
||||
import { create } from 'zustand'
|
||||
|
||||
const useQuizStore = create(set => ({
|
||||
questions: [
|
||||
{
|
||||
id:1,
|
||||
text: 'Сколько будет 2+2?',
|
||||
options: ['3','4','2'],
|
||||
correctAnswer: '4',
|
||||
},
|
||||
{
|
||||
id:2,
|
||||
text: 'Столица России?',
|
||||
options: ['Москва','Париж','Лондон'],
|
||||
correctAnswer: 'Москва',
|
||||
},
|
||||
],
|
||||
currentQuestion: 0,
|
||||
userAnswer:[],
|
||||
isFinished: false,
|
||||
goToNextQuestion: () => set(state => ({
|
||||
currentQuestion: state.currentQuestion + 1,
|
||||
})),
|
||||
submitAnswer: answer => set(state => {
|
||||
const updatedAnswer = [...state.userAnswer, answer];
|
||||
const isFinished = (state.currentQuestion >= state.questions.length - 1);
|
||||
return {userAnswer: updatedAnswer, isFinished};
|
||||
}),/*
|
||||
restartQuiz: () => set({
|
||||
currentQuestion: 0,
|
||||
userAnswer: [],
|
||||
isFinished: false
|
||||
}), */
|
||||
}))
|
||||
|
||||
export default useQuizStore
|
||||
Reference in New Issue
Block a user