Last active
September 11, 2019 21:09
-
-
Save paveltyk/875aa65de51e19ce984bb4941210a470 to your computer and use it in GitHub Desktop.
Phoenix + React + Heroku + Docker https://medium.com/@paveltyk/phoenix-react-heroku-docker-44d5c4a83ab1
This file contains hidden or 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
import React, { useState } from 'react'; | |
import axios from 'axios'; | |
import './App.css'; | |
function App() { | |
const [email, setEmail] = useState(''); | |
const [error, setError] = useState(''); | |
const [isSubmitted, setSubmitted] = useState(false); | |
const [isProcessing, setProcessing] = useState(false); | |
async function handleSubmit(e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
try { | |
setProcessing(true); | |
await axios.post('/api/subscriptions', { email }); | |
setSubmitted(true); | |
} catch (error) { | |
setSubmitted(true); | |
setError('server responded with error'); | |
} | |
} | |
if (isSubmitted) { | |
return ( | |
<div className="App"> | |
{ | |
error | |
? <p className="error">Failed to submit: {error}</p> | |
: <p className="success">Thank you! Email <b>"{email}"</b> has been added to the list.</p> | |
} | |
</div> | |
); | |
} | |
return ( | |
<div className="App"> | |
<form onSubmit={handleSubmit}> | |
<input | |
type="email" | |
placeholder="Your email..." | |
value={email} | |
onChange={(e) => setEmail(e.target.value)} | |
disabled={isProcessing} /> | |
</form> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment