├── .env ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.js ├── components │ ├── add-tutorial.component.js │ ├── tutorial.component.js │ └── tutorials-list.component.js ├── http-common.js ├── index.css ├── index.js ├── serviceWorker.js ├── services │ └── tutorial.service.js └── setupTests.js └── yarn.lock /.env: -------------------------------------------------------------------------------- 1 | PORT=8081 -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Pagination with API using Material-UI 2 | 3 | For instruction: 4 | > [React Pagination with API using Material-UI](https://bezkoder.com/react-pagination-material-ui/) 5 | 6 | Server side Pagination for this app: 7 | > [Node.js Express Pagination with MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/) 8 | 9 | > [Node.js Express Pagination with PostgreSQL](https://bezkoder.com/node-js-pagination-postgresql/) 10 | 11 | > [Node.js Express Pagination with MongoDB](https://bezkoder.com/node-js-mongodb-pagination/) 12 | 13 | > [Spring Boot Pagination and Filter example](https://bezkoder.com/spring-boot-pagination-filter-jpa-pageable/) 14 | 15 | > [Spring Boot MongoDB Pagination example with Spring Data](https://bezkoder.com/spring-boot-mongodb-pagination/) 16 | 17 | More practice: 18 | > [React (Components) CRUD example to consume Web API](https://bezkoder.com/react-crud-web-api/) 19 | 20 | > [React (Hooks) CRUD example to consume Web API](https://bezkoder.com/react-hooks-crud-axios-api/) 21 | 22 | Fullstack with Node.js Express: 23 | > [React.js + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/) 24 | 25 | > [React.js + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/) 26 | 27 | > [React.js + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/) 28 | 29 | Fullstack with Spring Boot: 30 | > [React.js + Spring Boot + MySQL](https://bezkoder.com/react-spring-boot-crud/) 31 | 32 | > [React.js + Spring Boot + PostgreSQL](https://bezkoder.com/spring-boot-react-postgresql/) 33 | 34 | > [React.js + Spring Boot + MongoDB](https://bezkoder.com/react-spring-boot-mongodb/) 35 | 36 | Fullstack with Django: 37 | 38 | > [React.js + Django Rest Framework](https://bezkoder.com/django-react-axios-rest-framework/) 39 | 40 | Integration (run back-end & front-end on same server/port) 41 | > [How to integrate React.js with Spring Boot](https://bezkoder.com/integrate-reactjs-spring-boot/) 42 | 43 | > [Integrate React with Node.js Express on same Server/Port](https://bezkoder.com/integrate-react-express-same-server-port/) 44 | 45 | 46 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 47 | 48 | ### Set port 49 | .env 50 | ``` 51 | PORT=8081 52 | ``` 53 | 54 | ## Project setup 55 | 56 | In the project directory, you can run: 57 | 58 | ``` 59 | npm install 60 | # or 61 | yarn install 62 | ``` 63 | 64 | or 65 | 66 | ### Compiles and hot-reloads for development 67 | 68 | ``` 69 | npm start 70 | # or 71 | yarn start 72 | ``` 73 | 74 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser. 75 | 76 | The page will reload if you make edits. 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-pagination", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@material-ui/core": "^4.10.2", 7 | "@material-ui/lab": "^4.0.0-alpha.56", 8 | "@testing-library/jest-dom": "^4.2.4", 9 | "@testing-library/react": "^9.3.2", 10 | "@testing-library/user-event": "^7.1.2", 11 | "axios": "^0.19.2", 12 | "bootstrap": "^4.5.0", 13 | "react": "^16.13.1", 14 | "react-dom": "^16.13.1", 15 | "react-router-dom": "^5.2.0", 16 | "react-scripts": "3.4.1" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": "react-app" 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-pagination-material-ui/795c7eafc8f00b1a3e6fe5cbf9aad83c6f22907a/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/bezkoder/react-pagination-material-ui/795c7eafc8f00b1a3e6fe5cbf9aad83c6f22907a/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bezkoder/react-pagination-material-ui/795c7eafc8f00b1a3e6fe5cbf9aad83c6f22907a/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 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .list { 2 | text-align: left; 3 | max-width: 750px; 4 | margin: auto; 5 | } 6 | 7 | .submit-form { 8 | max-width: 300px; 9 | margin: auto; 10 | } 11 | 12 | .edit-form { 13 | max-width: 300px; 14 | margin: auto; 15 | } -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; 3 | import "bootstrap/dist/css/bootstrap.min.css"; 4 | import "./App.css"; 5 | 6 | import AddTutorial from "./components/add-tutorial.component"; 7 | import Tutorial from "./components/tutorial.component"; 8 | import TutorialsList from "./components/tutorials-list.component"; 9 | 10 | class App extends Component { 11 | render() { 12 | return ( 13 | 14 |
15 | 32 | 33 |
34 | 35 | 36 | 37 | 38 | 39 |
40 |
41 |
42 | ); 43 | } 44 | } 45 | 46 | export default App; 47 | -------------------------------------------------------------------------------- /src/components/add-tutorial.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import TutorialDataService from "../services/tutorial.service"; 3 | 4 | export default class AddTutorial extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.onChangeTitle = this.onChangeTitle.bind(this); 8 | this.onChangeDescription = this.onChangeDescription.bind(this); 9 | this.saveTutorial = this.saveTutorial.bind(this); 10 | this.newTutorial = this.newTutorial.bind(this); 11 | 12 | this.state = { 13 | id: null, 14 | title: "", 15 | description: "", 16 | published: false, 17 | 18 | submitted: false 19 | }; 20 | } 21 | 22 | onChangeTitle(e) { 23 | this.setState({ 24 | title: e.target.value 25 | }); 26 | } 27 | 28 | onChangeDescription(e) { 29 | this.setState({ 30 | description: e.target.value 31 | }); 32 | } 33 | 34 | saveTutorial() { 35 | var data = { 36 | title: this.state.title, 37 | description: this.state.description 38 | }; 39 | 40 | TutorialDataService.create(data) 41 | .then(response => { 42 | this.setState({ 43 | id: response.data.id, 44 | title: response.data.title, 45 | description: response.data.description, 46 | published: response.data.published, 47 | 48 | submitted: true 49 | }); 50 | console.log(response.data); 51 | }) 52 | .catch(e => { 53 | console.log(e); 54 | }); 55 | } 56 | 57 | newTutorial() { 58 | this.setState({ 59 | id: null, 60 | title: "", 61 | description: "", 62 | published: false, 63 | 64 | submitted: false 65 | }); 66 | } 67 | 68 | render() { 69 | return ( 70 |
71 | {this.state.submitted ? ( 72 |
73 |

You submitted successfully!

74 | 77 |
78 | ) : ( 79 |
80 |
81 | 82 | 91 |
92 | 93 |
94 | 95 | 104 |
105 | 106 | 109 |
110 | )} 111 |
112 | ); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/components/tutorial.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import TutorialDataService from "../services/tutorial.service"; 3 | 4 | export default class Tutorial extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.onChangeTitle = this.onChangeTitle.bind(this); 8 | this.onChangeDescription = this.onChangeDescription.bind(this); 9 | this.getTutorial = this.getTutorial.bind(this); 10 | this.updatePublished = this.updatePublished.bind(this); 11 | this.updateTutorial = this.updateTutorial.bind(this); 12 | this.deleteTutorial = this.deleteTutorial.bind(this); 13 | 14 | this.state = { 15 | currentTutorial: { 16 | id: null, 17 | title: "", 18 | description: "", 19 | published: false 20 | }, 21 | message: "" 22 | }; 23 | } 24 | 25 | componentDidMount() { 26 | this.getTutorial(this.props.match.params.id); 27 | } 28 | 29 | onChangeTitle(e) { 30 | const title = e.target.value; 31 | 32 | this.setState(function(prevState) { 33 | return { 34 | currentTutorial: { 35 | ...prevState.currentTutorial, 36 | title: title 37 | } 38 | }; 39 | }); 40 | } 41 | 42 | onChangeDescription(e) { 43 | const description = e.target.value; 44 | 45 | this.setState(prevState => ({ 46 | currentTutorial: { 47 | ...prevState.currentTutorial, 48 | description: description 49 | } 50 | })); 51 | } 52 | 53 | getTutorial(id) { 54 | TutorialDataService.get(id) 55 | .then(response => { 56 | this.setState({ 57 | currentTutorial: response.data 58 | }); 59 | console.log(response.data); 60 | }) 61 | .catch(e => { 62 | console.log(e); 63 | }); 64 | } 65 | 66 | updatePublished(status) { 67 | var data = { 68 | id: this.state.currentTutorial.id, 69 | title: this.state.currentTutorial.title, 70 | description: this.state.currentTutorial.description, 71 | published: status 72 | }; 73 | 74 | TutorialDataService.update(this.state.currentTutorial.id, data) 75 | .then(response => { 76 | this.setState(prevState => ({ 77 | currentTutorial: { 78 | ...prevState.currentTutorial, 79 | published: status 80 | } 81 | })); 82 | console.log(response.data); 83 | }) 84 | .catch(e => { 85 | console.log(e); 86 | }); 87 | } 88 | 89 | updateTutorial() { 90 | TutorialDataService.update( 91 | this.state.currentTutorial.id, 92 | this.state.currentTutorial 93 | ) 94 | .then(response => { 95 | console.log(response.data); 96 | this.setState({ 97 | message: "The tutorial was updated successfully!" 98 | }); 99 | }) 100 | .catch(e => { 101 | console.log(e); 102 | }); 103 | } 104 | 105 | deleteTutorial() { 106 | TutorialDataService.delete(this.state.currentTutorial.id) 107 | .then(response => { 108 | console.log(response.data); 109 | this.props.history.push('/tutorials') 110 | }) 111 | .catch(e => { 112 | console.log(e); 113 | }); 114 | } 115 | 116 | render() { 117 | const { currentTutorial } = this.state; 118 | 119 | return ( 120 |
121 | {currentTutorial ? ( 122 |
123 |

Tutorial

124 |
125 |
126 | 127 | 134 |
135 |
136 | 137 | 144 |
145 | 146 |
147 | 150 | {currentTutorial.published ? "Published" : "Pending"} 151 |
152 |
153 | 154 | {currentTutorial.published ? ( 155 | 161 | ) : ( 162 | 168 | )} 169 | 170 | 176 | 177 | 184 |

{this.state.message}

185 |
186 | ) : ( 187 |
188 |
189 |

Please click on a Tutorial...

190 |
191 | )} 192 |
193 | ); 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /src/components/tutorials-list.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import TutorialDataService from "../services/tutorial.service"; 3 | import { Link } from "react-router-dom"; 4 | import Pagination from "@material-ui/lab/Pagination"; 5 | 6 | export default class TutorialsList extends Component { 7 | constructor(props) { 8 | super(props); 9 | this.onChangeSearchTitle = this.onChangeSearchTitle.bind(this); 10 | this.retrieveTutorials = this.retrieveTutorials.bind(this); 11 | this.refreshList = this.refreshList.bind(this); 12 | this.setActiveTutorial = this.setActiveTutorial.bind(this); 13 | this.removeAllTutorials = this.removeAllTutorials.bind(this); 14 | this.handlePageChange = this.handlePageChange.bind(this); 15 | this.handlePageSizeChange = this.handlePageSizeChange.bind(this); 16 | 17 | this.state = { 18 | tutorials: [], 19 | currentTutorial: null, 20 | currentIndex: -1, 21 | searchTitle: "", 22 | 23 | page: 1, 24 | count: 0, 25 | pageSize: 3, 26 | }; 27 | 28 | this.pageSizes = [3, 6, 9]; 29 | } 30 | 31 | componentDidMount() { 32 | this.retrieveTutorials(); 33 | } 34 | 35 | onChangeSearchTitle(e) { 36 | const searchTitle = e.target.value; 37 | 38 | this.setState({ 39 | searchTitle: searchTitle, 40 | }); 41 | } 42 | 43 | getRequestParams(searchTitle, page, pageSize) { 44 | let params = {}; 45 | 46 | if (searchTitle) { 47 | params["title"] = searchTitle; 48 | } 49 | 50 | if (page) { 51 | params["page"] = page - 1; 52 | } 53 | 54 | if (pageSize) { 55 | params["size"] = pageSize; 56 | } 57 | 58 | return params; 59 | } 60 | 61 | retrieveTutorials() { 62 | const { searchTitle, page, pageSize } = this.state; 63 | const params = this.getRequestParams(searchTitle, page, pageSize); 64 | 65 | TutorialDataService.getAll(params) 66 | .then((response) => { 67 | const { tutorials, totalPages } = response.data; 68 | 69 | this.setState({ 70 | tutorials: tutorials, 71 | count: totalPages, 72 | }); 73 | console.log(response.data); 74 | }) 75 | .catch((e) => { 76 | console.log(e); 77 | }); 78 | } 79 | 80 | refreshList() { 81 | this.retrieveTutorials(); 82 | this.setState({ 83 | currentTutorial: null, 84 | currentIndex: -1, 85 | }); 86 | } 87 | 88 | setActiveTutorial(tutorial, index) { 89 | this.setState({ 90 | currentTutorial: tutorial, 91 | currentIndex: index, 92 | }); 93 | } 94 | 95 | removeAllTutorials() { 96 | TutorialDataService.deleteAll() 97 | .then((response) => { 98 | console.log(response.data); 99 | this.refreshList(); 100 | }) 101 | .catch((e) => { 102 | console.log(e); 103 | }); 104 | } 105 | 106 | handlePageChange(event, value) { 107 | this.setState( 108 | { 109 | page: value, 110 | }, 111 | () => { 112 | this.retrieveTutorials(); 113 | } 114 | ); 115 | } 116 | 117 | handlePageSizeChange(event) { 118 | this.setState( 119 | { 120 | pageSize: event.target.value, 121 | page: 1 122 | }, 123 | () => { 124 | this.retrieveTutorials(); 125 | } 126 | ); 127 | } 128 | 129 | render() { 130 | const { 131 | searchTitle, 132 | tutorials, 133 | currentTutorial, 134 | currentIndex, 135 | page, 136 | count, 137 | pageSize, 138 | } = this.state; 139 | 140 | return ( 141 |
142 |
143 |
144 | 151 |
152 | 159 |
160 |
161 |
162 |
163 |

Tutorials List

164 | 165 |
166 | {"Items per Page: "} 167 | 174 | 175 | 185 |
186 | 187 | 202 | 203 | 209 |
210 |
211 | {currentTutorial ? ( 212 |
213 |

Tutorial

214 |
215 | {" "} 218 | {currentTutorial.title} 219 |
220 |
221 | {" "} 224 | {currentTutorial.description} 225 |
226 |
227 | {" "} 230 | {currentTutorial.published ? "Published" : "Pending"} 231 |
232 | 233 | 237 | Edit 238 | 239 |
240 | ) : ( 241 |
242 |
243 |

Please click on a Tutorial...

244 |
245 | )} 246 |
247 |
248 | ); 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /src/http-common.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | export default axios.create({ 4 | baseURL: "http://localhost:8080/api", 5 | headers: { 6 | "Content-type": "application/json" 7 | } 8 | }); -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/services/tutorial.service.js: -------------------------------------------------------------------------------- 1 | import http from "../http-common"; 2 | 3 | class TutorialDataService { 4 | getAll(params) { 5 | return http.get("/tutorials", { params }); 6 | } 7 | 8 | get(id) { 9 | return http.get(`/tutorials/${id}`); 10 | } 11 | 12 | create(data) { 13 | return http.post("/tutorials", data); 14 | } 15 | 16 | update(id, data) { 17 | return http.put(`/tutorials/${id}`, data); 18 | } 19 | 20 | delete(id) { 21 | return http.delete(`/tutorials/${id}`); 22 | } 23 | 24 | deleteAll() { 25 | return http.delete("/tutorials"); 26 | } 27 | } 28 | 29 | export default new TutorialDataService(); 30 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 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 | --------------------------------------------------------------------------------