homeflowjs 0.13.29 → 0.13.31

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.
@@ -155,7 +155,9 @@ export const loadPage = (payload) => (dispatch, getState) => {
155
155
  { ...property, resultPage: newSearch.page }
156
156
  ));
157
157
  newProperties = [...addedProperties, ...newProperties];
158
+ dispatch(setSearch(newSearch));
158
159
  dispatch(setProperties(newProperties));
160
+ dispatch(setPagination(json.pagination));
159
161
  dispatch(setLoading({ properties: false }));
160
162
  }
161
163
  });
package/hooks/index.js CHANGED
@@ -4,6 +4,7 @@ import useOutsideClick from './use-outside-click';
4
4
  import { useOnScreen } from './use-on-screen';
5
5
  import usePropertyInfiniteScroll from './use-property-inifinite-scroll';
6
6
  import useLoadBranches from './use-load-branches';
7
+ import useLoadPreviousProperties from './use-load-previous-properties';
7
8
 
8
9
  export {
9
10
  useDefaultSort,
@@ -12,4 +13,5 @@ export {
12
13
  useOnScreen,
13
14
  usePropertyInfiniteScroll,
14
15
  useLoadBranches,
16
+ useLoadPreviousProperties,
15
17
  };
@@ -0,0 +1,49 @@
1
+ import { useState, useEffect } from 'react';
2
+ import { useDispatch, useSelector } from 'react-redux';
3
+
4
+ import { loadPage } from '../actions/properties.actions';
5
+ import { addSearchToLocalStorage } from '../app/user-history';
6
+ import { updatePageInURL } from '../properties/property-utils/property-utils';
7
+
8
+ const useLoadPreviousProperties = () => {
9
+ const loadingProperties = useSelector((state) => state.app.loading?.properties);
10
+ const currentSearch = useSelector((state) => state.search?.currentSearch);
11
+ const [currentTopPage, setCurrentTopPage] = useState();
12
+ const dispatch = useDispatch();
13
+
14
+ const findInitialTopPage = () => {
15
+ const pathSegments = window.location.pathname.split('/');
16
+ const pageSegment = pathSegments.find((segment) => segment.match(/page-\d+/));
17
+ if (!pageSegment) return null;
18
+
19
+ const pageNumber = pageSegment.match(/\d+/);
20
+
21
+ if (pageNumber) {
22
+ return parseInt(pageNumber, 10);
23
+ }
24
+ };
25
+
26
+ useEffect(() => {
27
+ setCurrentTopPage(findInitialTopPage());
28
+ }, []);
29
+
30
+ const loadPreviousProperties = () => {
31
+ const currentFirstPage = Number(document.querySelector('[data-page-marker]').dataset?.pageMarker);
32
+ if (currentFirstPage > 1) {
33
+ dispatch(loadPage(currentFirstPage - 1))
34
+ .then(() => {
35
+ addSearchToLocalStorage(currentSearch);
36
+ updatePageInURL(currentFirstPage - 1);
37
+ setCurrentTopPage(currentFirstPage - 1);
38
+ });
39
+ }
40
+ };
41
+
42
+ return {
43
+ loadPreviousProperties,
44
+ hasPreviousPage: currentTopPage && currentTopPage !== 1,
45
+ loadingProperties,
46
+ };
47
+ };
48
+
49
+ export default useLoadPreviousProperties;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "0.13.29",
3
+ "version": "0.13.31",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",
@@ -1,9 +1,9 @@
1
1
  import React, { useEffect, useRef } from 'react';
2
- import { connect } from 'react-redux';
2
+ import { connect, useSelector } from 'react-redux';
3
3
  import PropTypes from 'prop-types';
4
4
 
5
5
  import { propertiesByPage, updatePageInURL } from '../property-utils/property-utils';
6
- import { usePropertyInfiniteScroll } from '../../hooks';
6
+ import { usePropertyInfiniteScroll, useLoadPreviousProperties } from '../../hooks';
7
7
 
8
8
  const ConditionalWrapper = ({ condition, wrapper, children }) => (
9
9
  condition ? wrapper(children) : children
@@ -17,10 +17,21 @@ const PropertiesDisplay = ({
17
17
  InfiniteScrollLoader,
18
18
  inserts,
19
19
  noResultsMessage,
20
+ includePreviousBtn,
21
+ previousBtnClasses,
20
22
  ...other
21
23
  }) => {
24
+ const propertiesPagination = useSelector((state) => state.properties?.pagination);
22
25
  const infiniteScrollRef = useRef();
23
- const { loadingProperties, hasNextPage } = usePropertyInfiniteScroll(infiniteScrollRef);
26
+ const {
27
+ loadingProperties: loadingNextProperties,
28
+ hasNextPage,
29
+ } = usePropertyInfiniteScroll(infiniteScrollRef);
30
+ const {
31
+ loadPreviousProperties,
32
+ hasPreviousPage,
33
+ loadingProperties: loadingPreviousProperties,
34
+ } = useLoadPreviousProperties();
24
35
 
25
36
  useEffect(() => {
26
37
  const pageMarkers = document.querySelectorAll('[data-page-marker]');
@@ -48,7 +59,18 @@ const PropertiesDisplay = ({
48
59
 
49
60
  const addWrapper = displayType === 'list';
50
61
 
51
- const items = propertiesByPage(properties).map((page, i) => (
62
+ const showPreviousBtn = includePreviousBtn && hasPreviousPage && !loadingPreviousProperties;
63
+ const showPreviousLoader = includePreviousBtn
64
+ && InfiniteScrollLoader
65
+ && hasPreviousPage
66
+ && loadingPreviousProperties;
67
+ const showScrollRef = infiniteScroll && hasNextPage;
68
+ const showScrollLoader = infiniteScroll
69
+ && InfiniteScrollLoader
70
+ && hasNextPage
71
+ && loadingNextProperties;
72
+
73
+ const items = propertiesByPage(properties, propertiesPagination).map((page, i) => (
52
74
  <ConditionalWrapper
53
75
  key={i}
54
76
  condition={addWrapper}
@@ -88,13 +110,25 @@ const PropertiesDisplay = ({
88
110
 
89
111
  return (
90
112
  <>
113
+ {showPreviousBtn && (
114
+ <button
115
+ type="button"
116
+ onClick={loadPreviousProperties}
117
+ className={previousBtnClasses}
118
+ >
119
+ Load Previous
120
+ </button>
121
+ )}
122
+ {showPreviousLoader && (
123
+ <InfiniteScrollLoader />
124
+ )}
91
125
  <div className={`hf-property-results hf-property-results__${displayType}`}>
92
126
  {items}
93
127
  </div>
94
- {infiniteScroll && hasNextPage && (
128
+ {showScrollRef && (
95
129
  <div ref={infiniteScrollRef} />
96
130
  )}
97
- {infiniteScroll && InfiniteScrollLoader && hasNextPage && loadingProperties && (
131
+ {showScrollLoader && (
98
132
  <InfiniteScrollLoader />
99
133
  )}
100
134
  </>
@@ -661,6 +661,8 @@ export default class DraggableMap {
661
661
 
662
662
  if (json.properties) {
663
663
  this.setSearchResponse(json);
664
+ } else {
665
+ store.dispatch(setProperties([]));
664
666
  }
665
667
  })
666
668
  }
@@ -30,9 +30,9 @@ export const updatePageInURL = (currentPage) => {
30
30
  if (currentPage === 1 && !pathname.includes('page-')) return null;
31
31
 
32
32
  if (currentPage === 1) {
33
- newPath = pathname.replace(/page-./, '');
33
+ newPath = pathname.replace(/page-\d+/, '');
34
34
  } else if (pathname.includes('page-')) {
35
- newPath = pathname.replace(/page-./, `page-${currentPage}`);
35
+ newPath = pathname.replace(/page-\d+/, `page-${currentPage}`);
36
36
  } else {
37
37
  newPath = pathname[pathname.length - 1] === '/' ? pathname : `${pathname}/`;
38
38
  newPath = `${newPath}page-${currentPage}`;
@@ -63,13 +63,15 @@ export const handleScroll = (infinite) => {
63
63
  });
64
64
  };
65
65
 
66
- export const propertiesByPage = (properties) => {
66
+ export const propertiesByPage = (properties, pagination) => {
67
67
  // sort properties per-page into 2d array
68
68
  const properties2d = [];
69
69
 
70
70
  for (let i = 0; i < properties.length; i++) {
71
71
  const property = properties[i];
72
- if (!property.resultPage) property.resultPage = 1;
72
+ if (!property.resultPage) {
73
+ property.resultPage = pagination?.current_page ? pagination.current_page : 1;
74
+ }
73
75
 
74
76
  if (!properties2d[property.resultPage - 1]) {
75
77
  properties2d[property.resultPage - 1] = [property];