├── .eslintrc.js ├── .gitignore ├── .prettierrc.js ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── components │ ├── App │ │ ├── App.test.tsx │ │ ├── index.tsx │ │ └── styles.ts │ ├── GuestBookEntryForm │ │ ├── index.tsx │ │ └── styles.ts │ └── Home │ │ ├── index.tsx │ │ └── styles.ts ├── hooks │ └── index.ts ├── index.css ├── index.tsx ├── interfaces │ ├── GuestBookEntry.ts │ ├── GuestBookModel.ts │ └── Store.ts ├── react-app-env.d.ts ├── setupTests.ts └── store │ ├── GuestBook.ts │ └── index.tsx └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | browser: true, 4 | es6: true, 5 | jest: true, 6 | }, 7 | extends: [ 8 | 'airbnb-typescript', 9 | 'airbnb/hooks', 10 | 'plugin:@typescript-eslint/eslint-recommended', 11 | 'plugin:@typescript-eslint/recommended', 12 | 'plugin:@typescript-eslint/recommended-requiring-type-checking' 13 | ], 14 | globals: { 15 | Atomics: 'readonly', 16 | SharedArrayBuffer: 'readonly', 17 | }, 18 | settings: { 19 | react: { 20 | pragma: 'React', 21 | version: 'detect' 22 | } 23 | }, 24 | parser: '@typescript-eslint/parser', 25 | parserOptions: { 26 | ecmaFeatures: { 27 | jsx: true, 28 | }, 29 | ecmaVersion: 2018, 30 | sourceType: 'module', 31 | project: './tsconfig.json', 32 | }, 33 | plugins: [ 34 | '@typescript-eslint', 35 | 'prettier' 36 | ], 37 | rules: { 38 | 'react/jsx-filename-extension': 0, 39 | 'react/jsx-props-no-spreading': 0, 40 | 'import/no-named-default': 0, 41 | 'import/no-named-as-default': 0, 42 | 'import/prefer-default-export': 0, 43 | '@typescript-eslint/ban-ts-ignore': 0, 44 | 'jsx-a11y/accessible-emoji': 0, 45 | 'no-param-reassign': 0, 46 | }, 47 | }; -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | trailingComma: "es5", 3 | tabWidth: 2, 4 | semi: true, 5 | singleQuote: true 6 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Setup 2 | 3 | * eslint + prettier - https://dev.to/benweiser/how-to-set-up-eslint-typescript-prettier-with-create-react-app-3675 4 | 5 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 6 | 7 | ## Available Scripts 8 | 9 | In the project directory, you can run: 10 | 11 | ### `npm start` 12 | 13 | Runs the app in the development mode.
14 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 15 | 16 | The page will reload if you make edits.
17 | You will also see any lint errors in the console. 18 | 19 | ### `npm test` 20 | 21 | Launches the test runner in the interactive watch mode.
22 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 23 | 24 | ### `npm run build` 25 | 26 | Builds the app for production to the `build` folder.
27 | It correctly bundles React in production mode and optimizes the build for the best performance. 28 | 29 | The build is minified and the filenames include the hashes.
30 | Your app is ready to be deployed! 31 | 32 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 33 | 34 | ### `npm run eject` 35 | 36 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 37 | 38 | 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. 39 | 40 | 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. 41 | 42 | 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. 43 | 44 | ## Learn More 45 | 46 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 47 | 48 | To learn React, check out the [React documentation](https://reactjs.org/). 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "guestbook", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@material-ui/core": "^4.8.3", 7 | "@testing-library/jest-dom": "^4.2.4", 8 | "@testing-library/react": "^9.4.0", 9 | "@testing-library/user-event": "^7.2.1", 10 | "@types/jest": "^24.0.25", 11 | "@types/node": "^12.12.24", 12 | "@types/react": "^16.9.17", 13 | "@types/react-dom": "^16.9.4", 14 | "@types/react-router-dom": "^5.1.3", 15 | "@types/yup": "^0.26.27", 16 | "easy-peasy": "^3.2.6", 17 | "react": "^16.12.0", 18 | "react-dom": "^16.12.0", 19 | "react-hook-form": "^4.5.2", 20 | "react-router-dom": "^5.1.2", 21 | "react-scripts": "3.3.0", 22 | "typescript": "^3.7.4", 23 | "yup": "^0.28.0" 24 | }, 25 | "scripts": { 26 | "start": "react-scripts start", 27 | "build": "react-scripts build", 28 | "test": "react-scripts test", 29 | "eject": "react-scripts eject" 30 | }, 31 | "eslintConfig": { 32 | "extends": "react-app" 33 | }, 34 | "browserslist": { 35 | "production": [ 36 | ">0.2%", 37 | "not dead", 38 | "not op_mini all" 39 | ], 40 | "development": [ 41 | "last 1 chrome version", 42 | "last 1 firefox version", 43 | "last 1 safari version" 44 | ] 45 | }, 46 | "devDependencies": { 47 | "@typescript-eslint/eslint-plugin": "^2.16.0", 48 | "@typescript-eslint/parser": "^2.16.0", 49 | "eslint": "^6.8.0", 50 | "eslint-config-airbnb": "^18.0.1", 51 | "eslint-config-airbnb-typescript": "^6.3.1", 52 | "eslint-config-prettier": "^6.9.0", 53 | "eslint-plugin-import": "^2.20.0", 54 | "eslint-plugin-jsx-a11y": "^6.2.3", 55 | "eslint-plugin-prettier": "^3.1.2", 56 | "eslint-plugin-react": "^7.17.0", 57 | "eslint-plugin-react-hooks": "^1.7.0", 58 | "prettier": "^1.19.1" 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingGarden/react-typescript-guestbook/dd77e834dfbdb608284279e08720f12a3794149f/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingGarden/react-typescript-guestbook/dd77e834dfbdb608284279e08720f12a3794149f/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingGarden/react-typescript-guestbook/dd77e834dfbdb608284279e08720f12a3794149f/public/logo512.png -------------------------------------------------------------------------------- /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 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /src/components/App/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './index'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/components/App/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { 3 | Switch, Route, Redirect, Link, 4 | } from 'react-router-dom'; 5 | import AppBar from '@material-ui/core/AppBar'; 6 | import Toolbar from '@material-ui/core/Toolbar'; 7 | import Button from '@material-ui/core/Button'; 8 | import IconButton from '@material-ui/core/IconButton'; 9 | import Container from '@material-ui/core/Container'; 10 | 11 | import useStyles from './styles'; 12 | import Home from '../Home'; 13 | 14 | const App: React.FC = () => { 15 | const classes = useStyles(); 16 | return ( 17 |
18 | 19 | 20 | 21 | 🍔 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |

About Page

32 |
33 | 34 |
35 |
36 |
37 | ); 38 | }; 39 | 40 | export default App; 41 | -------------------------------------------------------------------------------- /src/components/App/styles.ts: -------------------------------------------------------------------------------- 1 | import { makeStyles } from '@material-ui/core/styles'; 2 | 3 | export default makeStyles({ 4 | root: { 5 | flexGrow: 1, 6 | }, 7 | title: { 8 | flexGrow: 1, 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /src/components/GuestBookEntryForm/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import TextField from '@material-ui/core/TextField'; 3 | import Button from '@material-ui/core/Button'; 4 | import Box from '@material-ui/core/Box'; 5 | import { useForm } from 'react-hook-form'; 6 | import * as yup from 'yup'; 7 | 8 | import useStyles from './styles'; 9 | import GuestBookEntry from '../../interfaces/GuestBookEntry'; 10 | import { useStoreActions } from '../../hooks'; 11 | 12 | const GuestBookEntrySchema = yup.object().shape({ 13 | name: yup 14 | .string() 15 | .trim() 16 | .required('Required.'), 17 | content: yup 18 | .string() 19 | .trim() 20 | .min(10, 'Must be at least 10 characters.') 21 | .max(200, 'Can be no longer than 200 characters') 22 | .required('Required.'), 23 | }); 24 | 25 | const GuestBookEntryForm: React.FC = () => { 26 | const classes = useStyles(); 27 | const createEntry = useStoreActions((state) => state.guestbook.createEntry); 28 | const { 29 | register, handleSubmit, errors, reset, 30 | } = useForm({ 31 | validationSchema: GuestBookEntrySchema, 32 | }); 33 | const onSubmit = (data: GuestBookEntry): void => { 34 | createEntry(data); 35 | reset(); 36 | }; 37 | return ( 38 |
43 | 52 | 63 | 64 | 67 | 68 | 69 | ); 70 | }; 71 | 72 | export default GuestBookEntryForm; 73 | -------------------------------------------------------------------------------- /src/components/GuestBookEntryForm/styles.ts: -------------------------------------------------------------------------------- 1 | import { makeStyles } from '@material-ui/core/styles'; 2 | 3 | export default makeStyles({ 4 | formContainer: { 5 | margin: '1rem', 6 | '& .MuiTextField-root': { 7 | margin: '1rem 0', 8 | }, 9 | }, 10 | }); 11 | -------------------------------------------------------------------------------- /src/components/Home/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from 'react'; 2 | import { Card, CardContent, Typography } from '@material-ui/core'; 3 | 4 | import { useStoreState, useStoreActions } from '../../hooks'; 5 | import GuestBookEntryForm from '../GuestBookEntryForm'; 6 | import useStyles from './styles'; 7 | 8 | const Home: React.FC = () => { 9 | const reverseEntries = useStoreState((state) => state.guestbook.reverseEntries); 10 | const getEntries = useStoreActions((state) => state.guestbook.getEntries); 11 | const classes = useStyles(); 12 | 13 | useEffect(() => { 14 | getEntries(); 15 | }, []); // eslint-disable-line 16 | return ( 17 |
18 | 19 | { 20 | reverseEntries.map((entry) => ( 21 | 22 | 23 | 24 | {entry.name} 25 | 26 | 27 | {entry.content} 28 | 29 | 30 | {entry.submitted} 31 | 32 | 33 | 34 | )) 35 | } 36 |
37 | ); 38 | }; 39 | 40 | export default Home; 41 | -------------------------------------------------------------------------------- /src/components/Home/styles.ts: -------------------------------------------------------------------------------- 1 | import { makeStyles } from '@material-ui/core/styles'; 2 | 3 | export default makeStyles({ 4 | entryCard: { 5 | margin: '1rem 0', 6 | }, 7 | }); 8 | -------------------------------------------------------------------------------- /src/hooks/index.ts: -------------------------------------------------------------------------------- 1 | import { createTypedHooks } from 'easy-peasy'; 2 | import Store from '../interfaces/Store'; 3 | 4 | const typedHooks = createTypedHooks(); 5 | 6 | /* eslint-disable prefer-destructuring */ 7 | export const useStoreActions = typedHooks.useStoreActions; 8 | export const useStoreDispatch = typedHooks.useStoreDispatch; 9 | export const useStoreState = typedHooks.useStoreState; 10 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 0; 3 | margin: 0; 4 | } -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { BrowserRouter as Router } from 'react-router-dom'; 4 | import { StoreProvider } from 'easy-peasy'; 5 | 6 | import './index.css'; 7 | import App from './components/App'; 8 | import store from './store'; 9 | 10 | ReactDOM.render( 11 | 12 | 13 | 14 | 15 | , 16 | document.getElementById('root'), 17 | ); 18 | -------------------------------------------------------------------------------- /src/interfaces/GuestBookEntry.ts: -------------------------------------------------------------------------------- 1 | export default interface GuestBookEntry { 2 | id: number; 3 | name: string; 4 | content: string; 5 | submitted?: string; 6 | } 7 | -------------------------------------------------------------------------------- /src/interfaces/GuestBookModel.ts: -------------------------------------------------------------------------------- 1 | import { Action, Thunk, Computed } from 'easy-peasy'; 2 | 3 | import GuestBookEntry from './GuestBookEntry'; 4 | 5 | export default interface GuestBookModel { 6 | entries: GuestBookEntry[]; 7 | reverseEntries: Computed; 8 | setEntries: Action; 9 | addEntry: Action; 10 | createEntry: Thunk; 11 | getEntries: Thunk; 12 | } 13 | -------------------------------------------------------------------------------- /src/interfaces/Store.ts: -------------------------------------------------------------------------------- 1 | import GuestBookModel from './GuestBookModel'; 2 | 3 | export default interface Store { 4 | guestbook: GuestBookModel; 5 | } 6 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/store/GuestBook.ts: -------------------------------------------------------------------------------- 1 | import { action, thunk, computed } from 'easy-peasy'; 2 | 3 | import GuestBookModel from '../interfaces/GuestBookModel'; 4 | 5 | const GuestBook: GuestBookModel = { 6 | entries: [], 7 | reverseEntries: computed((state) => state.entries.slice().reverse()), 8 | setEntries: action((state, entries) => { 9 | state.entries = entries; 10 | }), 11 | addEntry: action((state, entry) => { 12 | state.entries.push(entry); 13 | }), 14 | createEntry: thunk(async (state, entry) => { 15 | const response = await fetch('http://localhost:5454/entries', { 16 | method: 'POST', 17 | headers: { 18 | 'content-type': 'application/json', 19 | }, 20 | body: JSON.stringify(entry), 21 | }); 22 | const result = await response.json(); 23 | state.addEntry(result); 24 | }), 25 | getEntries: thunk(async (state) => { 26 | const response = await fetch('http://localhost:5454/entries'); 27 | const entries = await response.json(); 28 | state.setEntries(entries); 29 | }), 30 | }; 31 | 32 | export default GuestBook; 33 | -------------------------------------------------------------------------------- /src/store/index.tsx: -------------------------------------------------------------------------------- 1 | import { createStore } from 'easy-peasy'; 2 | 3 | import Store from '../interfaces/Store'; 4 | import GuestBook from './GuestBook'; 5 | 6 | const store: Store = { 7 | guestbook: GuestBook, 8 | }; 9 | 10 | export default createStore(store); 11 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "esnext", 16 | "moduleResolution": "node", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "noEmit": true, 20 | "jsx": "react" 21 | }, 22 | "include": [ 23 | "src" 24 | ] 25 | } 26 | --------------------------------------------------------------------------------