From 5f2ed4bf81de96d30cea934604a12038e17fc00d Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 8 Feb 2024 01:40:24 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Improve=20getLocation=20set=20va?= =?UTF-8?q?riable=20snippet=20(#1213)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added location error handling. ## 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. --------- Co-authored-by: Baptiste Arnaud --- .../docs/editor/blocks/logic/set-variable.mdx | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/apps/docs/editor/blocks/logic/set-variable.mdx b/apps/docs/editor/blocks/logic/set-variable.mdx index 4b43ae60f..4faac7199 100644 --- a/apps/docs/editor/blocks/logic/set-variable.mdx +++ b/apps/docs/editor/blocks/logic/set-variable.mdx @@ -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.