From fed9b216afc59e35bd634a646e8a1238b5867b64 Mon Sep 17 00:00:00 2001 From: olejech Date: Sun, 7 Feb 2021 18:30:38 +0300 Subject: [PATCH 1/3] Fix eslint warnings --- .gitignore | 3 ++ src/components/TodoList.tsx | 73 ++++++++++++++++++++----------------- src/components/UserList.tsx | 47 ++++++++++++------------ 3 files changed, 66 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 4d29575..9f202a6 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +#IDE +.idea \ No newline at end of file diff --git a/src/components/TodoList.tsx b/src/components/TodoList.tsx index e95e088..ca84b84 100644 --- a/src/components/TodoList.tsx +++ b/src/components/TodoList.tsx @@ -1,40 +1,47 @@ -import React, {useEffect} from 'react'; -import {useTypedSelector} from "../hooks/useTypedSelector"; -import {useActions} from "../hooks/useActions"; +import React, { useEffect } from 'react' +import { useTypedSelector } from '../hooks/useTypedSelector' +import { useActions } from '../hooks/useActions' const TodoList: React.FC = () => { - const {page, error, loading, todos, limit} = useTypedSelector(state => state.todo) - const {fetchTodos, setTodoPage} = useActions() - const pages = [1, 2, 3, 4, 5] + const { page, error, loading, todos, limit } = useTypedSelector( + state => state.todo + ) + const { fetchTodos, setTodoPage } = useActions() + const pages = [1, 2, 3, 4, 5] - useEffect(() => { - fetchTodos(page, limit) - }, [page]) + useEffect(() => { + fetchTodos(page, limit) + }, [fetchTodos, limit, page]) - if (loading) { - return

Идет загрузка...

- } - if (error) { - return

{error}

- } + if (loading) { + return

Идет загрузка...

+ } + if (error) { + return

{error}

+ } - return ( -
- {todos.map(todo => -
{todo.id} - {todo.title}
- )} -
- {pages.map(p => -
setTodoPage(p)} - style={{border:p === page ? '2px solid green' : '1px solid gray', padding: 10}} - > - {p} -
- )} -
+ return ( +
+ {todos.map(todo => ( +
+ {todo.id} - {todo.title}
- ); -}; + ))} +
+ {pages.map(p => ( +
setTodoPage(p)} + style={{ + border: p === page ? '2px solid green' : '1px solid gray', + padding: 10, + }} + > + {p} +
+ ))} +
+
+ ) +} -export default TodoList; +export default TodoList diff --git a/src/components/UserList.tsx b/src/components/UserList.tsx index 7e8cdd6..b1024a1 100644 --- a/src/components/UserList.tsx +++ b/src/components/UserList.tsx @@ -1,30 +1,29 @@ -import React, {useEffect} from 'react'; -import {useTypedSelector} from "../hooks/useTypedSelector"; -import {fetchUsers} from "../store/action-creators/user"; -import {useActions} from "../hooks/useActions"; +import React, { useEffect } from 'react' +import { useTypedSelector } from '../hooks/useTypedSelector' +import { useActions } from '../hooks/useActions' const UserList: React.FC = () => { - const {users, error, loading} = useTypedSelector(state => state.user) - const {fetchUsers} = useActions() + const { users, error, loading } = useTypedSelector(state => state.user) + const { fetchUsers } = useActions() - useEffect(() => { - fetchUsers() - }, []) + useEffect(() => { + fetchUsers() + }, [fetchUsers]) - if (loading) { - return

Идет загрузка...

- } - if (error) { - return

{error}

- } + if (loading) { + return

Идет загрузка...

+ } + if (error) { + return

{error}

+ } - return ( -
- {users.map(user => -
{user.name}
- )} -
- ); -}; + return ( +
+ {users.map(user => ( +
{user.name}
+ ))} +
+ ) +} -export default UserList; +export default UserList From ea7120ecb4021e76d3e26fa98f38042c7ec9c538 Mon Sep 17 00:00:00 2001 From: utimur Date: Mon, 1 Feb 2021 16:56:17 +0300 Subject: [PATCH 2/3] init commit --- .gitignore | 3 ++ README.md | 47 +-------------------- package-lock.json | 67 ++++++++++++++++++++++++++++++ package.json | 5 +++ src/App.css | 38 ----------------- src/App.test.tsx | 9 ---- src/App.tsx | 33 +++++---------- src/components/TodoList.tsx | 40 ++++++++++++++++++ src/components/UserList.tsx | 29 +++++++++++++ src/hooks/useActions.ts | 8 ++++ src/hooks/useTypedSelector.ts | 5 +++ src/index.css | 13 ------ src/index.tsx | 14 +++---- src/logo.svg | 1 - src/react-app-env.d.ts | 1 - src/reportWebVitals.ts | 15 ------- src/setupTests.ts | 5 --- src/store/action-creators/index.ts | 7 ++++ src/store/action-creators/todo.ts | 25 +++++++++++ src/store/action-creators/user.ts | 20 +++++++++ src/store/index.ts | 6 +++ src/store/reducers/index.ts | 11 +++++ src/store/reducers/todoReducer.ts | 24 +++++++++++ src/store/reducers/userReducer.ts | 20 +++++++++ src/types/todo.ts | 35 ++++++++++++++++ src/types/user.ts | 22 ++++++++++ 26 files changed, 345 insertions(+), 158 deletions(-) delete mode 100644 src/App.css delete mode 100644 src/App.test.tsx create mode 100644 src/components/TodoList.tsx create mode 100644 src/components/UserList.tsx create mode 100644 src/hooks/useActions.ts create mode 100644 src/hooks/useTypedSelector.ts delete mode 100644 src/index.css delete mode 100644 src/logo.svg delete mode 100644 src/react-app-env.d.ts delete mode 100644 src/reportWebVitals.ts delete mode 100644 src/setupTests.ts create mode 100644 src/store/action-creators/index.ts create mode 100644 src/store/action-creators/todo.ts create mode 100644 src/store/action-creators/user.ts create mode 100644 src/store/index.ts create mode 100644 src/store/reducers/index.ts create mode 100644 src/store/reducers/todoReducer.ts create mode 100644 src/store/reducers/userReducer.ts create mode 100644 src/types/todo.ts create mode 100644 src/types/user.ts diff --git a/.gitignore b/.gitignore index 4d29575..9f202a6 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +#IDE +.idea \ No newline at end of file diff --git a/README.md b/README.md index b87cb00..236cbdf 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,3 @@ -# Getting Started with Create React App +## Если воспользовался репозиторием, дай обратную связь - поставь звезду -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). - -## Available Scripts - -In the project directory, you can run: - -### `npm start` - -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.\ -You will also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. - -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). +### npm start - запуск diff --git a/package-lock.json b/package-lock.json index 35f4a96..fe9c1c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2210,6 +2210,15 @@ "@types/node": "*" } }, + "@types/hoist-non-react-statics": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz", + "integrity": "sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==", + "requires": { + "@types/react": "*", + "hoist-non-react-statics": "^3.3.0" + } + }, "@types/html-minifier-terser": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz", @@ -2307,6 +2316,17 @@ "@types/react": "^16" } }, + "@types/react-redux": { + "version": "7.1.16", + "resolved": "https://registry.npmjs.org/@types/react-redux/-/react-redux-7.1.16.tgz", + "integrity": "sha512-f/FKzIrZwZk7YEO9E1yoxIuDNRiDducxkFlkw/GNMGEnK9n4K8wJzlJBghpSuOVDgEUHoDkDF7Gi9lHNQR4siw==", + "requires": { + "@types/hoist-non-react-statics": "^3.3.0", + "@types/react": "*", + "hoist-non-react-statics": "^3.3.0", + "redux": "^4.0.0" + } + }, "@types/resolve": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz", @@ -3018,6 +3038,14 @@ "resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.1.1.tgz", "integrity": "sha512-5Kgy8Cz6LPC9DJcNb3yjAXTu3XihQgEdnIg50c//zOC/MyLP0Clg+Y8Sh9ZjjnvBrDZU4DgXS9C3T9r4/scGZQ==" }, + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, "axobject-query": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz", @@ -7021,6 +7049,14 @@ "minimalistic-crypto-utils": "^1.0.1" } }, + "hoist-non-react-statics": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz", + "integrity": "sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==", + "requires": { + "react-is": "^16.7.0" + } + }, "hoopy": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/hoopy/-/hoopy-0.1.4.tgz", @@ -12285,6 +12321,18 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, + "react-redux": { + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/react-redux/-/react-redux-7.2.2.tgz", + "integrity": "sha512-8+CQ1EvIVFkYL/vu6Olo7JFLWop1qRUeb46sGtIMDCSpgwPQq8fPLpirIB0iTqFe9XYEFPHssdX8/UwN6pAkEA==", + "requires": { + "@babel/runtime": "^7.12.1", + "hoist-non-react-statics": "^3.3.2", + "loose-envify": "^1.4.0", + "prop-types": "^15.7.2", + "react-is": "^16.13.1" + } + }, "react-refresh": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.8.3.tgz", @@ -12471,6 +12519,20 @@ "strip-indent": "^3.0.0" } }, + "redux": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/redux/-/redux-4.0.5.tgz", + "integrity": "sha512-VSz1uMAH24DM6MF72vcojpYPtrTUu3ByVWfPL1nPfVRb5mZVTve5GnNCUV53QM/BZ66xfWrm0CTWoM+Xlz8V1w==", + "requires": { + "loose-envify": "^1.4.0", + "symbol-observable": "^1.2.0" + } + }, + "redux-thunk": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/redux-thunk/-/redux-thunk-2.3.0.tgz", + "integrity": "sha512-km6dclyFnmcvxhAcrQV2AkZmPQjzPDjgVlQtR0EQjxZPyJ0BnMf3in1ryuR8A2qU0HldVRfxYXbFSKlI3N7Slw==" + }, "regenerate": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.2.tgz", @@ -14123,6 +14185,11 @@ "util.promisify": "~1.0.0" } }, + "symbol-observable": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.2.0.tgz", + "integrity": "sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==" + }, "symbol-tree": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz", diff --git a/package.json b/package.json index 2a305c5..cb169b9 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,14 @@ "@types/node": "^12.19.15", "@types/react": "^16.14.2", "@types/react-dom": "^16.9.10", + "@types/react-redux": "^7.1.16", + "axios": "^0.21.1", "react": "^17.0.1", "react-dom": "^17.0.1", + "react-redux": "^7.2.2", "react-scripts": "4.0.1", + "redux": "^4.0.5", + "redux-thunk": "^2.3.0", "typescript": "^4.1.3", "web-vitals": "^0.2.4" }, diff --git a/src/App.css b/src/App.css deleted file mode 100644 index 74b5e05..0000000 --- a/src/App.css +++ /dev/null @@ -1,38 +0,0 @@ -.App { - text-align: center; -} - -.App-logo { - height: 40vmin; - pointer-events: none; -} - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #61dafb; -} - -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} diff --git a/src/App.test.tsx b/src/App.test.tsx deleted file mode 100644 index 2a68616..0000000 --- a/src/App.test.tsx +++ /dev/null @@ -1,9 +0,0 @@ -import React from 'react'; -import { render, screen } from '@testing-library/react'; -import App from './App'; - -test('renders learn react link', () => { - render(); - const linkElement = screen.getByText(/learn react/i); - expect(linkElement).toBeInTheDocument(); -}); diff --git a/src/App.tsx b/src/App.tsx index a53698a..e32737d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,26 +1,15 @@ import React from 'react'; -import logo from './logo.svg'; -import './App.css'; +import UserList from "./components/UserList"; +import TodoList from "./components/TodoList"; -function App() { - return ( -
-
- logo -

- Edit src/App.tsx and save to reload. -

- - Learn React - -
-
- ); -} +const App = () => { + return ( +
+ +
+ +
+ ); +}; export default App; diff --git a/src/components/TodoList.tsx b/src/components/TodoList.tsx new file mode 100644 index 0000000..4fd9f8a --- /dev/null +++ b/src/components/TodoList.tsx @@ -0,0 +1,40 @@ +import React, {useEffect} from 'react'; +import {useTypedSelector} from "../hooks/useTypedSelector"; +import {useActions} from "../hooks/useActions"; + +const TodoList: React.FC = () => { + const {page, error, loading, todos, limit} = useTypedSelector(state => state.todo) + const {fetchTodos, setTodoPage} = useActions() + const pages = [1, 2, 3, 4, 5] + + useEffect(() => { + fetchTodos(page, limit) + }, [fetchTodos, limit, page]) + + if (loading) { + return

Идет загрузка...

+ } + if (error) { + return

{error}

+ } + + return ( +
+ {todos.map(todo => +
{todo.id} - {todo.title}
+ )} +
+ {pages.map(p => +
setTodoPage(p)} + style={{border:p === page ? '2px solid green' : '1px solid gray', padding: 10}} + > + {p} +
+ )} +
+
+ ); +}; + +export default TodoList; diff --git a/src/components/UserList.tsx b/src/components/UserList.tsx new file mode 100644 index 0000000..b6744a2 --- /dev/null +++ b/src/components/UserList.tsx @@ -0,0 +1,29 @@ +import React, {useEffect} from 'react'; +import {useTypedSelector} from "../hooks/useTypedSelector"; +import {useActions} from "../hooks/useActions"; + +const UserList: React.FC = () => { + const {users, error, loading} = useTypedSelector(state => state.user) + const {fetchUsers} = useActions() + + useEffect(() => { + fetchUsers() + }, [fetchUsers]) + + if (loading) { + return

Идет загрузка...

+ } + if (error) { + return

{error}

+ } + + return ( +
+ {users.map(user => +
{user.name}
+ )} +
+ ); +}; + +export default UserList; diff --git a/src/hooks/useActions.ts b/src/hooks/useActions.ts new file mode 100644 index 0000000..e6ba22a --- /dev/null +++ b/src/hooks/useActions.ts @@ -0,0 +1,8 @@ +import {useDispatch} from "react-redux"; +import {bindActionCreators} from "redux"; +import ActionCreators from '../store/action-creators/' + +export const useActions = () => { + const dispatch = useDispatch() + return bindActionCreators(ActionCreators, dispatch) +} diff --git a/src/hooks/useTypedSelector.ts b/src/hooks/useTypedSelector.ts new file mode 100644 index 0000000..9d9f748 --- /dev/null +++ b/src/hooks/useTypedSelector.ts @@ -0,0 +1,5 @@ +import {TypedUseSelectorHook, useSelector} from "react-redux"; +import {RootState} from "../store/reducers"; + + +export const useTypedSelector: TypedUseSelectorHook = useSelector diff --git a/src/index.css b/src/index.css deleted file mode 100644 index ec2585e..0000000 --- a/src/index.css +++ /dev/null @@ -1,13 +0,0 @@ -body { - margin: 0; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', - 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', - sans-serif; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; -} - -code { - font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', - monospace; -} diff --git a/src/index.tsx b/src/index.tsx index ef2edf8..ec04437 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,17 +1,13 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import './index.css'; import App from './App'; -import reportWebVitals from './reportWebVitals'; +import {Provider} from "react-redux"; +import {store} from "./store"; ReactDOM.render( - - - , + + + , document.getElementById('root') ); -// If you want to start measuring performance in your app, pass a function -// to log results (for example: reportWebVitals(console.log)) -// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals -reportWebVitals(); diff --git a/src/logo.svg b/src/logo.svg deleted file mode 100644 index 9dfc1c0..0000000 --- a/src/logo.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/src/react-app-env.d.ts b/src/react-app-env.d.ts deleted file mode 100644 index 6431bc5..0000000 --- a/src/react-app-env.d.ts +++ /dev/null @@ -1 +0,0 @@ -/// diff --git a/src/reportWebVitals.ts b/src/reportWebVitals.ts deleted file mode 100644 index 49a2a16..0000000 --- a/src/reportWebVitals.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { ReportHandler } from 'web-vitals'; - -const reportWebVitals = (onPerfEntry?: ReportHandler) => { - if (onPerfEntry && onPerfEntry instanceof Function) { - import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { - getCLS(onPerfEntry); - getFID(onPerfEntry); - getFCP(onPerfEntry); - getLCP(onPerfEntry); - getTTFB(onPerfEntry); - }); - } -}; - -export default reportWebVitals; diff --git a/src/setupTests.ts b/src/setupTests.ts deleted file mode 100644 index 8f2609b..0000000 --- a/src/setupTests.ts +++ /dev/null @@ -1,5 +0,0 @@ -// jest-dom adds custom jest matchers for asserting on DOM nodes. -// allows you to do things like: -// expect(element).toHaveTextContent(/react/i) -// learn more: https://github.com/testing-library/jest-dom -import '@testing-library/jest-dom'; diff --git a/src/store/action-creators/index.ts b/src/store/action-creators/index.ts new file mode 100644 index 0000000..14ad3b8 --- /dev/null +++ b/src/store/action-creators/index.ts @@ -0,0 +1,7 @@ +import * as UserActionCreators from './user' +import * as TodoActionCreators from './todo' + +export default { + ...TodoActionCreators, + ...UserActionCreators +} diff --git a/src/store/action-creators/todo.ts b/src/store/action-creators/todo.ts new file mode 100644 index 0000000..d8ad892 --- /dev/null +++ b/src/store/action-creators/todo.ts @@ -0,0 +1,25 @@ +import {Dispatch} from "redux"; +import axios from "axios"; +import {TodoAction, TodoActionTypes} from "../../types/todo"; + +export const fetchTodos = (page = 1, limit = 10) => { + return async (dispatch: Dispatch) => { + try { + dispatch({type: TodoActionTypes.FETCH_TODOS}) + const response = await axios.get('https://jsonplaceholder.typicode.com/todos', { + params: {_page: page, _limit: limit} + }) + setTimeout(() => { + dispatch({type: TodoActionTypes.FETCH_TODOS_SUCCESS, payload: response.data}) + }, 500) + } catch (e) { + dispatch({ + type: TodoActionTypes.FETCH_TODOS_ERROR, + payload: 'Произошла ошибка при загрузке списка дел' + }) + } + } +} +export function setTodoPage(page: number): TodoAction { + return {type: TodoActionTypes.SET_TODO_PAGE, payload: page} +} diff --git a/src/store/action-creators/user.ts b/src/store/action-creators/user.ts new file mode 100644 index 0000000..e5e3133 --- /dev/null +++ b/src/store/action-creators/user.ts @@ -0,0 +1,20 @@ +import {UserAction, UserActionTypes} from "../../types/user"; +import {Dispatch} from "redux"; +import axios from "axios"; + +export const fetchUsers = () => { + return async (dispatch: Dispatch) => { + try { + dispatch({type: UserActionTypes.FETCH_USERS}) + const response = await axios.get('https://jsonplaceholder.typicode.com/users') + setTimeout(() => { + dispatch({type: UserActionTypes.FETCH_USERS_SUCCESS, payload: response.data}) + }, 500) + } catch (e) { + dispatch({ + type: UserActionTypes.FETCH_USERS_ERROR, + payload: 'Произошла ошибка при загрузке пользователей' + }) + } + } +} diff --git a/src/store/index.ts b/src/store/index.ts new file mode 100644 index 0000000..a71a0a7 --- /dev/null +++ b/src/store/index.ts @@ -0,0 +1,6 @@ +import {applyMiddleware, createStore} from "redux"; +import thunk from "redux-thunk"; +import {rootReducer} from "./reducers"; + + +export const store = createStore(rootReducer, applyMiddleware(thunk)) diff --git a/src/store/reducers/index.ts b/src/store/reducers/index.ts new file mode 100644 index 0000000..02ec1f8 --- /dev/null +++ b/src/store/reducers/index.ts @@ -0,0 +1,11 @@ +import {combineReducers} from "redux"; +import {userReducer} from "./userReducer"; +import {todoReducer} from "./todoReducer"; + + +export const rootReducer = combineReducers({ + user: userReducer, + todo: todoReducer +}) + +export type RootState = ReturnType diff --git a/src/store/reducers/todoReducer.ts b/src/store/reducers/todoReducer.ts new file mode 100644 index 0000000..b3bfbea --- /dev/null +++ b/src/store/reducers/todoReducer.ts @@ -0,0 +1,24 @@ +import {TodoAction, TodoActionTypes, TodoState} from "../../types/todo"; + +const initialState: TodoState = { + todos: [], + page: 1, + error: null, + limit: 10, + loading: false +} + +export const todoReducer = (state = initialState, action: TodoAction): TodoState => { + switch (action.type) { + case TodoActionTypes.FETCH_TODOS: + return {...state, loading: true} + case TodoActionTypes.FETCH_TODOS_SUCCESS: + return {...state, loading: false, todos: action.payload} + case TodoActionTypes.FETCH_TODOS_ERROR: + return {...state, loading: false, error: action.payload} + case TodoActionTypes.SET_TODO_PAGE: + return {...state, page: action.payload} + default: + return state + } +} diff --git a/src/store/reducers/userReducer.ts b/src/store/reducers/userReducer.ts new file mode 100644 index 0000000..94d0913 --- /dev/null +++ b/src/store/reducers/userReducer.ts @@ -0,0 +1,20 @@ +import {UserAction, UserActionTypes, UserState} from "../../types/user"; + +const initialState: UserState = { + users: [], + loading: false, + error: null +} + +export const userReducer = (state = initialState, action: UserAction): UserState => { + switch (action.type) { + case UserActionTypes.FETCH_USERS: + return {loading: true, error: null, users: []} + case UserActionTypes.FETCH_USERS_SUCCESS: + return {loading: false, error: null, users: action.payload} + case UserActionTypes.FETCH_USERS_ERROR: + return {loading: false, error: action.payload, users: []} + default: + return state + } +} diff --git a/src/types/todo.ts b/src/types/todo.ts new file mode 100644 index 0000000..0b37573 --- /dev/null +++ b/src/types/todo.ts @@ -0,0 +1,35 @@ +export interface TodoState { + todos: any[]; + loading: boolean; + error: null | string; + page: number; + limit: number; +} + +export enum TodoActionTypes { + FETCH_TODOS= 'FETCH_TODOS', + FETCH_TODOS_SUCCESS= 'FETCH_TODOS_SUCCESS', + FETCH_TODOS_ERROR= 'FETCH_TODOS_ERROR', + SET_TODO_PAGE = 'SET_TODO_PAGE' +} +interface FetchTodoAction { + type: TodoActionTypes.FETCH_TODOS +} +interface FetchTodoSuccessAction { + type: TodoActionTypes.FETCH_TODOS_SUCCESS; + payload: any[]; +} +interface FetchTodoErrorAction { + type: TodoActionTypes.FETCH_TODOS_ERROR; + payload: string; +} +interface SetTodoPage { + type: TodoActionTypes.SET_TODO_PAGE; + payload: number; +} + +export type TodoAction = + FetchTodoAction + | FetchTodoErrorAction + | FetchTodoSuccessAction + | SetTodoPage diff --git a/src/types/user.ts b/src/types/user.ts new file mode 100644 index 0000000..8fe4aeb --- /dev/null +++ b/src/types/user.ts @@ -0,0 +1,22 @@ +export interface UserState { + users: any[]; + loading: boolean; + error: null | string; +} +export enum UserActionTypes { + FETCH_USERS = 'FETCH_USERS', + FETCH_USERS_SUCCESS = 'FETCH_USERS_SUCCESS', + FETCH_USERS_ERROR = 'FETCH_USERS_FETCH_USERS_ERROR', +} +interface FetchUsersAction { + type: UserActionTypes.FETCH_USERS; +} +interface FetchUsersSuccessAction { + type: UserActionTypes.FETCH_USERS_SUCCESS; + payload: any[] +} +interface FetchUsersErrorAction { + type: UserActionTypes.FETCH_USERS_ERROR; + payload: string; +} +export type UserAction = FetchUsersAction | FetchUsersErrorAction | FetchUsersSuccessAction From 4b6f9e2d07b2c1eb2c61daba6d1a06f0e1a0bec7 Mon Sep 17 00:00:00 2001 From: olejech Date: Sun, 7 Feb 2021 18:45:47 +0300 Subject: [PATCH 3/3] Remove extra files --- .eslintcache | 1 - src/react-app-env.d.ts | 1 - 2 files changed, 2 deletions(-) delete mode 100644 .eslintcache delete mode 100644 src/react-app-env.d.ts diff --git a/.eslintcache b/.eslintcache deleted file mode 100644 index a8b876e..0000000 --- a/.eslintcache +++ /dev/null @@ -1 +0,0 @@ -[{"/Users/user/code/js/learn/react-redux-typescript-course/src/index.tsx":"1","/Users/user/code/js/learn/react-redux-typescript-course/src/App.tsx":"2","/Users/user/code/js/learn/react-redux-typescript-course/src/store/index.ts":"3","/Users/user/code/js/learn/react-redux-typescript-course/src/components/UserList.tsx":"4","/Users/user/code/js/learn/react-redux-typescript-course/src/components/TodoList.tsx":"5","/Users/user/code/js/learn/react-redux-typescript-course/src/hooks/useActions.ts":"6","/Users/user/code/js/learn/react-redux-typescript-course/src/hooks/useTypedSelector.ts":"7","/Users/user/code/js/learn/react-redux-typescript-course/src/store/reducers/index.ts":"8","/Users/user/code/js/learn/react-redux-typescript-course/src/store/action-creators/index.ts":"9","/Users/user/code/js/learn/react-redux-typescript-course/src/store/reducers/todoReducer.ts":"10","/Users/user/code/js/learn/react-redux-typescript-course/src/store/reducers/userReducer.ts":"11","/Users/user/code/js/learn/react-redux-typescript-course/src/store/action-creators/user.ts":"12","/Users/user/code/js/learn/react-redux-typescript-course/src/store/action-creators/todo.ts":"13","/Users/user/code/js/learn/react-redux-typescript-course/src/types/user.ts":"14","/Users/user/code/js/learn/react-redux-typescript-course/src/types/todo.ts":"15"},{"size":273,"mtime":1612711287311,"results":"16","hashOfConfig":"17"},{"size":279,"mtime":1612711287310,"results":"18","hashOfConfig":"17"},{"size":197,"mtime":1612711287312,"results":"19","hashOfConfig":"17"},{"size":620,"mtime":1612711803774,"results":"20","hashOfConfig":"17"},{"size":1073,"mtime":1612711792531,"results":"21","hashOfConfig":"17"},{"size":266,"mtime":1612711287311,"results":"22","hashOfConfig":"17"},{"size":187,"mtime":1612711287311,"results":"23","hashOfConfig":"17"},{"size":276,"mtime":1612711287312,"results":"24","hashOfConfig":"17"},{"size":163,"mtime":1612711287312,"results":"25","hashOfConfig":"17"},{"size":772,"mtime":1612711287312,"results":"26","hashOfConfig":"17"},{"size":666,"mtime":1612711287312,"results":"27","hashOfConfig":"17"},{"size":764,"mtime":1612711287312,"results":"28","hashOfConfig":"17"},{"size":969,"mtime":1612711287312,"results":"29","hashOfConfig":"17"},{"size":636,"mtime":1612711287313,"results":"30","hashOfConfig":"17"},{"size":808,"mtime":1612711287313,"results":"31","hashOfConfig":"17"},{"filePath":"32","messages":"33","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"1wuu5m",{"filePath":"34","messages":"35","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"36","messages":"37","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"38","messages":"39","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"40","messages":"41","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"42","messages":"43","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"44","messages":"45","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"46","messages":"47","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"48","messages":"49","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"50","messages":"51","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"52","messages":"53","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"54","messages":"55","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"56","messages":"57","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"58","messages":"59","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"60","messages":"61","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/Users/user/code/js/learn/react-redux-typescript-course/src/index.tsx",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/App.tsx",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/index.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/components/UserList.tsx",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/components/TodoList.tsx",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/hooks/useActions.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/hooks/useTypedSelector.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/reducers/index.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/action-creators/index.ts",["62"],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/reducers/todoReducer.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/reducers/userReducer.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/action-creators/user.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/store/action-creators/todo.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/types/user.ts",[],"/Users/user/code/js/learn/react-redux-typescript-course/src/types/todo.ts",[],{"ruleId":"63","severity":1,"message":"64","line":4,"column":1,"nodeType":"65","endLine":7,"endColumn":2},"import/no-anonymous-default-export","Assign object to a variable before exporting as module default","ExportDefaultDeclaration"] \ No newline at end of file diff --git a/src/react-app-env.d.ts b/src/react-app-env.d.ts deleted file mode 100644 index 6431bc5..0000000 --- a/src/react-app-env.d.ts +++ /dev/null @@ -1 +0,0 @@ -///