-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from BU-Spark/districtFromAPI
Boston Voter App: District number from google civic api
- Loading branch information
Showing
9 changed files
with
321 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* Form asking for user address and getting council district from Google Civic | ||
* API. Note: API key is in .env file | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import axios from 'axios'; | ||
import { Button, Grid, TextField } from '@mui/material'; | ||
|
||
|
||
// Set base URL for Axios | ||
// const api = axios.create({ | ||
// baseURL: 'https://pitne-voter-app-express-production.up.railway.app/', // Point this to server URL | ||
// }); | ||
const api = axios.create({ | ||
baseURL: 'http://localhost:3001', // Point this to server URL | ||
}); | ||
|
||
|
||
const DistrictForm: React.FC = () => { | ||
// Functions and variables to set district data | ||
const [street, setStreet] = useState(''); | ||
const [city, setCity] = useState(''); | ||
const [state, setState] = useState(''); | ||
const [zip, setZip] = useState(''); | ||
const [districtNum, setDistrictNum] = useState<string | null>(null); | ||
|
||
// Call API when address is submitted | ||
const handleSubmit = async (event: React.FormEvent) => { | ||
|
||
// Reset past data | ||
event.preventDefault(); | ||
setDistrictNum(null); | ||
|
||
// Set address | ||
const address = `${street} ${city}, ${state} ${zip}`; | ||
|
||
// Call API | ||
try { | ||
const response = await api.get('/api/district', { | ||
params: { address }, | ||
}); | ||
|
||
const data = response.data; | ||
|
||
// Set district number or error if no district number | ||
if (data) { | ||
console.log(data); | ||
setDistrictNum(data); | ||
} else { | ||
console.log("ERROR FETCHING DISTRICT - ensure address is within Boston bounds") | ||
} | ||
} catch { | ||
console.log("ERROR FETCHING DISTRICT - ensure address is within Boston bounds"); | ||
} | ||
}; | ||
|
||
|
||
// Address form | ||
return ( | ||
<div className='flex flex-col justify-center items-center p-4 flex-wrap'> | ||
|
||
{/* Address form */} | ||
<form onSubmit={handleSubmit} style={{ width: '100%', maxWidth: 600 }}> | ||
<Grid container spacing={2} > | ||
<Grid item xs={12} sm={6} > | ||
<TextField | ||
label="Street Number and Name" | ||
variant="outlined" | ||
fullWidth | ||
value={street} | ||
onChange={(e) => setStreet(e.target.value)} | ||
required | ||
sx={{ mb: 2 }} | ||
/> | ||
</Grid> | ||
<Grid item xs={12} sm={6}> | ||
<TextField | ||
label="City" | ||
variant="outlined" | ||
fullWidth | ||
value={city} | ||
onChange={(e) => setCity(e.target.value)} | ||
required | ||
sx={{ mb: 2 }} | ||
/> | ||
</Grid> | ||
<Grid item xs={12} sm={6}> | ||
<TextField | ||
label="State" | ||
variant="outlined" | ||
fullWidth | ||
value={state} | ||
onChange={(e) => setState(e.target.value)} | ||
required | ||
sx={{ mb: 2 }} | ||
/> | ||
</Grid> | ||
<Grid item xs={12} sm={6}> | ||
<TextField | ||
label="Zipcode" | ||
variant="outlined" | ||
fullWidth | ||
value={zip} | ||
onChange={(e) => setZip(e.target.value)} | ||
required | ||
sx={{ mb: 2 }} | ||
/> | ||
</Grid> | ||
</Grid> | ||
<div className="flex justify-center"> | ||
<Button type="submit" variant="outlined" className='p-3 mt-4 rounded-full bg-white text-blue-700 border-blue-800 hover:bg-blue-100'> | ||
Submit Address | ||
</Button> | ||
</div> | ||
</form> | ||
|
||
{/* NOTE: REMOVE BELOW PRINT, JUST FOR CHECKING WHILE BALLOT INFO IS IN PROGRESS */} | ||
<p>District Num: {districtNum}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default DistrictForm; |
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
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
34 changes: 34 additions & 0 deletions
34
strapi/src/api/candidate/content-types/candidate/schema.json
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,34 @@ | ||
{ | ||
"kind": "collectionType", | ||
"collectionName": "candidates", | ||
"info": { | ||
"singularName": "candidate", | ||
"pluralName": "candidates", | ||
"displayName": "Candidates", | ||
"description": "" | ||
}, | ||
"options": { | ||
"draftAndPublish": true | ||
}, | ||
"pluginOptions": {}, | ||
"attributes": { | ||
"District": { | ||
"type": "enumeration", | ||
"enum": [ | ||
"District 1", | ||
"District 2", | ||
"District 3", | ||
"District 4", | ||
"District 5", | ||
"District 6", | ||
"District 7", | ||
"District 8", | ||
"District 9" | ||
], | ||
"required": true | ||
}, | ||
"Name": { | ||
"type": "string" | ||
} | ||
} | ||
} |
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,9 @@ | ||
'use strict'; | ||
|
||
/** | ||
* candidate controller | ||
*/ | ||
|
||
const { createCoreController } = require('@strapi/strapi').factories; | ||
|
||
module.exports = createCoreController('api::candidate.candidate'); |
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,9 @@ | ||
'use strict'; | ||
|
||
/** | ||
* candidate router | ||
*/ | ||
|
||
const { createCoreRouter } = require('@strapi/strapi').factories; | ||
|
||
module.exports = createCoreRouter('api::candidate.candidate'); |
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,9 @@ | ||
'use strict'; | ||
|
||
/** | ||
* candidate service | ||
*/ | ||
|
||
const { createCoreService } = require('@strapi/strapi').factories; | ||
|
||
module.exports = createCoreService('api::candidate.candidate'); |
Oops, something went wrong.