Code Refactoring before geoCODING!¶
Refactoring code means re-writing code without changing its function to be more understandable and/or reusable. We are going to refactor so that when we get the Google form data it is simpler to know where to change the code!
Putting the Fetch in a function¶
Our fetch call sits out in the middle of nowhere, which is the Global space! That is not good because if the fetch doesn’t work then our page won’t load!
| js/init.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 | |
Leaving it there will break our code if we leave it there without a file to get, so let’s move that fetch into a function we will call later:
function loadData(){
fetch(`map.geojson`)
.then(response => {
console.log(response)
return response
})
.then(data =>{
// do something with the data
})
}
// we will put this comment to remember to call our function later!
// loadData()
fetch(`map.geojson`)
.then(response => {
console.log(response)
return response
})
.then(data =>{
// do something with the data
})
Changing the map.geojson into a variable¶
Let’s also change the map.geojson to a parameter called url that way we can use this function to get different urls!
To make our lives easier, we’ll define a new variable called dataUrl and use that as a placeholder for our function too.
const dataUrl = "https://some.data.com/" //(1)!
function loadData(url){ //(2)!
fetch(url)
.then(response => {
return response
})
.then(data =>{
// do something with the data
})
}
// we will put this comment to remember to call our function later!
// loadData(dataUrl)
// (3)!
- The URL variable we can change later when we get the URL we need.
- The new URL parameter to get data from!
- Function call that uses the
loadData()function anddataUrlparameter!
Some CSS Touch-up!¶
Lastly, let’s make our survey look a little nicer, by adding two columns to our secondary grid in the .main CSS selector.
Change #1 Adding columns lengths¶
Because we are using css-grid the way to add columns is by using the grid-template-columns class. We can assign a fixed value, like 100px for 100 pixels, but let’s make our site scalable to any screen size by using a fr value for each of 1fr 1fr. So our current change should look like:
.main{
grid-area: main_content;
grid-template-columns: 1fr 1fr;
grid-template-areas: "main_map" "content";
display: grid;
}
freal, an side about CSS unit lengths
CSS has many units for length, such as pixels or % percentage that can account for how much of a page to cover. However, is a new unit fr stands for fraction and it represents a fraction of the available space in the grid container. What this means it can automatically account for the fraction of a page!!! You can also mix and match units. Learn more here.
CCS Change #2: Putting content on the same row¶
Now that we created the columns, now we need to assign the columns to the rows! With css-grid the grid-template-areas property is already how we assign rows and columns:
grid-template-areas: "main_map" "content";
Means have one row for main_map and one row for content.
To put the areas on the same row we modify both of them to be in the same ” “ pair, and separated by a space (), as follows:
grid-template-areas: "main_map content";
If you change the order, like "content main_map" then content will show up on the left:
grid-template-areas: "content main_map";
For now, let’s keep the map on the left.
The resulting CSS should look like the CSS after tab:
.main{
grid-area: main_content;
grid-template-columns: 1fr 1fr; /* (1)! */
grid-template-areas: "main_map content"; /* (2)! */
display: grid;
}
1fr 1frgives us two equal columns, setting it to2fr 1frmakes the first column fill up twice the space of the second."main_map content"are in the same quotations"now!
.main{
grid-area: main_content;
grid-template-areas: "main_map" "content" ;
display: grid;
}
🏁Checkpoint¶
Before moving on, make sure your code looks like the following:
| index.html | |
|---|---|
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 | |
| js/init.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 | |
✅ Final Template Code¶
| index.html | |
|---|---|
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 | |
| js/init.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 | |
| styles/style.css | |
|---|---|
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 | |