@abcagency/hc-ui-components 1.3.17 → 1.3.18

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.
@@ -1,242 +1,242 @@
1
- import React, { createContext, useState, useEffect, useContext } from 'react';
2
-
3
- import { generateFilterOptions, applyFilters, filterListingsByLocation } from '~/util/filterUtil';
4
- import { getStorageObject, setStorageObject } from '~/util/localStorageUtil';
5
-
6
- import { getListingEntities } from "~/services/listingEntityService";
7
- import fetchListings from '~/services/listingAggregatorService';
8
-
9
- const MapListContext = createContext();
10
-
11
- export const useMapList = () => useContext(MapListContext);
12
-
13
- const getQuery = () => {
14
- let query;
15
- //if (!hasQueryInUrl(location)) {
16
- query = typeof window !== 'undefined' ? localStorage.getItem('query') : '';
17
- // }
18
- //else {
19
- // query = filtersFromURL(location).query;
20
- // }
21
- return query;
22
- };
23
-
24
- export const MapListProvider = ({
25
- children,
26
- siteConfig,
27
- resetFilters,
28
- navigateToDetails,
29
- navigateToEasyApply,
30
- Link,
31
- linkFormat,
32
- trackEvent
33
- }) => {
34
- // const location = useLocation();
35
- // const navigate = useNavigate();
36
- const [allListings, setAllListings] = useState(getStorageObject("listings", []));
37
- const [filteredListings, setFilteredListings] = useState([]);
38
- const [loading, setLoading] = useState(false);
39
- const [mapItems, setMapItems] = useState(getStorageObject('mapItems', []));
40
- const [query, setQuery] = useState(() => resetFilters ? null : getQuery());
41
- const [sortSetting, setSortSetting] = useState(getStorageObject('sortSetting', null));
42
- const [listingEntities, setListingEntities] = useState(getStorageObject("listingEntities", null));
43
- const [firstLoad, setFirstLoad] = useState(true);
44
- const [commuteLocation, setCommuteLocation] = useState(getStorageObject('commuteLocation'));
45
- const [selectedFilters, setSelectedFilters] = useState(() => resetFilters ? {} : getStorageObject('selectedFilters', {}));//hasFiltersInURL(location) ? filtersFromURL(location).filters : getStorageObject('selectedFilters', {}));
46
- const [filterOptions, setFilterOptions] = useState();
47
- const [recruiters, setRecruiters] = useState(getStorageObject("recruiters", {}));
48
- const [filterDialogIsOpen, setFilterDialogIsOpen] = useState(false);
49
- const [mobileTab, setMobileTab] = useState("listTab");
50
- const [favorites, setFavorites] = useState([]);
51
- const [filterByFavorites, setFilterByFavorites] = useState(false);
52
-
53
- const setNewFilteredListings = filteredListings =>{
54
- setFilteredListings(filteredListings);
55
- };
56
-
57
- useEffect(() => {
58
- if (!sortSetting) return;
59
- localStorage.setItem('sortSetting', JSON.stringify(sortSetting));
60
- setNewFilteredListings(filteredListings);
61
- }, [sortSetting]);
62
-
63
- useEffect(() => {
64
- const loadedFavorites = JSON.parse(localStorage.getItem('favorites')) || [];
65
- setFavorites(loadedFavorites);
66
- }, []);
67
-
68
- useEffect(() => {
69
- setStorageObject("commuteLocation", commuteLocation);
70
- }, [commuteLocation]);
71
-
72
- useEffect(() => {
73
- if (commuteLocation === null || commuteLocation === '') return;
74
- async function fetchEntities() {
75
- const distinctEntityIds = [
76
- // eslint-disable-next-line no-undef
77
- ...new Set(allListings.map(listing => listing.entityId))
78
- ];
79
- try {
80
- const fetchedEntities = await getListingEntities(
81
- distinctEntityIds,
82
- `${commuteLocation.lat}, ${commuteLocation.lng}`
83
- );
84
- setListingEntities(fetchedEntities);
85
- var newFilteredListings = filteredListings;
86
- for (var i = 0; i < allListings.length; i++) {
87
- if (newFilteredListings[i].entityId != -1) {
88
- newFilteredListings[i].fields.travelTime = fetchedEntities[newFilteredListings[i].entityId].travelTime;
89
- }
90
- }
91
- for (var j = 0; j < newFilteredListings.length; j++) {
92
- if (newFilteredListings[j].entityId != -1) {
93
- newFilteredListings[j].fields.travelTime = fetchedEntities[newFilteredListings[j].entityId].travelTime;
94
- }
95
- }
96
- setNewFilteredListings(newFilteredListings);
97
- } catch (error) {
98
- console.error("Failed to fetch listing entities:", error);
99
- }
100
- }
101
-
102
- fetchEntities();
103
- }, [commuteLocation, allListings, siteConfig.companyId]);
104
-
105
- useEffect(() => {
106
- const handleFetchListings = async () => {
107
- if (!getStorageObject('listings', []).length > 0) {
108
- setLoading(true);
109
- }
110
-
111
- try {
112
- const {
113
- listingsResult,
114
- fetchedRecruiters,
115
- fetchedEntities,
116
- distinctItems
117
- } = await fetchListings(query, siteConfig, commuteLocation);
118
- setAllListings(listingsResult);
119
- setRecruiters(fetchedRecruiters);
120
- setListingEntities(fetchedEntities);
121
- setMapItems(distinctItems);
122
- setStorageObject("mapItems", distinctItems);
123
- setStorageObject("listingEntities", fetchedEntities);
124
- setStorageObject("recruiters", fetchedRecruiters);
125
- setStorageObject("listings", listingsResult);
126
- } catch (error) {
127
- console.log(error);
128
- }
129
- setLoading(false);
130
- };
131
- handleFetchListings();
132
- }, [query, siteConfig]);
133
-
134
- useEffect(() => {
135
- const processListings = () => {
136
- let { filteredListings, mapItems } = applyFilters(
137
- allListings,
138
- selectedFilters,
139
- query,
140
- listingEntities,
141
- favorites,
142
- siteConfig
143
- );
144
- if (filterByFavorites) {
145
- filteredListings = filteredListings.filter(x => favorites.includes(x.id));
146
- }
147
- setNewFilteredListings(filteredListings);
148
- // if (firstLoad && hasFiltersInURL(location)) {
149
- // const { filters } = filtersFromURL(location);
150
- // setSelectedFilters(filters);
151
- // }
152
- if (firstLoad && selectedFilters) {
153
- //updateURLWithFilters(selectedFilters,location, navigate, query);
154
- } else if (Object.keys(selectedFilters).length === 0 && !firstLoad) {
155
- localStorage.removeItem('selectedFilters');
156
- //updateURLWithFilters(selectedFilters,location, navigate, query);
157
- } else if (!firstLoad) {
158
- setStorageObject('selectedFilters', selectedFilters);
159
- //updateURLWithFilters(selectedFilters,location, navigate, query);
160
- }
161
- query != null ? localStorage.setItem('query', query) : localStorage.removeItem('query');
162
- setMapItems(mapItems);
163
-
164
- if (selectedFilters) {
165
- const keys = Object.keys(selectedFilters);
166
- const lastKey = keys[keys.length - 1];
167
- const options = generateFilterOptions(
168
- firstLoad ? allListings : filteredListings,
169
- allListings,
170
- siteConfig,
171
- filterOptions,
172
- lastKey,
173
- favorites
174
- );
175
- if (options) {
176
- setFilterOptions(options);
177
- if (firstLoad) setFirstLoad(false);
178
- }
179
- }
180
- };
181
-
182
- processListings();
183
- }, [selectedFilters, query, listingEntities, filterByFavorites, favorites]);
184
-
185
- const handleFilterListingsByLocation = selectedLocation => {
186
- let { filteredListings } = filterListingsByLocation(
187
- allListings,
188
- selectedLocation,
189
- listingEntities
190
- );
191
-
192
- setNewFilteredListings(filteredListings);
193
- //setMapItems(mapItems);
194
- };
195
-
196
- const handleSettingFavorites = newFavorites => {
197
- if (newFavorites == null) {
198
- localStorage.removeItem('favorites');
199
- } else {
200
- setFavorites(newFavorites);
201
- localStorage.setItem('favorites', JSON.stringify(newFavorites));
202
- }
203
- };
204
-
205
- return (
206
- <MapListContext.Provider value={{
207
- loading,
208
- allListings,
209
- filteredListings,
210
- mapItems,
211
- query,
212
- setNewFilteredListings,
213
- setQuery,
214
- listingEntities,
215
- selectedFilters,
216
- setSelectedFilters,
217
- filterOptions,
218
- recruiters,
219
- handleFilterListingsByLocation,
220
- filterDialogIsOpen,
221
- setFilterDialogIsOpen,
222
- setMobileTab,
223
- mobileTab,
224
- siteConfig,
225
- favorites,
226
- handleSettingFavorites,
227
- setFilterByFavorites,
228
- filterByFavorites,
229
- commuteLocation,
230
- setCommuteLocation,
231
- navigateToDetails,
232
- navigateToEasyApply,
233
- Link,
234
- linkFormat,
235
- sortSetting,
236
- setSortSetting,
237
- trackEvent
238
- }}>
239
- {children}
240
- </MapListContext.Provider>
241
- );
242
- };
1
+ import React, { createContext, useState, useEffect, useContext } from 'react';
2
+
3
+ import { generateFilterOptions, applyFilters, filterListingsByLocation } from '~/util/filterUtil';
4
+ import { getStorageObject, setStorageObject } from '~/util/localStorageUtil';
5
+
6
+ import { getListingEntities } from "~/services/listingEntityService";
7
+ import fetchListings from '~/services/listingAggregatorService';
8
+
9
+ const MapListContext = createContext();
10
+
11
+ export const useMapList = () => useContext(MapListContext);
12
+
13
+ const getQuery = () => {
14
+ let query;
15
+ //if (!hasQueryInUrl(location)) {
16
+ query = typeof window !== 'undefined' ? localStorage.getItem('query') : '';
17
+ // }
18
+ //else {
19
+ // query = filtersFromURL(location).query;
20
+ // }
21
+ return query;
22
+ };
23
+
24
+ export const MapListProvider = ({
25
+ children,
26
+ siteConfig,
27
+ resetFilters,
28
+ navigateToDetails,
29
+ navigateToEasyApply,
30
+ Link,
31
+ linkFormat,
32
+ trackEvent
33
+ }) => {
34
+ // const location = useLocation();
35
+ // const navigate = useNavigate();
36
+ const [allListings, setAllListings] = useState(getStorageObject("listings", []));
37
+ const [filteredListings, setFilteredListings] = useState([]);
38
+ const [loading, setLoading] = useState(false);
39
+ const [mapItems, setMapItems] = useState(getStorageObject('mapItems', []));
40
+ const [query, setQuery] = useState(() => resetFilters ? null : getQuery());
41
+ const [sortSetting, setSortSetting] = useState(getStorageObject('sortSetting', { field: 'position', type: 'asc' }));
42
+ const [listingEntities, setListingEntities] = useState(getStorageObject("listingEntities", null));
43
+ const [firstLoad, setFirstLoad] = useState(true);
44
+ const [commuteLocation, setCommuteLocation] = useState(getStorageObject('commuteLocation'));
45
+ const [selectedFilters, setSelectedFilters] = useState(() => resetFilters ? {} : getStorageObject('selectedFilters', {}));//hasFiltersInURL(location) ? filtersFromURL(location).filters : getStorageObject('selectedFilters', {}));
46
+ const [filterOptions, setFilterOptions] = useState();
47
+ const [recruiters, setRecruiters] = useState(getStorageObject("recruiters", {}));
48
+ const [filterDialogIsOpen, setFilterDialogIsOpen] = useState(false);
49
+ const [mobileTab, setMobileTab] = useState("listTab");
50
+ const [favorites, setFavorites] = useState([]);
51
+ const [filterByFavorites, setFilterByFavorites] = useState(false);
52
+
53
+ const setNewFilteredListings = filteredListings =>{
54
+ setFilteredListings(filteredListings);
55
+ };
56
+
57
+ useEffect(() => {
58
+ if (!sortSetting) return;
59
+ localStorage.setItem('sortSetting', JSON.stringify(sortSetting));
60
+ setNewFilteredListings(filteredListings);
61
+ }, [sortSetting]);
62
+
63
+ useEffect(() => {
64
+ const loadedFavorites = JSON.parse(localStorage.getItem('favorites')) || [];
65
+ setFavorites(loadedFavorites);
66
+ }, []);
67
+
68
+ useEffect(() => {
69
+ setStorageObject("commuteLocation", commuteLocation);
70
+ }, [commuteLocation]);
71
+
72
+ useEffect(() => {
73
+ if (commuteLocation === null || commuteLocation === '') return;
74
+ async function fetchEntities() {
75
+ const distinctEntityIds = [
76
+ // eslint-disable-next-line no-undef
77
+ ...new Set(allListings.map(listing => listing.entityId))
78
+ ];
79
+ try {
80
+ const fetchedEntities = await getListingEntities(
81
+ distinctEntityIds,
82
+ `${commuteLocation.lat}, ${commuteLocation.lng}`
83
+ );
84
+ setListingEntities(fetchedEntities);
85
+ var newFilteredListings = filteredListings;
86
+ for (var i = 0; i < allListings.length; i++) {
87
+ if (newFilteredListings[i].entityId != -1) {
88
+ newFilteredListings[i].fields.travelTime = fetchedEntities[newFilteredListings[i].entityId].travelTime;
89
+ }
90
+ }
91
+ for (var j = 0; j < newFilteredListings.length; j++) {
92
+ if (newFilteredListings[j].entityId != -1) {
93
+ newFilteredListings[j].fields.travelTime = fetchedEntities[newFilteredListings[j].entityId].travelTime;
94
+ }
95
+ }
96
+ setNewFilteredListings(newFilteredListings);
97
+ } catch (error) {
98
+ console.error("Failed to fetch listing entities:", error);
99
+ }
100
+ }
101
+
102
+ fetchEntities();
103
+ }, [commuteLocation, allListings, siteConfig.companyId]);
104
+
105
+ useEffect(() => {
106
+ const handleFetchListings = async () => {
107
+ if (!getStorageObject('listings', []).length > 0) {
108
+ setLoading(true);
109
+ }
110
+
111
+ try {
112
+ const {
113
+ listingsResult,
114
+ fetchedRecruiters,
115
+ fetchedEntities,
116
+ distinctItems
117
+ } = await fetchListings(query, siteConfig, commuteLocation);
118
+ setAllListings(listingsResult);
119
+ setRecruiters(fetchedRecruiters);
120
+ setListingEntities(fetchedEntities);
121
+ setMapItems(distinctItems);
122
+ setStorageObject("mapItems", distinctItems);
123
+ setStorageObject("listingEntities", fetchedEntities);
124
+ setStorageObject("recruiters", fetchedRecruiters);
125
+ setStorageObject("listings", listingsResult);
126
+ } catch (error) {
127
+ console.log(error);
128
+ }
129
+ setLoading(false);
130
+ };
131
+ handleFetchListings();
132
+ }, [query, siteConfig]);
133
+
134
+ useEffect(() => {
135
+ const processListings = () => {
136
+ let { filteredListings, mapItems } = applyFilters(
137
+ allListings,
138
+ selectedFilters,
139
+ query,
140
+ listingEntities,
141
+ favorites,
142
+ siteConfig
143
+ );
144
+ if (filterByFavorites) {
145
+ filteredListings = filteredListings.filter(x => favorites.includes(x.id));
146
+ }
147
+ setNewFilteredListings(filteredListings);
148
+ // if (firstLoad && hasFiltersInURL(location)) {
149
+ // const { filters } = filtersFromURL(location);
150
+ // setSelectedFilters(filters);
151
+ // }
152
+ if (firstLoad && selectedFilters) {
153
+ //updateURLWithFilters(selectedFilters,location, navigate, query);
154
+ } else if (Object.keys(selectedFilters).length === 0 && !firstLoad) {
155
+ localStorage.removeItem('selectedFilters');
156
+ //updateURLWithFilters(selectedFilters,location, navigate, query);
157
+ } else if (!firstLoad) {
158
+ setStorageObject('selectedFilters', selectedFilters);
159
+ //updateURLWithFilters(selectedFilters,location, navigate, query);
160
+ }
161
+ query != null ? localStorage.setItem('query', query) : localStorage.removeItem('query');
162
+ setMapItems(mapItems);
163
+
164
+ if (selectedFilters) {
165
+ const keys = Object.keys(selectedFilters);
166
+ const lastKey = keys[keys.length - 1];
167
+ const options = generateFilterOptions(
168
+ firstLoad ? allListings : filteredListings,
169
+ allListings,
170
+ siteConfig,
171
+ filterOptions,
172
+ lastKey,
173
+ favorites
174
+ );
175
+ if (options) {
176
+ setFilterOptions(options);
177
+ if (firstLoad) setFirstLoad(false);
178
+ }
179
+ }
180
+ };
181
+
182
+ processListings();
183
+ }, [selectedFilters, query, listingEntities, filterByFavorites, favorites]);
184
+
185
+ const handleFilterListingsByLocation = selectedLocation => {
186
+ let { filteredListings } = filterListingsByLocation(
187
+ allListings,
188
+ selectedLocation,
189
+ listingEntities
190
+ );
191
+
192
+ setNewFilteredListings(filteredListings);
193
+ //setMapItems(mapItems);
194
+ };
195
+
196
+ const handleSettingFavorites = newFavorites => {
197
+ if (newFavorites == null) {
198
+ localStorage.removeItem('favorites');
199
+ } else {
200
+ setFavorites(newFavorites);
201
+ localStorage.setItem('favorites', JSON.stringify(newFavorites));
202
+ }
203
+ };
204
+
205
+ return (
206
+ <MapListContext.Provider value={{
207
+ loading,
208
+ allListings,
209
+ filteredListings,
210
+ mapItems,
211
+ query,
212
+ setNewFilteredListings,
213
+ setQuery,
214
+ listingEntities,
215
+ selectedFilters,
216
+ setSelectedFilters,
217
+ filterOptions,
218
+ recruiters,
219
+ handleFilterListingsByLocation,
220
+ filterDialogIsOpen,
221
+ setFilterDialogIsOpen,
222
+ setMobileTab,
223
+ mobileTab,
224
+ siteConfig,
225
+ favorites,
226
+ handleSettingFavorites,
227
+ setFilterByFavorites,
228
+ filterByFavorites,
229
+ commuteLocation,
230
+ setCommuteLocation,
231
+ navigateToDetails,
232
+ navigateToEasyApply,
233
+ Link,
234
+ linkFormat,
235
+ sortSetting,
236
+ setSortSetting,
237
+ trackEvent
238
+ }}>
239
+ {children}
240
+ </MapListContext.Provider>
241
+ );
242
+ };