Skip to content

Commit f269a3c

Browse files
committedMay 29, 2021
routing tansition
1 parent 2538242 commit f269a3c

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed
 

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# react quotes app
2+
3+
4+
`npm install react-router-dom`
5+
6+
7+
18
# Getting Started with Create React App
29

310
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

‎src/components/quotes/QuoteForm.js

+44-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { useRef } from 'react';
1+
import { Fragment, useRef, useState } from 'react';
2+
import { Prompt } from 'react-router-dom';
23

34
import Card from '../UI/Card';
45
import LoadingSpinner from '../UI/LoadingSpinner';
56
import classes from './QuoteForm.module.css';
67

78
const QuoteForm = (props) => {
9+
const [isEntering, setIsEntering] = useState(false);
10+
811
const authorInputRef = useRef();
912
const textInputRef = useRef();
1013

@@ -19,28 +22,48 @@ const QuoteForm = (props) => {
1922
props.onAddQuote({ author: enteredAuthor, text: enteredText });
2023
}
2124

25+
const finishEnteringHandler = () => {
26+
setIsEntering(false);
27+
};
28+
29+
const formFocusedHandler = () => {
30+
setIsEntering(true);
31+
};
32+
2233
return (
23-
<Card>
24-
<form className={classes.form} onSubmit={submitFormHandler}>
25-
{props.isLoading && (
26-
<div className={classes.loading}>
27-
<LoadingSpinner />
34+
<Fragment>
35+
<Prompt
36+
when={isEntering}
37+
message={(location) =>
38+
'Are you sure you want to leave? All your entered data will be lost!'
39+
}
40+
/>
41+
<Card>
42+
<form
43+
onFocus={formFocusedHandler}
44+
className={classes.form}
45+
onSubmit={submitFormHandler}
46+
>
47+
{props.isLoading && (
48+
<div className={classes.loading}>
49+
<LoadingSpinner />
50+
</div>
51+
)}
52+
53+
<div className={classes.control}>
54+
<label htmlFor='author'>Author</label>
55+
<input type='text' id='author' ref={authorInputRef} />
56+
</div>
57+
<div className={classes.control}>
58+
<label htmlFor='text'>Text</label>
59+
<textarea id='text' rows='5' ref={textInputRef}></textarea>
60+
</div>
61+
<div className={classes.actions}>
62+
<button onClick={finishEnteringHandler} className='btn'>Add Quote</button>
2863
</div>
29-
)}
30-
31-
<div className={classes.control}>
32-
<label htmlFor='author'>Author</label>
33-
<input type='text' id='author' ref={authorInputRef} />
34-
</div>
35-
<div className={classes.control}>
36-
<label htmlFor='text'>Text</label>
37-
<textarea id='text' rows='5' ref={textInputRef}></textarea>
38-
</div>
39-
<div className={classes.actions}>
40-
<button className='btn'>Add Quote</button>
41-
</div>
42-
</form>
43-
</Card>
64+
</form>
65+
</Card>
66+
</Fragment>
4467
);
4568
};
4669

‎src/pages/AllQuotes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import QuoteList from '../components/quotes/QuoteList';
22

33
const DUMMY_QUOTES = [
4-
{ id: 'q1', author: 'sarvesh', text: 'Learning React is fun!' },
5-
{ id: 'q2', author: 'sarvesh singh', text: 'Learning React is great!' },
4+
{ id: 'q1', author: 'Sarvesh', text: 'Learning React is fun!' },
5+
{ id: 'q2', author: 'Sarvesh Singh', text: 'Learning React is great!' },
66
];
77

88
const AllQuotes = () => {

‎src/pages/NewQuote.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import { useHistory } from 'react-router-dom';
2+
13
import QuoteForm from '../components/quotes/QuoteForm';
24

35
const NewQuote = () => {
6+
const history = useHistory();
7+
48
const addQuoteHandler = (quoteData) => {
59
console.log(quoteData);
10+
11+
history.push('/quotes');
612
};
713

814
return <QuoteForm onAddQuote={addQuoteHandler} />;

0 commit comments

Comments
 (0)
Please sign in to comment.