@chayns-components/person-finder
Version:
A set of beautiful React components for developing your own applications with chayns.
145 lines • 4.45 kB
JavaScript
import { PersonFinderFilterTypes } from '../types/personFinder';
import { getPersons } from '../api/person/get';
import { getSites } from '../api/site/get';
import { convertPersonEntry, convertSiteEntry } from './convert';
import { getUser } from 'chayns-api';
export const getGroupName = key => {
const names = {
person: 'Personen',
site: 'Sites',
uac: 'UAC Gruppen'
};
return names[key];
};
export const isSiteEntry = entry => 'name' in entry && !('firstName' in entry);
export const filterDataByKeys = (data = {}, keys = [], options = {}) => {
const filterSingle = entry => {
let filteredEntries = entry.entries;
const {
excludedEntryIds
} = options;
if (Array.isArray(excludedEntryIds) && excludedEntryIds.length > 0) {
filteredEntries = filteredEntries.filter(e => !excludedEntryIds.includes(e.id));
}
const excludedCount = entry.entries.length - filteredEntries.length;
return {
...entry,
entries: filteredEntries,
count: Math.max(0, entry.count - excludedCount)
};
};
const relevantKeys = keys.length > 0 ? keys : Object.keys(data);
return relevantKeys.reduce((acc, key) => {
const original = data[key] ?? {
searchString: '',
count: 0,
skip: 0,
entries: []
};
const filteredEntry = filterSingle(original);
if (options.shouldShowOwnUser && key === PersonFinderFilterTypes.PERSON) {
const user = getUser();
if (typeof user?.personId === 'string') {
// Ensure that the own user is always included in the person filter
const ownUserEntry = {
commonSites: 0,
firstName: user.firstName ?? '',
id: user.personId,
isVerified: false,
lastName: user.lastName ?? '',
type: PersonFinderFilterTypes.PERSON
};
const fullName = `${ownUserEntry.firstName} ${ownUserEntry.lastName}`.trim();
if (original.searchString.length < 3 || fullName.toLowerCase().includes(original.searchString.toLowerCase())) {
filteredEntry.entries = [ownUserEntry, ...filteredEntry.entries];
filteredEntry.count += 1; // Increment count to account for the own user
}
}
}
return {
...acc,
[key]: filteredEntry
};
}, {});
};
export const capitalizeFirstLetter = str => str.charAt(0).toUpperCase() + str.slice(1);
export const destructureData = (data, filterType) => ({
count: data?.[filterType]?.count ?? 0,
skip: data?.[filterType]?.skip ?? 0,
searchString: data?.[filterType]?.searchString ?? '',
entries: data?.[filterType]?.entries ?? []
});
export const loadData = async ({
skipMap,
searchString,
filter
}) => {
const promises = filter.map(async filterType => {
const skip = skipMap[filterType] ?? 0;
if (filterType === PersonFinderFilterTypes.PERSON) {
if (searchString.length <= 2) {
return {
key: PersonFinderFilterTypes.PERSON,
value: {
searchString,
count: 0,
skip: 0,
entries: []
}
};
}
const data = await getPersons({
search: searchString,
skip
});
return {
key: PersonFinderFilterTypes.PERSON,
value: {
searchString,
count: data?.count ?? 0,
skip: skip + (data?.list?.length ?? 0),
entries: convertPersonEntry(data?.list ?? [])
}
};
}
if (filterType === PersonFinderFilterTypes.SITE) {
if (searchString.length <= 2) {
return {
key: PersonFinderFilterTypes.SITE,
value: {
searchString,
count: 0,
skip: 0,
entries: []
}
};
}
const data = await getSites({
search: searchString,
skip
});
const filteredList = data?.list.filter(({
siteId
}) => siteId !== null);
return {
key: PersonFinderFilterTypes.SITE,
value: {
searchString,
count: data?.count ?? 0,
skip: skip + (filteredList?.length ?? 0),
entries: convertSiteEntry(filteredList ?? [])
}
};
}
return null;
});
const results = await Promise.all(promises);
return results.reduce((acc, item) => {
if (!item) return acc;
return {
...acc,
[item.key]: item.value
};
}, {});
};
//# sourceMappingURL=personFinder.js.map