@geophrase/react 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,9 +1,68 @@
1
1
  # @geophrase/react
2
2
 
3
- The official React and Next.js hooks for integrating the Geophrase Connect address selector to reduce Return to Origin (RTO) costs.
3
+ The official React and Next.js hook for Geophrase Connect. Drop the Geophrase address selector into any React checkout in minutes and capture perfectly structured Indian addresses and coordinates to eliminate Return to Origin (RTO) costs.
4
4
 
5
- 👉 **[Read the full documentation and integration guide here](https://business.geophrase.com/docs)**
5
+ 👉 **[Full documentation and integration guide](https://business.geophrase.com/docs)**
6
6
 
7
7
  ---
8
8
 
9
- *Note: This package requires the [`@geophrase/core`](https://www.npmjs.com/package/@geophrase/core) peer dependency. If you are not using React, you should use the core package directly.*
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @geophrase/react
13
+ ```
14
+
15
+ `@geophrase/core` is installed automatically as a dependency. `react >= 16.8` is a peer dependency.
16
+
17
+ ## Quick Start
18
+
19
+ ```jsx
20
+ import { useState } from 'react';
21
+ import { useGeophrase } from '@geophrase/react';
22
+
23
+ export default function Checkout() {
24
+ const [result, setResult] = useState(null);
25
+
26
+ const { open } = useGeophrase({
27
+ mode: 'client', // 'client' (default) or 'server'
28
+ key: 'YOUR_API_KEY', // required when mode is 'client'
29
+ theme: 'system', // 'light' | 'dark' | 'system'
30
+ orderId: 'ORD-98765', // your internal reference id
31
+ phone: '9999999999', // pre-fill the customer phone
32
+
33
+ onSuccess: (data) => setResult(data),
34
+ onError: (error) => console.error('Error:', error.message),
35
+ onClose: () => console.log('User dismissed the widget.')
36
+ });
37
+
38
+ return (
39
+ <div>
40
+ <button onClick={open}>Select Delivery Address</button>
41
+ {result && <pre>{JSON.stringify(result, null, 2)}</pre>}
42
+ </div>
43
+ );
44
+ }
45
+ ```
46
+
47
+ ### Next.js (App Router)
48
+
49
+ The component calling `useGeophrase` relies on browser APIs, so it must be a **Client Component** — add `"use client";` at the top of the file.
50
+
51
+ ### TypeScript
52
+
53
+ The hook is fully typed. Import `GeophraseAddress` and `GeophraseToken` from `@geophrase/core` to type `onSuccess` results:
54
+
55
+ ```tsx
56
+ import { GeophraseAddress, GeophraseToken } from '@geophrase/core';
57
+
58
+ const [result, setResult] = useState<GeophraseAddress | GeophraseToken | null>(null);
59
+ ```
60
+
61
+ ## What the hook returns
62
+
63
+ | Value | Description |
64
+ | :--- | :--- |
65
+ | `open()` | Opens the modal. Safe to wire directly to a button's `onClick`. |
66
+ | `close()` | Closes the modal without firing `onSuccess`. |
67
+
68
+ The underlying `Geophrase` instance is created on mount and destroyed on unmount automatically — you do not need to call `destroy()` manually. See the [full docs](https://business.geophrase.com/docs) for the complete configuration reference, security guidance, and response schemas.
package/package.json CHANGED
@@ -1,27 +1,39 @@
1
1
  {
2
2
  "name": "@geophrase/react",
3
- "version": "1.2.0",
4
- "description": "React and Next.js hooks for the Geophrase Connect SDK",
3
+ "version": "1.3.0",
4
+ "description": "React & Next.js hook for the Geophrase address picker. Drop-in widget that captures structured Indian addresses with coordinates to reduce RTO.",
5
+ "license": "MIT",
6
+ "author": "Geophrase",
5
7
  "homepage": "https://business.geophrase.com/docs",
6
8
  "repository": {
7
9
  "type": "git",
8
- "url": "https://github.com/geophrase/geophrase-js.git"
10
+ "url": "git+https://github.com/geophrase/geophrase-js.git"
9
11
  },
10
12
  "bugs": {
11
13
  "url": "https://github.com/geophrase/geophrase-js/issues"
12
14
  },
13
15
  "keywords": [
14
16
  "react",
17
+ "react-hook",
15
18
  "nextjs",
16
19
  "hooks",
20
+ "sdk",
21
+ "widget",
17
22
  "address",
23
+ "address-picker",
24
+ "address-autocomplete",
25
+ "address-verification",
26
+ "delivery-address",
27
+ "pincode",
18
28
  "india",
19
29
  "rto",
20
30
  "ecommerce",
21
31
  "checkout"
22
32
  ],
33
+ "type": "module",
23
34
  "main": "src/index.js",
24
35
  "types": "index.d.ts",
36
+ "sideEffects": false,
25
37
  "files": [
26
38
  "src",
27
39
  "index.d.ts",
@@ -32,6 +44,9 @@
32
44
  "react": ">=16.8.0"
33
45
  },
34
46
  "dependencies": {
35
- "@geophrase/core": "^1.2.0"
47
+ "@geophrase/core": "^1.3.0"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
36
51
  }
37
- }
52
+ }
@@ -23,43 +23,44 @@ export const useGeophrase = (options = {}) => {
23
23
  useEffect(() => {
24
24
  if (typeof window === 'undefined') return;
25
25
 
26
- // 1. Strict Developer Warning
27
- if (!options.key) {
28
- console.warn("Geophrase: 'key' is required to initialize the widget.");
26
+ try {
27
+ geoInstance.current = new Geophrase({
28
+ mode: options.mode,
29
+ theme: options.theme,
30
+ key: options.key,
31
+ orderId: options.orderId,
32
+ phone: options.phone,
33
+ onSuccess: (data) => {
34
+ if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
35
+ },
36
+ onError: (error) => {
37
+ if (savedCallbacks.current.onError) savedCallbacks.current.onError(error);
38
+ },
39
+ onClose: () => {
40
+ if (savedCallbacks.current.onClose) savedCallbacks.current.onClose();
41
+ }
42
+ });
43
+ } catch (error) {
44
+ console.error(error.message);
29
45
  }
30
46
 
31
- geoInstance.current = new Geophrase({
32
- key: options.key,
33
- order_id: options.order_id,
34
- phone: options.phone,
35
- onSuccess: (data) => {
36
- if (savedCallbacks.current.onSuccess) savedCallbacks.current.onSuccess(data);
37
- },
38
- onError: (error) => {
39
- if (savedCallbacks.current.onError) savedCallbacks.current.onError(error);
40
- },
41
- onClose: () => {
42
- if (savedCallbacks.current.onClose) savedCallbacks.current.onClose();
43
- }
44
- });
45
-
46
47
  return () => {
47
48
  if (geoInstance.current) {
48
49
  geoInstance.current.destroy();
49
50
  geoInstance.current = null;
50
51
  }
51
52
  };
52
- }, [options.key, options.order_id, options.phone]);
53
+ // Include mode and theme in the dependency array so the instance rebuilds if they change state
54
+ }, [options.mode, options.theme, options.key, options.orderId, options.phone]);
53
55
 
54
56
  const open = useCallback(() => {
55
57
  if (geoInstance.current) {
56
58
  geoInstance.current.open();
57
59
  } else {
58
- console.warn("Geophrase widget is not ready yet.");
60
+ console.warn("Geophrase widget is not ready yet or failed to initialize.");
59
61
  }
60
62
  }, []);
61
63
 
62
- // 2. Fixed missing close implementation
63
64
  const close = useCallback(() => {
64
65
  if (geoInstance.current) {
65
66
  geoInstance.current.close();