Skip to content

Commit

Permalink
feat: add button message
Browse files Browse the repository at this point in the history
  • Loading branch information
int-tt committed Oct 21, 2019
1 parent 87da9db commit 0dd08ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
// import logo from './logo.svg';
import './App.css';
import { getResponseCount } from './backend';

function App() {
const [submitted, setSubmitted] = useState(false)
const [responseCount, setResponseCount] = useState()
const [status, setStatus] = useState("loading")

useEffect(() => {
getResponseCount().then(res => {
setResponseCount(res)
setStatus("loaded")
})
.catch(e => {
console.log(e.message)
setStatus("error")
})
}, [])
let buttonMessage = ""
switch (status) {
case "loading":
buttonMessage = "loading..."
break
case "loaded":
buttonMessage = `Vouch with ${responseCount} other's`
break
case "error":
buttonMessage = "error"
break
default:
}
const content = submitted ? (
<p>
Thanks for joining in!<br />
Expand All @@ -29,8 +56,8 @@ function App() {
<input value='' />
</label>
<button type="submit">
I'll vouch for that
</button>
{buttonMessage}
</button>
</form>
)

Expand Down
7 changes: 7 additions & 0 deletions src/backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function getResponseCount() {
let res = await window.fetch(
'https://us-central1-example-project-4ad97.cloudfunctions.net/api/response-count',
)
let { data } = await res.json()
return data.count
}

0 comments on commit 0dd08ab

Please sign in to comment.