├── .gitignore
├── .prettierignore
├── .prettierrc
├── .vscode
└── settings.json
├── README.md
├── jsconfig.json
├── package.json
├── public
├── favicon.ico
├── index.html
└── manifest.json
├── src
├── App.js
├── components
│ ├── AuthProvider.js
│ ├── Debug.js
│ ├── Footer.js
│ ├── Header.js
│ ├── Login.js
│ ├── PrivateRoute.js
│ └── Template.js
├── fonts
│ └── vazir.woff2
├── hooks
│ ├── useAuth.js
│ ├── useAuthActions.js
│ ├── useBlog.js
│ ├── useHistory.js
│ ├── useLocation.js
│ ├── useParams.js
│ └── useRouter.js
├── index.css
├── index.js
├── pages
│ ├── AboutUsPage.js
│ ├── BlogPage.js
│ ├── Example.js
│ ├── HomePage.js
│ ├── LoginPage.js
│ ├── LogoutPage.js
│ ├── NotFound.js
│ ├── PostPage.js
│ └── ProfilePage.js
└── routes.js
└── yarn.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | build
3 | package-lock.json
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "trailingComma": "es5",
3 | "bracketSpacing": true,
4 | "jsxBracketSameLine": false,
5 | "arrowParens": "always",
6 | "semi": false,
7 | "singleQuote": false
8 | }
9 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "files.autoSave": "onWindowChange",
3 | "editor.formatOnSave": true
4 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | 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.
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
46 | ### Code Splitting
47 |
48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49 |
50 | ### Analyzing the Bundle Size
51 |
52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53 |
54 | ### Making a Progressive Web App
55 |
56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57 |
58 | ### Advanced Configuration
59 |
60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61 |
62 | ### Deployment
63 |
64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65 |
66 | ### `npm run build` fails to minify
67 |
68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
69 |
--------------------------------------------------------------------------------
/jsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "baseUrl": "src"
4 | },
5 | "include": ["src"]
6 | }
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "intro-to-react-router-5",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "bootstrap": "^4.3.1",
7 | "formik": "^1.5.8",
8 | "helmet": "^3.20.0",
9 | "husky": "^3.0.4",
10 | "lint-staged": "^9.2.3",
11 | "prettier": "^1.18.2",
12 | "react": "^16.8.6",
13 | "react-dom": "^16.8.6",
14 | "react-router-dom": "^5.0.1",
15 | "react-scripts": "3.0.1",
16 | "reactstrap": "^8.0.1"
17 | },
18 | "lint-staged": {
19 | "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
20 | "prettier --write",
21 | "git add"
22 | ]
23 | },
24 | "scripts": {
25 | "start": "react-scripts start",
26 | "build": "react-scripts build",
27 | "test": "react-scripts test",
28 | "eject": "react-scripts eject"
29 | },
30 | "husky": {
31 | "hooks": {
32 | "pre-commit": "lint-staged"
33 | }
34 | },
35 | "eslintConfig": {
36 | "extends": "react-app"
37 | },
38 | "browserslist": {
39 | "production": [
40 | ">0.2%",
41 | "not dead",
42 | "not op_mini all"
43 | ],
44 | "development": [
45 | "last 1 chrome version",
46 | "last 1 firefox version",
47 | "last 1 safari version"
48 | ]
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nimaa77/react-router-course/da9fb701b8dcf31a66f58376f72e50ead8b22d71/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
13 |
14 |
23 | React App
24 |
25 |
26 |
27 |
28 |
29 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { Route, Switch } from "react-router-dom"
3 | import routes from "./routes"
4 | import Template from "components/Template"
5 | import AuthProvider from "components/AuthProvider"
6 | import PrivateRoute from "components/PrivateRoute"
7 |
8 | function App() {
9 | return (
10 |
11 |
12 |
13 | {routes.map((route) =>
14 | route.private === true ? (
15 |
16 | ) : (
17 |
18 | )
19 | )}
20 |
21 |
22 |
23 | )
24 | }
25 |
26 | export default App
27 |
--------------------------------------------------------------------------------
/src/components/AuthProvider.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | export const AuthProviderContext = React.createContext()
4 | export const AuthProviderContextDispatcher = React.createContext()
5 |
6 | const LOCAL_STORAGE_KEY = "authState"
7 |
8 | const initialState = () =>
9 | JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY) || false)
10 |
11 | function AuthProvider({ children }) {
12 | const [state, setState] = React.useState(initialState)
13 |
14 | React.useEffect(() => {
15 | const data = JSON.stringify(state)
16 | localStorage.setItem(LOCAL_STORAGE_KEY, data)
17 | }, [state])
18 |
19 | return (
20 |
21 |
22 | {children}
23 |
24 |
25 | )
26 | }
27 |
28 | export default AuthProvider
29 |
--------------------------------------------------------------------------------
/src/components/Debug.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | function Debug(props) {
4 | return (
5 | {JSON.stringify(props, null, 2)}
6 | )
7 | }
8 |
9 | export default Debug
10 |
--------------------------------------------------------------------------------
/src/components/Footer.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | function Footer(props) {
4 | return
5 | }
6 |
7 | export default Footer
8 |
--------------------------------------------------------------------------------
/src/components/Header.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import {
3 | Collapse,
4 | Navbar,
5 | NavbarToggler,
6 | NavbarBrand,
7 | Nav,
8 | NavItem,
9 | } from "reactstrap"
10 | import { NavLink } from "react-router-dom"
11 |
12 | import useAuth from "hooks/useAuth"
13 |
14 | function Header(props) {
15 | const [open, setOpen] = React.useState(false)
16 | const isLoggedIn = useAuth()
17 | return (
18 |
19 | تست
20 | setOpen((o) => !o)} />
21 |
22 |
71 |
72 |
73 | )
74 | }
75 |
76 | export default Header
77 |
--------------------------------------------------------------------------------
/src/components/Login.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { Formik, Form } from "formik"
3 | import useAuthActions from "hooks/useAuthActions"
4 | import useHistory from "hooks/useHistory"
5 |
6 | function Login() {
7 | const setLogin = useAuthActions()
8 | const history = useHistory()
9 | return (
10 | {
13 | if (values.email === "nima@arefi.com" && values.password === "admin") {
14 | setLogin(true)
15 | history.push("/profile")
16 | } else {
17 | actions.setFieldError("email", "نام کاربری یا رمزعبور اشتباه است")
18 | }
19 | actions.setSubmitting(false)
20 | }}
21 | render={(props) => (
22 |
59 | )}
60 | />
61 | )
62 | }
63 |
64 | export default Login
65 |
--------------------------------------------------------------------------------
/src/components/PrivateRoute.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { Redirect, Route } from "react-router-dom"
3 | import useAuth from "hooks/useAuth"
4 |
5 | function PrivateRoute(props) {
6 | const loggedIn = useAuth()
7 | return loggedIn ? :
8 | }
9 |
10 | export default PrivateRoute
11 |
--------------------------------------------------------------------------------
/src/components/Template.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import Header from "./Header"
3 | import Footer from "./Footer"
4 |
5 | function Template({ children }) {
6 | return (
7 | <>
8 |
9 | {children}
10 |
11 | >
12 | )
13 | }
14 |
15 | export default Template
16 |
--------------------------------------------------------------------------------
/src/fonts/vazir.woff2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nimaa77/react-router-course/da9fb701b8dcf31a66f58376f72e50ead8b22d71/src/fonts/vazir.woff2
--------------------------------------------------------------------------------
/src/hooks/useAuth.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { AuthProviderContext } from "components/AuthProvider"
3 |
4 | function useAuth() {
5 | return React.useContext(AuthProviderContext)
6 | }
7 |
8 | export default useAuth
9 |
--------------------------------------------------------------------------------
/src/hooks/useAuthActions.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { AuthProviderContextDispatcher } from "components/AuthProvider"
3 |
4 | function useAuthActions() {
5 | return React.useContext(AuthProviderContextDispatcher)
6 | }
7 |
8 | export default useAuthActions
9 |
--------------------------------------------------------------------------------
/src/hooks/useBlog.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | function useBlog() {
4 | const [data, setData] = React.useState(null)
5 | const [loading, setLoading] = React.useState(true)
6 |
7 | React.useEffect(() => {
8 | let mounted = true
9 | fetch("https://jsonplaceholder.ir/posts")
10 | .then((response) => response.json())
11 | .then((data) => mounted && setData(data))
12 | .then(false)
13 | .then(setLoading)
14 | return () => (mounted = false)
15 | }, [])
16 |
17 | return [loading, data]
18 | }
19 |
20 | export default useBlog
21 |
--------------------------------------------------------------------------------
/src/hooks/useHistory.js:
--------------------------------------------------------------------------------
1 | import useRouter from "hooks/useRouter"
2 |
3 | function useHistory() {
4 | const { history } = useRouter()
5 | return history
6 | }
7 |
8 | export default useHistory
9 |
--------------------------------------------------------------------------------
/src/hooks/useLocation.js:
--------------------------------------------------------------------------------
1 | import useRouter from "hooks/useRouter"
2 |
3 | function useLocation() {
4 | const { location } = useRouter()
5 | return location
6 | }
7 |
8 | export default useLocation
9 |
--------------------------------------------------------------------------------
/src/hooks/useParams.js:
--------------------------------------------------------------------------------
1 | import useRouter from "hooks/useRouter"
2 |
3 | function useParams() {
4 | const { match } = useRouter()
5 | return match.params
6 | }
7 |
8 | export default useParams
9 |
--------------------------------------------------------------------------------
/src/hooks/useRouter.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { __RouterContext } from "react-router-dom"
3 |
4 | function useRouter() {
5 | return React.useContext(__RouterContext)
6 | }
7 |
8 | export default useRouter
9 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | @font-face {
2 | font-family: vazir;
3 | font-style: normal;
4 | font-weight: normal;
5 | font-display: swap;
6 | src: url("./fonts/vazir.woff2") format("woff2");
7 | }
8 |
9 | body {
10 | direction: rtl;
11 | text-align: right;
12 | font-family: vazir;
13 | }
14 |
15 | footer {
16 | position: fixed;
17 | bottom: 0;
18 | padding: 10px 0;
19 | width: 100%;
20 | text-align: center;
21 | background: white;
22 | }
23 |
24 | .App {
25 | text-align: center;
26 | margin-top: 50vh;
27 | font-size: 50px;
28 | transform: translateY(-50%);
29 | }
30 |
31 | .ltr {
32 | direction: ltr;
33 | text-align: left;
34 | }
35 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import ReactDOM from "react-dom"
3 | import App from "./App"
4 | import { BrowserRouter } from "react-router-dom"
5 |
6 | import "bootstrap/dist/css/bootstrap.css"
7 | import "./index.css"
8 |
9 | ReactDOM.render(
10 |
11 |
12 | ,
13 | document.getElementById("root")
14 | )
15 |
--------------------------------------------------------------------------------
/src/pages/AboutUsPage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | function AboutUs(props) {
4 | return (
5 | <>
6 | اینجا صفحه درباره ما است
7 | >
8 | )
9 | }
10 |
11 | export default AboutUs
12 |
--------------------------------------------------------------------------------
/src/pages/BlogPage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { Card, CardBody, CardTitle } from "reactstrap"
3 | import { Link } from "react-router-dom"
4 |
5 | import useBlog from "hooks/useBlog"
6 |
7 | function Blog(props) {
8 | const [loading, posts] = useBlog()
9 | return (
10 | <>
11 | اینجا صفحه بلاگ است
12 | {loading ? (
13 | درحال بارگذاری
14 | ) : (
15 | posts.map((post) => )
16 | )}
17 | >
18 | )
19 | }
20 |
21 | function Post({ title, body, id }) {
22 | return (
23 |
24 |
25 |
26 | {title}
27 |
28 |
29 |
30 | )
31 | }
32 |
33 | export default Blog
34 |
--------------------------------------------------------------------------------
/src/pages/Example.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import Debug from "components/Debug"
3 |
4 | function Example(props) {
5 | const sort = props.match.params.sort || "asc"
6 |
7 | return
8 | }
9 |
10 | export default Example
11 |
--------------------------------------------------------------------------------
/src/pages/HomePage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { Link } from "react-router-dom"
3 |
4 | function HomePage(props) {
5 | return (
6 | <>
7 | اینجا صفحه اصلی است
8 | برو به صفحه درباره ما
9 | >
10 | )
11 | }
12 |
13 | export default HomePage
14 |
--------------------------------------------------------------------------------
/src/pages/LoginPage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import Login from "components/Login"
3 | import { Jumbotron, Container } from "reactstrap"
4 |
5 | function LoginPage() {
6 | return (
7 | <>
8 |
9 |
10 |
11 | ورود به حساب کاربری
12 |
13 |
14 |
15 |
16 | >
17 | )
18 | }
19 |
20 | export default LoginPage
21 |
--------------------------------------------------------------------------------
/src/pages/LogoutPage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import useAuthActions from "hooks/useAuthActions"
3 | import useHistory from "hooks/useHistory"
4 |
5 | function LogoutPage() {
6 | const setLogin = useAuthActions()
7 | const history = useHistory()
8 |
9 | React.useEffect(() => {
10 | setLogin(false)
11 | history.push("/")
12 | // eslint-disable-next-line react-hooks/exhaustive-deps
13 | }, [])
14 |
15 | return لطفا کمی صبر کنید
16 | }
17 |
18 | export default LogoutPage
19 |
--------------------------------------------------------------------------------
/src/pages/NotFound.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | function NotFound(props) {
4 | return (
5 |
6 | 404
7 |
8 | صفحهایی که به دنبال آن بودید پیدا نشد!
9 |
10 | )
11 | }
12 |
13 | export default NotFound
14 |
--------------------------------------------------------------------------------
/src/pages/PostPage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 |
3 | class Post extends React.Component {
4 | state = {
5 | loading: true,
6 | data: null,
7 | }
8 |
9 | mounted = true
10 |
11 | setStateIfMounted = (newState) => {
12 | if (this.mounted) {
13 | this.setState(newState)
14 | }
15 | }
16 |
17 | fetchData = () => {
18 | const { page } = this.props.match.params
19 | fetch(`https://jsonplaceholder.ir/posts/${page}`)
20 | .then((response) => response.json())
21 | .then((data) => this.setState({ data }))
22 | .finally(() => this.setState({ loading: false }))
23 | }
24 |
25 | componentDidMount() {
26 | this.fetchData()
27 | }
28 |
29 | componentDidUpdate(prevProps) {
30 | const { page } = this.props.match.params
31 | if (prevProps.match.params.page !== page) {
32 | this.setState({ loading: true })
33 | this.fetchData()
34 | }
35 | }
36 |
37 | componentWillUnmount() {
38 | this.mounted = false
39 | }
40 |
41 | render() {
42 | const { data, loading } = this.state
43 | return loading ? "در حال بار گذاری" : JSON.stringify(data, null, 2)
44 | }
45 | }
46 |
47 | export default Post
48 |
--------------------------------------------------------------------------------
/src/pages/ProfilePage.js:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | import { Jumbotron, Container } from "reactstrap"
3 |
4 | function ProfilePage() {
5 | return (
6 |
7 |
8 |
9 | اینجا صفحهایی است که شما بعد از لاگین شدن میتوانید ببینید.
10 |
11 |
12 |
13 | )
14 | }
15 |
16 | export default ProfilePage
17 |
--------------------------------------------------------------------------------
/src/routes.js:
--------------------------------------------------------------------------------
1 | import HomePage from "pages/HomePage"
2 | import AboutUs from "pages/AboutUsPage"
3 | import Post from "pages/PostPage"
4 | import Blog from "pages/BlogPage"
5 | import NotFound from "pages/NotFound"
6 | import Example from "pages/Example"
7 | import Login from "pages/LoginPage"
8 | import ProfilePage from "pages/ProfilePage"
9 | import LogoutPage from "pages/LogoutPage"
10 |
11 | const routes = [
12 | {
13 | exact: true,
14 | path: "/",
15 | component: HomePage,
16 | },
17 | {
18 | path: "/about-us",
19 | component: AboutUs,
20 | },
21 | {
22 | path: "/blog",
23 | component: Blog,
24 | },
25 | {
26 | path: "/post",
27 | component: Post,
28 | },
29 | {
30 | private: true,
31 | path: "/profile",
32 | component: ProfilePage,
33 | },
34 | {
35 | path: "/example/:sort(asc|dec)?",
36 | component: Example,
37 | },
38 | {
39 | path: "/login",
40 | component: Login,
41 | },
42 | {
43 | path: "/logout",
44 | component: LogoutPage,
45 | },
46 | {
47 | component: NotFound,
48 | },
49 | ]
50 |
51 | export default routes
52 |
53 | // path: "/example/:page([0-9]+)",
54 | // path: "/example/:name([A-Za-z]+)",
55 |
--------------------------------------------------------------------------------