homeflowjs 1.0.59 → 1.0.60
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/hooks/index.js +2 -0
- package/hooks/use-search-from-fragment.js +53 -0
- package/package.json +1 -1
package/hooks/index.js
CHANGED
@@ -7,9 +7,11 @@ import useLoadBranches from './use-load-branches';
|
|
7
7
|
import useLoadPreviousProperties from './use-load-previous-properties';
|
8
8
|
import useRecaptcha from './use-recaptcha';
|
9
9
|
import useScrollTo from './use-scroll-to.hook';
|
10
|
+
import useSearchFromFragment from './use-search-from-fragment';
|
10
11
|
|
11
12
|
export {
|
12
13
|
useDefaultSort,
|
14
|
+
useSearchFromFragment,
|
13
15
|
useGeolocate,
|
14
16
|
useOutsideClick,
|
15
17
|
useOnScreen,
|
@@ -0,0 +1,53 @@
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
2
|
+
import 'regenerator-runtime/runtime';
|
3
|
+
|
4
|
+
const useSearchFromFragment = ({
|
5
|
+
fragment,
|
6
|
+
channel = 'sales',
|
7
|
+
count = 6, // page size
|
8
|
+
placeId,
|
9
|
+
}) => {
|
10
|
+
const [foundProperties, setFoundProperties] = useState(null);
|
11
|
+
const [loading, setLoading] = useState(true);
|
12
|
+
const [error, setError] = useState(null);
|
13
|
+
|
14
|
+
const searchUrl = useCallback(() => {
|
15
|
+
let buildUrl = `/search.ljson?channel=${channel}`;
|
16
|
+
|
17
|
+
if (placeId) buildUrl = buildUrl += `&place_id=${placeId}`
|
18
|
+
if (count) buildUrl = buildUrl += `&count=${count}`
|
19
|
+
|
20
|
+
buildUrl += `&fragment=${fragment}`
|
21
|
+
|
22
|
+
return buildUrl;
|
23
|
+
}, []);
|
24
|
+
|
25
|
+
useEffect(() => {
|
26
|
+
const findProperties = async () => {
|
27
|
+
try {
|
28
|
+
const searchResponse = await fetch(searchUrl());
|
29
|
+
if (!searchResponse.ok) {
|
30
|
+
throw new Error('Failed to fetch properties data');
|
31
|
+
}
|
32
|
+
const { properties } = await searchResponse.json();
|
33
|
+
setFoundProperties(properties);
|
34
|
+
} catch (error) {
|
35
|
+
setError(error);
|
36
|
+
console.error(error);
|
37
|
+
} finally {
|
38
|
+
setLoading(false);
|
39
|
+
}
|
40
|
+
};
|
41
|
+
|
42
|
+
|
43
|
+
if (foundProperties === null) findProperties();
|
44
|
+
}, []);
|
45
|
+
|
46
|
+
return {
|
47
|
+
foundProperties,
|
48
|
+
error,
|
49
|
+
loading,
|
50
|
+
};
|
51
|
+
}
|
52
|
+
|
53
|
+
export default useSearchFromFragment;
|