-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a51c29
commit f7c8e3d
Showing
4 changed files
with
168 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import isNil from 'lodash/isNil'; | ||
import get from 'lodash/get'; | ||
|
||
import { SEARCH_PARAMETER } from '../AcqList'; | ||
|
||
const normalizeValue = (value) => value.toString().trim().toLowerCase(); | ||
|
||
/** | ||
* Sorts and filters an array of objects. | ||
* @param { Object } config - Configuration for mapping and query fields. | ||
* @param { Object } activeFilters - Filters to apply to the array elements. | ||
* @param { Array } items - Array of objects to filter and sort. | ||
* @returns { Array } - Filtered and sorted array of objects. | ||
*/ | ||
export const filterAndSort = (config, activeFilters, items) => { | ||
const { | ||
sorting, | ||
sortingDirection, | ||
...filters | ||
} = activeFilters; | ||
|
||
const filteredItems = items.filter(item => { | ||
return Object.entries(filters) | ||
.filter(entry => !isNil(entry[1])) | ||
.every(([key, value]) => { | ||
const itemKey = config.filterMap?.[key] || key; | ||
|
||
if (Array.isArray(value)) { | ||
return value.includes(item[itemKey]); | ||
} | ||
|
||
if (key === SEARCH_PARAMETER) { | ||
return config.queryIndexes.some(field => ( | ||
normalizeValue(get(item, field, '')).includes(normalizeValue(value)) | ||
)); | ||
} | ||
|
||
return get(item, itemKey, '').includes(value); | ||
}); | ||
}); | ||
|
||
if (sorting) { | ||
filteredItems.sort((a, b) => { | ||
const pathToField = config.sortingMap?.[sorting] || sorting; | ||
|
||
const valueA = normalizeValue(get(a, pathToField, '')); | ||
const valueB = normalizeValue(get(b, pathToField, '')); | ||
|
||
return sortingDirection === 'descending' ? valueB.localeCompare(valueA) : valueA.localeCompare(valueB); | ||
}); | ||
} | ||
|
||
return filteredItems; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,62 @@ | ||
import omit from 'lodash/omit'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { useLocations } from '../hooks'; | ||
import { FILTERS_CONFIG } from './configs'; | ||
import { filterAndSort } from './filterAndSort'; | ||
|
||
const hydrateLocations = ( | ||
locations, | ||
selectedLocations, | ||
institutionsMap, | ||
campusessMap, | ||
librariesMap, | ||
) => { | ||
return locations.map(location => ({ | ||
...location, | ||
isAssigned: Boolean(selectedLocations[location.id]), | ||
institution: institutionsMap[location.institutionId], | ||
campus: campusessMap[location.campusId], | ||
library: librariesMap[location.libraryId], | ||
})); | ||
}; | ||
|
||
const dehydrateLocations = (hydratedLocations) => { | ||
return hydratedLocations.map((location) => omit(location, ['isAssigned'])); | ||
}; | ||
|
||
export const useLocationsRecords = ({ | ||
filters, | ||
selectedLocations, | ||
institutionsMap, | ||
campusessMap, | ||
librariesMap, | ||
}) => { | ||
const [isProcessing, setIsProcessing] = useState(false); | ||
const [locationRecords, setLocationRecords] = useState([]); | ||
const { locations, isLoading: isLocationsLoading } = useLocations(); | ||
|
||
export const useLocationsRecords = () => { | ||
const { | ||
useEffect(() => { | ||
Promise.resolve(true) | ||
.then(setIsProcessing) | ||
.then(() => hydrateLocations(locations, selectedLocations, institutionsMap, campusessMap, librariesMap)) | ||
.then((items) => filterAndSort(FILTERS_CONFIG, filters, items)) | ||
.then(dehydrateLocations) | ||
.then(setLocationRecords) | ||
.then(() => setIsProcessing(false)); | ||
}, [ | ||
filters, | ||
locations, | ||
isLoading, | ||
} = useLocations(); | ||
selectedLocations, | ||
institutionsMap, | ||
campusessMap, | ||
librariesMap, | ||
]); | ||
|
||
const isLoading = isLocationsLoading || isProcessing; | ||
|
||
return { | ||
locations, | ||
locations: locationRecords, | ||
isLoading, | ||
} | ||
} |