Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group livewire updates and return 7 decimal places in lat lng #136

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 43 additions & 34 deletions resources/js/filament-google-geocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,48 +216,57 @@ export default function filamentGoogleGeocomplete({
}
},
updateReverseGeocode: async function (place) {
if (this.hasReverseGeocode()) {
if (place.address_components) {
//await setStateUsing(config.autocomplete, response.results[0].formatted_address);
const replacements = this.getReplacements(place.address_components);

for (const field in reverseGeocodeFields) {
let replaced = reverseGeocodeFields[field];

for (const replacement in replacements) {
replaced = replaced
.split(replacement)
.join(replacements[replacement]);
}
if (this.hasReverseGeocode()) {
if (place.address_components) {
const replacements = this.getReplacements(place.address_components);
const updatePromises = [];

for (const symbol in this.symbols) {
replaced = replaced.split(symbol).join("");
}
// Collect all update promises
for (const field in reverseGeocodeFields) {
let replaced = reverseGeocodeFields[field];

replaced = replaced.trim();
await setStateUsing(field, replaced);
for (const replacement in replacements) {
replaced = replaced
.split(replacement)
.join(replacements[replacement]);
}

for (const symbol in this.symbols) {
replaced = replaced.split(symbol).join("");
}

replaced = replaced.trim();
updatePromises.push(setStateUsing(field, replaced));
}

if (hasReverseGeocodeUsing) {
reverseGeocodeUsing(place);
// Wait for all updates to complete
try {
await Promise.all(updatePromises);
} catch (error) {
console.error('Error batch updating fields:', error);
}
}
},
updateLatLng: async function (place) {
if (Object.keys(latLngFields).length > 0) {
if (place.geometry) {
await setStateUsing(
latLngFields.lat,
place.geometry.location.lat().toString()
);
await setStateUsing(
latLngFields.lng,
place.geometry.location.lng().toString()
);
}

// Handle the reverseGeocodeUsing callback separately if it exists
if (hasReverseGeocodeUsing) {
reverseGeocodeUsing(place);
}
},
}
},
updateLatLng: async function (place) {
if (Object.keys(latLngFields).length > 0 && place.geometry) {
const updatePromises = [
setStateUsing(latLngFields.lat, place.geometry.location.lat().toFixed(7)),
setStateUsing(latLngFields.lng, place.geometry.location.lng().toFixed(7))
];

try {
await Promise.all(updatePromises);
} catch (error) {
console.error('Error updating lat/lng fields:', error);
}
}
},
getReplacements: function (address_components) {
let replacements = {};

Expand Down