-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathApp.js
84 lines (78 loc) · 2.73 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import './App.css';
import { useState } from "react";
import LoadingSpinner from './components/spinner.js'
import CountryPicker from './components/countryPicker.js'
import StatePicker from './components/statePicker.js'
import CityPicker from './components/cityPicker.js'
// Import the functions to make API calls
import getRecords from './requests/getRecords.js';
import postRecord from './requests/postRecord.js';
function App() {
// Our hooks for data and setting that data.
const [loading, setLoading] = useState(false);
const [selectedCountry, setSelectedCountry] = useState("");
const [selectedState, setSelectedState] = useState("");
const [selectedCity, setSelectedCity] = useState("");
const [records, setRecords] = useState([]);
// Submit button's onclick function. Calls POST request
const submit = async () => {
setLoading(true);
let location = {
country: selectedCountry.name,
state: selectedState.name,
city: selectedCity.name
}
console.log(selectedCountry)
if (selectedCountry === "") {
alert("At least pick a country...")
} else {
let response = await postRecord(location);
console.log(response)
}
setLoading(false);
}
// Get button's onClick function. Calls GET API request.
const get = async () => {
setLoading(true);
let response = await getRecords();
let locationArray = [];
response.records.forEach((record, index) => {
locationArray.push(<li key={index}>{record.country.value}, {record.state.value}, {record.city.value}</li>)
});
setRecords(locationArray);
setLoading(false);
}
// Our react JSX.
return (
<div className="main">
<h2>I Use Kintone!</h2>
{/* If loading is true, show a spinner, otherwise show nothing. */}
{loading ? (
<div className="loadingDiv">
<LoadingSpinner />
</div>
) : null}
Welcome to React and Kintone!
<div className="selectDiv">
<p>Pick a Country</p>
<CountryPicker selectedCountry={selectedCountry} setSelectedCountry={setSelectedCountry}/>
<p>Then Pick a State</p>
<StatePicker selectedCountry={selectedCountry} selectedState={selectedState} setSelectedState={setSelectedState}/>
<p>Lastly, Pick a City</p>
<CityPicker selectedState={selectedState} selectedCity={selectedCity} setSelectedCity={setSelectedCity}/>
</div>
<div className="submitDiv">
<button onClick={submit} disabled={loading ? true : false}>
Submit!
</button>
<button onClick={get} disabled={loading ? true : false}>
Get!
</button>
</div>
<div className="listRecordsDiv">
<ul>{records}</ul>
</div>
</div>
);
}
export default App;