Skip to content

Commit

Permalink
馃摑 Improve getLocation set variable snippet (#1213)
Browse files Browse the repository at this point in the history
Added location error handling.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit


- **Refactor**
	- Updated location retrieval functionality:
		- Renamed function for clarity.
		- Enhanced location information to include latitude and longitude.
		- Improved error handling for smoother user experience.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
  • Loading branch information
CptPlastic and baptisteArno committed Feb 8, 2024
1 parent 2f6de8e commit 5f2ed4b
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions apps/docs/editor/blocks/logic/set-variable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,24 @@ This value block allows you to find the `Id` from `Ids` with the same index as `
For this you can provide the following custom code:

```js
function getPosition() {
return new Promise((resolve, reject) => {
function getLocation() {
return new Promise((resolve) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve(position),
(error) => reject(error),
position => resolve(`${position.coords.latitude}, ${position.coords.longitude}`),
error => resolve("error"),
{ enableHighAccuracy: true, timeout: 5000 }
)
})
);
});
}

const coords = (await getPosition()).coords
const coords = await getLocation();

return `${coords.latitude}, ${coords.longitude}`
// Check for error
if (coords === "error") {
return "Unable to get location";
}

return coords;
```

This custom function can only work when it is executed on the client browser so you need to make sure to enable the "Execute on client" option.

0 comments on commit 5f2ed4b

Please sign in to comment.