@@ -1029,40 +1029,75 @@ export default {
1029
1029
this .manualZip = localStorage .getItem (" manualZip" ) === " true" ;
1030
1030
this .filterValues [" States" ] = [localStorage .getItem (" state" )];
1031
1031
1032
+ // Pre-set the default zip code to ensure immediate response
1033
+ if (! this .zipCode ) {
1034
+ this .zipCode = " 19355" ;
1035
+ }
1036
+
1032
1037
if (! this .manualZip && " geolocation" in navigator ) {
1038
+ // Set loading state
1039
+ this .isLoadingLocation = true ;
1040
+
1041
+ // Use a timeout to limit how long we wait for geolocation
1042
+ const geolocationTimeout = setTimeout (() => {
1043
+ if (this .isLoadingLocation ) {
1044
+ // If still loading after timeout, use default
1045
+ this .isLoadingLocation = false ;
1046
+ this .setDefaultZipCode ();
1047
+ }
1048
+ }, 2000 ); // 2 second timeout
1049
+
1033
1050
navigator .geolocation .getCurrentPosition (
1034
1051
async (position ) => {
1052
+ clearTimeout (geolocationTimeout);
1053
+
1035
1054
const data = {
1036
1055
latitude: position .coords .latitude ,
1037
1056
longitude: position .coords .longitude ,
1038
1057
};
1039
1058
1040
- const response = await fetch (" /get-zip" , {
1041
- method: " POST" ,
1042
- headers: {
1043
- " Content-Type" : " application/json" ,
1044
- },
1045
- body: JSON .stringify (data),
1046
- });
1047
- let json = await response .json ();
1048
- this .zipCode = json .code ;
1049
- this .filterValues [" States" ] = [json .state ];
1050
- this .displayLocation = ` ${ json .city } , ${ json .state } ` ;
1051
-
1052
- localStorage .setItem (" zipCode" , this .zipCode );
1053
- localStorage .setItem (" state" , json .state );
1054
- localStorage .setItem (" displayLocation" , this .displayLocation );
1055
- localStorage .removeItem (" manualZip" );
1056
- },
1057
- () => {
1058
- if (! this .zipCode ) {
1059
- this .zipCode = " 19355" ;
1059
+ try {
1060
+ const response = await fetch (" /get-zip" , {
1061
+ method: " POST" ,
1062
+ headers: {
1063
+ " Content-Type" : " application/json" ,
1064
+ },
1065
+ body: JSON .stringify (data),
1066
+ });
1067
+
1068
+ if (! response .ok ) {
1069
+ throw new Error (' Network response was not ok' );
1070
+ }
1071
+
1072
+ let json = await response .json ();
1073
+ this .zipCode = json .code ;
1074
+ this .filterValues [" States" ] = [json .state ];
1075
+ this .displayLocation = ` ${ json .city } , ${ json .state } ` ;
1076
+
1077
+ localStorage .setItem (" zipCode" , this .zipCode );
1078
+ localStorage .setItem (" state" , json .state );
1079
+ localStorage .setItem (" displayLocation" , this .displayLocation );
1080
+ localStorage .removeItem (" manualZip" );
1081
+ } catch (error) {
1082
+ console .error (" Error fetching zip code:" , error);
1083
+ this .setDefaultZipCode ();
1084
+ } finally {
1085
+ this .isLoadingLocation = false ;
1060
1086
}
1061
- }
1087
+ },
1088
+ (error ) => {
1089
+ clearTimeout (geolocationTimeout);
1090
+ console .error (" Geolocation error:" , error);
1091
+ this .isLoadingLocation = false ;
1092
+
1093
+ // Use default zip code if geolocation fails
1094
+ this .setDefaultZipCode ();
1095
+ },
1096
+ { timeout: 3000 , maximumAge: 60000 } // 3s timeout, cache for 1 minute
1062
1097
);
1063
- } else if (! this .zipCode ) {
1098
+ } else if (! this .displayLocation ) {
1064
1099
// Geolocation not available or user chose manual zip
1065
- this .zipCode = " 19355 " ;
1100
+ this .setDefaultZipCode () ;
1066
1101
}
1067
1102
1068
1103
await this .determineFilterCountsAndSubmit ();
@@ -1072,6 +1107,33 @@ export default {
1072
1107
document .body .removeEventListener (" click" , this .bodyClick );
1073
1108
},
1074
1109
methods: {
1110
+ setDefaultZipCode () {
1111
+ // Set default zip code to 19355 (Malvern, PA)
1112
+ this .zipCode = " 19355" ;
1113
+
1114
+ // Get city and state info for this zip code
1115
+ fetch (" /get-city" , {
1116
+ method: " POST" ,
1117
+ headers: {
1118
+ " Content-Type" : " application/json" ,
1119
+ },
1120
+ body: JSON .stringify ({ zipCode: this .zipCode }),
1121
+ })
1122
+ .then (response => response .json ())
1123
+ .then (json => {
1124
+ this .filterValues [" States" ] = [json .state ];
1125
+ this .displayLocation = ` ${ json .city } , ${ json .state } ` ;
1126
+
1127
+ // Save to localStorage
1128
+ localStorage .setItem (" zipCode" , this .zipCode );
1129
+ localStorage .setItem (" state" , json .state );
1130
+ localStorage .setItem (" displayLocation" , this .displayLocation );
1131
+ })
1132
+ .catch (error => {
1133
+ console .error (" Error fetching city data:" , error);
1134
+ });
1135
+ },
1136
+
1075
1137
forceUpdate () {
1076
1138
// Force Vue to re-render by directly calling submit and manipulating the DOM
1077
1139
this .submit ();
0 commit comments