Compare commits

..

4 Commits

8 changed files with 161 additions and 120 deletions
+44 -30
View File
@@ -8,9 +8,10 @@
"name": "store-client", "name": "store-client",
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"@tanstack/react-query": "^5.100.14", "immer": "^11.1.8",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0" "react-dom": "^19.1.0",
"zustand": "^5.0.14"
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.25.0", "@eslint/js": "^9.25.0",
@@ -1305,32 +1306,6 @@
"win32" "win32"
] ]
}, },
"node_modules/@tanstack/query-core": {
"version": "5.100.14",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.100.14.tgz",
"integrity": "sha512-5X41dGpxgeaHISCRW2oYwcSycZeULZzAunaudXT9ov1KOTj9xwt0CH6hbwqP1/z74ZWF7rYFnDpyYH07XFcZew==",
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.100.14",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.100.14.tgz",
"integrity": "sha512-oOr6aRdSFEwWhzxEkD/9ZcItM3+LjBSkeVmadWKwUssAHTsqd/7bOjWrX4AbvEkoEhgAxzN0Xk6H/aYzXiYBAw==",
"license": "MIT",
"dependencies": {
"@tanstack/query-core": "5.100.14"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"react": "^18 || ^19"
}
},
"node_modules/@types/babel__core": { "node_modules/@types/babel__core": {
"version": "7.20.5", "version": "7.20.5",
"resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz",
@@ -1394,7 +1369,7 @@
"version": "19.1.6", "version": "19.1.6",
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.1.6.tgz", "resolved": "https://registry.npmjs.org/@types/react/-/react-19.1.6.tgz",
"integrity": "sha512-JeG0rEWak0N6Itr6QUx+X60uQmN+5t3j9r/OVDtWzFXKaj6kD1BwJzOksD0FF6iWxZlbE1kB0q9vtnU2ekqa1Q==", "integrity": "sha512-JeG0rEWak0N6Itr6QUx+X60uQmN+5t3j9r/OVDtWzFXKaj6kD1BwJzOksD0FF6iWxZlbE1kB0q9vtnU2ekqa1Q==",
"dev": true, "devOptional": true,
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"csstype": "^3.0.2" "csstype": "^3.0.2"
@@ -1646,7 +1621,7 @@
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
"integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==", "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==",
"dev": true, "devOptional": true,
"license": "MIT" "license": "MIT"
}, },
"node_modules/debug": { "node_modules/debug": {
@@ -2081,6 +2056,16 @@
"node": ">= 4" "node": ">= 4"
} }
}, },
"node_modules/immer": {
"version": "11.1.8",
"resolved": "https://registry.npmjs.org/immer/-/immer-11.1.8.tgz",
"integrity": "sha512-/tbkHMW7y10Lx6i1crLjD4/OhNkRG+Fo7byZHtah0547nIeXYcpIXaUh0IAQY6gO5459qpGGYapcEOHtFXkIuA==",
"license": "MIT",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/immer"
}
},
"node_modules/import-fresh": { "node_modules/import-fresh": {
"version": "3.3.1", "version": "3.3.1",
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
@@ -2821,6 +2806,35 @@
"funding": { "funding": {
"url": "https://github.com/sponsors/sindresorhus" "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
}
}
} }
} }
} }
+3 -2
View File
@@ -10,9 +10,10 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"@tanstack/react-query": "^5.100.14", "immer": "^11.1.8",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0" "react-dom": "^19.1.0",
"zustand": "^5.0.14"
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.25.0", "@eslint/js": "^9.25.0",
+3 -86
View File
@@ -1,93 +1,10 @@
import React, {useState, useEffect, useRef } from 'react' import Quiz from './components/Quiz'
//import { useQuery } from '@tanstack/react-query'
function useWindowWidth() {
const [width,setWidth] = useState(window.innerWidth)
useEffect(() => {
const handleResize = () => setWidth(window.innerWidth);
window.addEventListener('resize',handleResize);
return () => window.removeEventListener('resize',handleResize);
},[])
return width
}
function useLocalStorage(key, initValue) {
const [value, setValue] = useState(()=>{
const stored = localStorage.getItem(key);
return stored !== null ? JSON.parse(stored) : initValue
})
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value))
},[key, value])
return [value,setValue]
}
/*
function useUserData(userId) {
return useQuery(
['user',userId],
() => fetch(`/api/user/${userId}`).then(res => res.json()),
{refetchOnWindowFocus: false}
)
}*/
function useInput(initValue, validate) {
const [value, setValue] = useState(initValue);
const [error, setError] = useState(null);
const onChange = e => {
const newValue = e.target.value
setValue(newValue)
if (validate) {
const validationResult = validate(newValue)
setError(validationResult)
}
}
return { value, error, onChange }
}
function useInterval(callback, delay) {
const savedCallback = useRef()
useEffect(()=>{
savedCallback.current = callback
},[callback]);
useEffect(()=>{
if (delay == null) return
const id = setInterval(()=>{
savedCallback.current()
},delay);
return () => clearInterval(id);
},[delay])
}
function App() { function App() {
const width = useWindowWidth();
const [theme, setTheme] = useLocalStorage('theme','light');
const username = useInput(
'',
value => value.length < 3 ? 'минимум 3 символа' : null
)
return ( return (
<> <>
<h1>Hello, React!{width}</h1> <h1>Практика по созданию списка задач</h1>
<button onClick={()=>{ <Quiz />
setTheme(theme === 'light' ? 'dark' : 'light')
}}>
Сменить тему
</button>
<p><input type="text"
value={username.value}
onChange={username.onChange} />
{ username.error ? username.error : '' }
</p>
</> </>
) )
} }
+31
View File
@@ -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
+35
View File
@@ -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
+9
View File
@@ -0,0 +1,9 @@
const Result = ({questions, userAswers, onRestart}) => {
return (
<div>
<h2>Результат</h2>
<button onClick={onRestart}>Начать заново</button>
</div>
)
}
export default Result
+36
View File
@@ -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
-2
View File
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
import App from './App.jsx' import App from './App.jsx'
createRoot(document.getElementById('root')).render( createRoot(document.getElementById('root')).render(
<StrictMode>
<App /> <App />
</StrictMode>
) )