├── .env
├── .gitignore
├── README.md
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
├── react-crud-example-web-api-demo.png
├── src
├── App.css
├── App.js
├── common
│ └── with-router.js
├── components
│ ├── add-tutorial.component.js
│ ├── tutorial.component.js
│ └── tutorials-list.component.js
├── http-common.js
├── index.js
├── logo.svg
├── 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.js CRUD App with React Router & Axios
2 |
3 | Build a React.js CRUD Application to consume Web API, display and modify data with Router, Axios & Bootstrap.
4 |
5 | React Tutorial Application in that:
6 | - Each Tutorial has id, title, description, published status.
7 | - We can create, retrieve, update, delete Tutorials.
8 | - There is a Search bar for finding Tutorials by title.
9 |
10 | 
11 |
12 | For instruction, please visit:
13 | > [React CRUD example to consume Web API](https://bezkoder.com/react-crud-web-api/)
14 |
15 | Related Posts:
16 | > [React Typescript CRUD example to consume Web API](https://bezkoder.com/react-typescript-axios/)
17 |
18 | > [React Redux CRUD App example with Rest API](https://bezkoder.com/react-redux-crud-example/)
19 |
20 | > [React (Hooks) CRUD example to consume Web API](https://bezkoder.com/react-hooks-crud-axios-api/)
21 |
22 | > [React Table example: CRUD App with react-table v7](https://bezkoder.com/react-table-example-hooks-crud/)
23 |
24 | Using Material UI instead of Bootstrap:
25 | > [React Material UI examples with a CRUD Application](https://bezkoder.com/react-material-ui-examples-crud/)
26 |
27 | More Practice:
28 | > [React Pagination example](https://bezkoder.com/react-pagination-material-ui/)
29 |
30 | > [React File Upload example](https://bezkoder.com/react-file-upload-axios/)
31 |
32 | > [React JWT Authentication & Authorization example](https://bezkoder.com/react-jwt-auth/)
33 |
34 | > [React + Redux: JWT Authentication & Authorization example](https://bezkoder.com/react-redux-jwt-auth/)
35 |
36 | Fullstack with Node.js Express:
37 | > [React.js + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/)
38 |
39 | > [React.js + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/)
40 |
41 | > [React.js + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/)
42 |
43 | Fullstack with Spring Boot:
44 | > [React.js + Spring Boot + MySQL](https://bezkoder.com/react-spring-boot-crud/)
45 |
46 | > [React.js + Spring Boot + PostgreSQL](https://bezkoder.com/spring-boot-react-postgresql/)
47 |
48 | > [React.js + Spring Boot + MongoDB](https://bezkoder.com/react-spring-boot-mongodb/)
49 |
50 | Fullstack with Django:
51 |
52 | > [React.js + Django Rest Framework](https://bezkoder.com/django-react-axios-rest-framework/)
53 |
54 | Serverless:
55 | > [React Firebase CRUD App with Realtime Database](https://bezkoder.com/react-firebase-crud/)
56 |
57 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://bezkoder.com/react-firestore-crud/)
58 |
59 | Integration (run back-end & front-end on same server/port)
60 | > [How to integrate React.js with Spring Boot](https://bezkoder.com/integrate-reactjs-spring-boot/)
61 |
62 | > [Integrate React with Node.js Express on same Server/Port](https://bezkoder.com/integrate-react-express-same-server-port/)
63 |
64 |
65 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
66 |
67 | ### Set port
68 | .env
69 | ```
70 | PORT=8081
71 | ```
72 |
73 | ## Project setup
74 |
75 | In the project directory, you can run:
76 |
77 | ```
78 | npm install
79 | # or
80 | yarn install
81 | ```
82 |
83 | or
84 |
85 | ### Compiles and hot-reloads for development
86 |
87 | ```
88 | npm start
89 | # or
90 | yarn start
91 | ```
92 |
93 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser.
94 |
95 | The page will reload if you make edits.
96 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-crud",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.16.4",
7 | "@testing-library/react": "^13.0.1",
8 | "@testing-library/user-event": "^14.1.1",
9 | "axios": "^0.27.2",
10 | "bootstrap": "^4.6.2",
11 | "react": "^18.2.0",
12 | "react-dom": "^18.2.0",
13 | "react-router-dom": "^6.4.0",
14 | "react-scripts": "5.0.1"
15 | },
16 | "scripts": {
17 | "start": "react-scripts start",
18 | "build": "react-scripts build",
19 | "test": "react-scripts test",
20 | "eject": "react-scripts eject"
21 | },
22 | "eslintConfig": {
23 | "extends": "react-app"
24 | },
25 | "browserslist": {
26 | "production": [
27 | ">0.2%",
28 | "not dead",
29 | "not op_mini all"
30 | ],
31 | "development": [
32 | "last 1 chrome version",
33 | "last 1 firefox version",
34 | "last 1 safari version"
35 | ]
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bezkoder/react-crud-web-api/8498b60d12cdbd7d3ada076fc5bb70a3eedc55e0/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-crud-web-api/8498b60d12cdbd7d3ada076fc5bb70a3eedc55e0/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bezkoder/react-crud-web-api/8498b60d12cdbd7d3ada076fc5bb70a3eedc55e0/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 |
--------------------------------------------------------------------------------
/react-crud-example-web-api-demo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bezkoder/react-crud-web-api/8498b60d12cdbd7d3ada076fc5bb70a3eedc55e0/react-crud-example-web-api-demo.png
--------------------------------------------------------------------------------
/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 | }
16 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from "react";
2 | import { Routes, 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 |
31 |
32 |
33 |
34 | } />
35 | } />
36 | } />
37 | } />
38 |
39 |
40 |
41 | );
42 | }
43 | }
44 |
45 | export default App;
46 |
--------------------------------------------------------------------------------
/src/common/with-router.js:
--------------------------------------------------------------------------------
1 | import { useLocation, useNavigate, useParams } from "react-router-dom";
2 |
3 | export const withRouter = (Component) => {
4 | function ComponentWithRouterProp(props) {
5 | let location = useLocation();
6 | let navigate = useNavigate();
7 | let params = useParams();
8 | return ;
9 | }
10 | return ComponentWithRouterProp;
11 | };
12 |
--------------------------------------------------------------------------------
/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 | import { withRouter } from '../common/with-router';
4 |
5 | class Tutorial extends Component {
6 | constructor(props) {
7 | super(props);
8 | this.onChangeTitle = this.onChangeTitle.bind(this);
9 | this.onChangeDescription = this.onChangeDescription.bind(this);
10 | this.getTutorial = this.getTutorial.bind(this);
11 | this.updatePublished = this.updatePublished.bind(this);
12 | this.updateTutorial = this.updateTutorial.bind(this);
13 | this.deleteTutorial = this.deleteTutorial.bind(this);
14 |
15 | this.state = {
16 | currentTutorial: {
17 | id: null,
18 | title: "",
19 | description: "",
20 | published: false
21 | },
22 | message: ""
23 | };
24 | }
25 |
26 | componentDidMount() {
27 | this.getTutorial(this.props.router.params.id);
28 | }
29 |
30 | onChangeTitle(e) {
31 | const title = e.target.value;
32 |
33 | this.setState(function(prevState) {
34 | return {
35 | currentTutorial: {
36 | ...prevState.currentTutorial,
37 | title: title
38 | }
39 | };
40 | });
41 | }
42 |
43 | onChangeDescription(e) {
44 | const description = e.target.value;
45 |
46 | this.setState(prevState => ({
47 | currentTutorial: {
48 | ...prevState.currentTutorial,
49 | description: description
50 | }
51 | }));
52 | }
53 |
54 | getTutorial(id) {
55 | TutorialDataService.get(id)
56 | .then(response => {
57 | this.setState({
58 | currentTutorial: response.data
59 | });
60 | console.log(response.data);
61 | })
62 | .catch(e => {
63 | console.log(e);
64 | });
65 | }
66 |
67 | updatePublished(status) {
68 | var data = {
69 | id: this.state.currentTutorial.id,
70 | title: this.state.currentTutorial.title,
71 | description: this.state.currentTutorial.description,
72 | published: status
73 | };
74 |
75 | TutorialDataService.update(this.state.currentTutorial.id, data)
76 | .then(response => {
77 | this.setState(prevState => ({
78 | currentTutorial: {
79 | ...prevState.currentTutorial,
80 | published: status
81 | }
82 | }));
83 | console.log(response.data);
84 | })
85 | .catch(e => {
86 | console.log(e);
87 | });
88 | }
89 |
90 | updateTutorial() {
91 | TutorialDataService.update(
92 | this.state.currentTutorial.id,
93 | this.state.currentTutorial
94 | )
95 | .then(response => {
96 | console.log(response.data);
97 | this.setState({
98 | message: "The tutorial was updated successfully!"
99 | });
100 | })
101 | .catch(e => {
102 | console.log(e);
103 | });
104 | }
105 |
106 | deleteTutorial() {
107 | TutorialDataService.delete(this.state.currentTutorial.id)
108 | .then(response => {
109 | console.log(response.data);
110 | this.props.router.navigate('/tutorials');
111 | })
112 | .catch(e => {
113 | console.log(e);
114 | });
115 | }
116 |
117 | render() {
118 | const { currentTutorial } = this.state;
119 |
120 | return (
121 |
122 | {currentTutorial ? (
123 |
124 |
Tutorial
125 |
154 |
155 | {currentTutorial.published ? (
156 |
162 | ) : (
163 |
169 | )}
170 |
171 |
177 |
178 |
185 |
{this.state.message}
186 |
187 | ) : (
188 |
189 |
190 |
Please click on a Tutorial...
191 |
192 | )}
193 |
194 | );
195 | }
196 | }
197 |
198 | export default withRouter(Tutorial);
--------------------------------------------------------------------------------
/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 |
5 | export default class TutorialsList extends Component {
6 | constructor(props) {
7 | super(props);
8 | this.onChangeSearchTitle = this.onChangeSearchTitle.bind(this);
9 | this.retrieveTutorials = this.retrieveTutorials.bind(this);
10 | this.refreshList = this.refreshList.bind(this);
11 | this.setActiveTutorial = this.setActiveTutorial.bind(this);
12 | this.removeAllTutorials = this.removeAllTutorials.bind(this);
13 | this.searchTitle = this.searchTitle.bind(this);
14 |
15 | this.state = {
16 | tutorials: [],
17 | currentTutorial: null,
18 | currentIndex: -1,
19 | searchTitle: ""
20 | };
21 | }
22 |
23 | componentDidMount() {
24 | this.retrieveTutorials();
25 | }
26 |
27 | onChangeSearchTitle(e) {
28 | const searchTitle = e.target.value;
29 |
30 | this.setState({
31 | searchTitle: searchTitle
32 | });
33 | }
34 |
35 | retrieveTutorials() {
36 | TutorialDataService.getAll()
37 | .then(response => {
38 | this.setState({
39 | tutorials: response.data
40 | });
41 | console.log(response.data);
42 | })
43 | .catch(e => {
44 | console.log(e);
45 | });
46 | }
47 |
48 | refreshList() {
49 | this.retrieveTutorials();
50 | this.setState({
51 | currentTutorial: null,
52 | currentIndex: -1
53 | });
54 | }
55 |
56 | setActiveTutorial(tutorial, index) {
57 | this.setState({
58 | currentTutorial: tutorial,
59 | currentIndex: index
60 | });
61 | }
62 |
63 | removeAllTutorials() {
64 | TutorialDataService.deleteAll()
65 | .then(response => {
66 | console.log(response.data);
67 | this.refreshList();
68 | })
69 | .catch(e => {
70 | console.log(e);
71 | });
72 | }
73 |
74 | searchTitle() {
75 | this.setState({
76 | currentTutorial: null,
77 | currentIndex: -1
78 | });
79 |
80 | TutorialDataService.findByTitle(this.state.searchTitle)
81 | .then(response => {
82 | this.setState({
83 | tutorials: response.data
84 | });
85 | console.log(response.data);
86 | })
87 | .catch(e => {
88 | console.log(e);
89 | });
90 | }
91 |
92 | render() {
93 | const { searchTitle, tutorials, currentTutorial, currentIndex } = this.state;
94 |
95 | return (
96 |
97 |
98 |
99 |
106 |
107 |
114 |
115 |
116 |
117 |
118 |
Tutorials List
119 |
120 |
121 | {tutorials &&
122 | tutorials.map((tutorial, index) => (
123 | - this.setActiveTutorial(tutorial, index)}
129 | key={index}
130 | >
131 | {tutorial.title}
132 |
133 | ))}
134 |
135 |
136 |
142 |
143 |
144 | {currentTutorial ? (
145 |
146 |
Tutorial
147 |
148 | {" "}
151 | {currentTutorial.title}
152 |
153 |
154 | {" "}
157 | {currentTutorial.description}
158 |
159 |
160 | {" "}
163 | {currentTutorial.published ? "Published" : "Pending"}
164 |
165 |
166 |
170 | Edit
171 |
172 |
173 | ) : (
174 |
175 |
176 |
Please click on a Tutorial...
177 |
178 | )}
179 |
180 |
181 | );
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/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.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import { createRoot } from "react-dom/client";
3 | import { BrowserRouter } from "react-router-dom";
4 |
5 | import App from "./App";
6 | import * as serviceWorker from "./serviceWorker";
7 |
8 | const container = document.getElementById("root");
9 | const root = createRoot(container);
10 |
11 | root.render(
12 |
13 |
14 |
15 | );
16 |
17 | serviceWorker.unregister();
18 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/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() {
5 | return http.get("/tutorials");
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 | findByTitle(title) {
29 | return http.get(`/tutorials?title=${title}`);
30 | }
31 | }
32 |
33 | export default new TutorialDataService();
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------