Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 56788f23d6 | |||
| ac00619fa6 | |||
| 93b5b8dd85 |
@@ -0,0 +1,18 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2026 laktionov-as
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||||
|
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
||||||
|
following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||||
|
portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||||
|
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||||
|
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
+9
-13
@@ -2,11 +2,17 @@ import js from '@eslint/js'
|
|||||||
import globals from 'globals'
|
import globals from 'globals'
|
||||||
import reactHooks from 'eslint-plugin-react-hooks'
|
import reactHooks from 'eslint-plugin-react-hooks'
|
||||||
import reactRefresh from 'eslint-plugin-react-refresh'
|
import reactRefresh from 'eslint-plugin-react-refresh'
|
||||||
|
import { defineConfig, globalIgnores } from 'eslint/config'
|
||||||
|
|
||||||
export default [
|
export default defineConfig([
|
||||||
{ ignores: ['dist'] },
|
globalIgnores(['dist']),
|
||||||
{
|
{
|
||||||
files: ['**/*.{js,jsx}'],
|
files: ['**/*.{js,jsx}'],
|
||||||
|
extends: [
|
||||||
|
js.configs.recommended,
|
||||||
|
reactHooks.configs['recommended-latest'],
|
||||||
|
reactRefresh.configs.vite,
|
||||||
|
],
|
||||||
languageOptions: {
|
languageOptions: {
|
||||||
ecmaVersion: 2020,
|
ecmaVersion: 2020,
|
||||||
globals: globals.browser,
|
globals: globals.browser,
|
||||||
@@ -16,18 +22,8 @@ export default [
|
|||||||
sourceType: 'module',
|
sourceType: 'module',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins: {
|
|
||||||
'react-hooks': reactHooks,
|
|
||||||
'react-refresh': reactRefresh,
|
|
||||||
},
|
|
||||||
rules: {
|
rules: {
|
||||||
...js.configs.recommended.rules,
|
|
||||||
...reactHooks.configs.recommended.rules,
|
|
||||||
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
|
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
|
||||||
'react-refresh/only-export-components': [
|
|
||||||
'warn',
|
|
||||||
{ allowConstantExport: true },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
])
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
<!DOCTYPE html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
|
|||||||
Generated
+435
-425
File diff suppressed because it is too large
Load Diff
+10
-11
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "store-client",
|
"name": "router-middleware-practice",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
@@ -10,20 +10,19 @@
|
|||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"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"
|
"react-router-dom": "^7.7.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.25.0",
|
"@eslint/js": "^9.30.1",
|
||||||
"@types/react": "^19.1.2",
|
"@types/react": "^19.1.8",
|
||||||
"@types/react-dom": "^19.1.2",
|
"@types/react-dom": "^19.1.6",
|
||||||
"@vitejs/plugin-react": "^4.4.1",
|
"@vitejs/plugin-react": "^4.6.0",
|
||||||
"eslint": "^9.25.0",
|
"eslint": "^9.30.1",
|
||||||
"eslint-plugin-react-hooks": "^5.2.0",
|
"eslint-plugin-react-hooks": "^5.2.0",
|
||||||
"eslint-plugin-react-refresh": "^0.4.19",
|
"eslint-plugin-react-refresh": "^0.4.20",
|
||||||
"globals": "^16.0.0",
|
"globals": "^16.3.0",
|
||||||
"vite": "^6.3.5"
|
"vite": "^7.0.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+26
-3
@@ -1,10 +1,33 @@
|
|||||||
import Quiz from './components/Quiz'
|
import { useState } from 'react'
|
||||||
|
import reactLogo from './assets/react.svg'
|
||||||
|
import viteLogo from '/vite.svg'
|
||||||
|
import './App.css'
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [count, setCount] = useState(0)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1>Практика по созданию списка задач</h1>
|
<div>
|
||||||
<Quiz />
|
<a href="https://vite.dev" target="_blank">
|
||||||
|
<img src={viteLogo} className="logo" alt="Vite logo" />
|
||||||
|
</a>
|
||||||
|
<a href="https://react.dev" target="_blank">
|
||||||
|
<img src={reactLogo} className="logo react" alt="React logo" />
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<h1>Vite + React</h1>
|
||||||
|
<div className="card">
|
||||||
|
<button onClick={() => setCount((count) => count + 1)}>
|
||||||
|
count is {count}
|
||||||
|
</button>
|
||||||
|
<p>
|
||||||
|
Edit <code>src/App.jsx</code> and save to test HMR
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<p className="read-the-docs">
|
||||||
|
Click on the Vite and React logos to learn more
|
||||||
|
</p>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
import { redirect } from 'react-router-dom'
|
||||||
|
|
||||||
|
// Проверка авторизации
|
||||||
|
export function requireAuth() {
|
||||||
|
const isAuthenticated = localStorage.getItem('auth') === 'true'
|
||||||
|
if (!isAuthenticated) {
|
||||||
|
return redirect('/login')
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка авторизации и роли
|
||||||
|
export function requireAdmin() {
|
||||||
|
const isAuthenticated = localStorage.getItem('auth') === 'true'
|
||||||
|
const role = localStorage.getItem('role')
|
||||||
|
|
||||||
|
if (!isAuthenticated) {
|
||||||
|
return redirect('/login')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (role !== 'admin') {
|
||||||
|
return redirect('/dashboard') // если авторизован, но не админ — на общую панель
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
const Result = ({questions, userAswers, onRestart}) => {
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<h2>Результат</h2>
|
|
||||||
<button onClick={onRestart}>Начать заново</button>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
export default Result
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
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
|
|
||||||
+8
-6
@@ -1,8 +1,10 @@
|
|||||||
import { StrictMode } from 'react'
|
import React from 'react'
|
||||||
import { createRoot } from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
// удаляем import './index.css'
|
import { RouterProvider } from 'react-router-dom'
|
||||||
import App from './App.jsx'
|
import router from './routes'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')).render(
|
ReactDOM.createRoot(document.getElementById('root')).render(
|
||||||
<App />
|
<React.StrictMode>
|
||||||
|
<RouterProvider router={router} />
|
||||||
|
</React.StrictMode>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default function Admin() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Админ-панель</h1>
|
||||||
|
<p>Доступ разрешён только администраторам.</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export default function Dashboard() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Панель пользователя</h1>
|
||||||
|
<p>Вы вошли как обычный пользователь или администратор.</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import React, { useState } from 'react'
|
||||||
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
|
||||||
|
export default function Login() {
|
||||||
|
const [role, setRole] = useState('user')
|
||||||
|
const navigate = useNavigate()
|
||||||
|
|
||||||
|
const handleLogin = () => {
|
||||||
|
localStorage.setItem('auth', 'true')
|
||||||
|
localStorage.setItem('role', role)
|
||||||
|
navigate('/dashboard')
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Вход</h1>
|
||||||
|
<label>
|
||||||
|
Роль:
|
||||||
|
<select value={role} onChange={e => setRole(e.target.value)}>
|
||||||
|
<option value='user'>Обычный пользователь</option>
|
||||||
|
<option value='admin'>Администратор</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<br />
|
||||||
|
<button onClick={handleLogin}>Войти</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { createBrowserRouter } from 'react-router-dom'
|
||||||
|
import Login from './pages/Login'
|
||||||
|
import Dashboard from './pages/Dashboard'
|
||||||
|
import Admin from './pages/Admin'
|
||||||
|
import { requireAuth, requireAdmin } from './auth'
|
||||||
|
|
||||||
|
const router = createBrowserRouter([
|
||||||
|
{
|
||||||
|
path: '/login',
|
||||||
|
element: <Login />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/dashboard',
|
||||||
|
element: <Dashboard />,
|
||||||
|
loader: requireAuth,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/admin',
|
||||||
|
element: <Admin />,
|
||||||
|
loader: requireAdmin,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
export default router
|
||||||
Reference in New Issue
Block a user