Skip to content

Commit 619c281

Browse files
committed
Fix zip code handling with geolocation
1 parent 35a3e04 commit 619c281

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

src/components/Explorer.vue

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ export default {
864864
filtersOpen: false,
865865
zipCode: "",
866866
displayLocation: "",
867+
manualZip: false,
867868
updatingCounts: false,
868869
selected: null,
869870
localStoreLinks: [],
@@ -1028,31 +1029,46 @@ export default {
10281029
async mounted() {
10291030
// Pick a random hero image after hydration to avoid SSR hydration mismatch
10301031
this.twoUpIndex = Math.floor(Math.random() * twoUpImageCredits.length);
1031-
this.displayLocation = localStorage.getItem("displayLocation")
1032-
this.zipCode= localStorage.getItem("zipCode")
1033-
this.filterValues["States"] = [localStorage.getItem("state")]
1034-
if ("geolocation" in navigator || this.zipCode == '') {
1035-
navigator.geolocation.getCurrentPosition(async (position) => {
1036-
let data = {
1037-
latitude: position.coords.latitude,
1038-
longitude: position.coords.longitude,
1039-
};
1040-
//get zipcode by lng/lat
1041-
//get state by lng / lat
1042-
const response = await fetch("/get-zip", {
1043-
method: "POST", // *GET, POST, PUT, DELETE, etc.
1044-
headers: {
1045-
"Content-Type": "application/json",
1046-
},
1047-
body: JSON.stringify(data), // body data type must match "Content-Type" header
1048-
});
1049-
let json = await response.json();
1050-
this.zipCode = json.code;
1051-
this.filterValues["States"] = [json.state];
1052-
this.taisplayLocation = `${json.city}, ${json.state}`;
1053-
});
1054-
} else {
1055-
console.log("Location not supported");
1032+
1033+
this.displayLocation = localStorage.getItem("displayLocation") || "";
1034+
this.zipCode = localStorage.getItem("zipCode") || "";
1035+
this.manualZip = localStorage.getItem("manualZip") === "true";
1036+
this.filterValues["States"] = [localStorage.getItem("state")];
1037+
1038+
if (!this.manualZip && "geolocation" in navigator) {
1039+
navigator.geolocation.getCurrentPosition(
1040+
async (position) => {
1041+
const data = {
1042+
latitude: position.coords.latitude,
1043+
longitude: position.coords.longitude,
1044+
};
1045+
1046+
const response = await fetch("/get-zip", {
1047+
method: "POST",
1048+
headers: {
1049+
"Content-Type": "application/json",
1050+
},
1051+
body: JSON.stringify(data),
1052+
});
1053+
let json = await response.json();
1054+
this.zipCode = json.code;
1055+
this.filterValues["States"] = [json.state];
1056+
this.displayLocation = `${json.city}, ${json.state}`;
1057+
1058+
localStorage.setItem("zipCode", this.zipCode);
1059+
localStorage.setItem("state", json.state);
1060+
localStorage.setItem("displayLocation", this.displayLocation);
1061+
localStorage.removeItem("manualZip");
1062+
},
1063+
() => {
1064+
if (!this.zipCode) {
1065+
this.zipCode = "19355";
1066+
}
1067+
}
1068+
);
1069+
} else if (!this.zipCode) {
1070+
// Geolocation not available or user chose manual zip
1071+
this.zipCode = "19355";
10561072
}
10571073
10581074
await this.determineFilterCountsAndSubmit();
@@ -1101,6 +1117,8 @@ export default {
11011117
localStorage.setItem("displayLocation", this.displayLocation)
11021118
localStorage.setItem("zipCode", this.zipCode)
11031119
localStorage.setItem("state", json.state)
1120+
this.manualZip = true;
1121+
localStorage.setItem("manualZip", "true")
11041122
},
11051123
async getVendors() {
11061124
if (!this.selected) return [];

0 commit comments

Comments
 (0)