-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d36dac
commit 82140a1
Showing
6 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import React from 'react'; | ||
import logo from './logo.svg'; | ||
import FormDemo from './FormDemo'; | ||
import TimeDemo from './TimeDemo'; | ||
import './App.css'; | ||
|
||
export default function App() { | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<p> | ||
Edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<a | ||
className="App-link" | ||
href="https://reactjs.org" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn React | ||
</a> | ||
<p>Flask/React Demo</p> | ||
</header> | ||
|
||
<main className="App-main"> | ||
<section> | ||
<TimeDemo /> | ||
</section> | ||
<section> | ||
<FormDemo /> | ||
</section> | ||
</main> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.FormDemo-fields { | ||
margin-bottom: 0.5em; | ||
} | ||
|
||
.FormDemo-fields button { | ||
margin-left: 0.5em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, {useState} from 'react'; | ||
import flask from 'flask-urls.macro'; | ||
import './FormDemo.css'; | ||
|
||
const greetingURL = flask`api.greeting`; | ||
|
||
export default function FormDemo() { | ||
const [name, setName] = useState(''); | ||
const [result, setResult] = useState(null); | ||
const [loading, setLoading] = useState(false); | ||
const handleNameChange = e => setName(e.target.value.replace(/\//g, '').trim()); | ||
|
||
const handleSubmit = async (evt) => { | ||
evt.preventDefault(); | ||
setLoading(true); | ||
const resp = await fetch(greetingURL({name})); | ||
const data = await resp.json(); | ||
setResult(data.greeting); | ||
setLoading(false); | ||
}; | ||
|
||
return ( | ||
<> | ||
<form className="FormDemo-fields" onSubmit={handleSubmit}> | ||
<input | ||
placeholder="Enter a name" | ||
size="50" | ||
autoComplete="off" | ||
onChange={handleNameChange} | ||
disabled={loading} | ||
/> | ||
<button type="submit" disabled={loading}>Submit</button> | ||
</form> | ||
<p> | ||
The URL we will call is <code>{greetingURL({name})}</code>.<br /> | ||
{result === null | ||
? <>The API has never been called.</> | ||
: <>The last call's result is <code>{result || 'n/a'}</code>.</> | ||
} | ||
</p> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import flask from 'flask-urls.macro'; | ||
|
||
const timeURL = flask`api.time`; | ||
|
||
|
||
const fetchTime = async () => { | ||
const resp = await fetch(timeURL()); | ||
const data = await resp.json(); | ||
return data.now; | ||
}; | ||
|
||
export default function TimeDemo() { | ||
const [time, setTime] = useState(null); | ||
const [loading, setLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
setLoading(true); | ||
setTime(await fetchTime()); | ||
setLoading(false); | ||
})(); | ||
}, []); | ||
|
||
const handleClick = async () => { | ||
setLoading(true); | ||
setTime(await fetchTime()); | ||
setLoading(false); | ||
}; | ||
|
||
return time && ( | ||
<> | ||
<p> | ||
The server time returned by the API is <code>{time}</code>. | ||
</p> | ||
<button onClick={handleClick} disabled={loading}>Update</button> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters