Skip to content

Commit 02450b5

Browse files
author
Jonas Gossens
committed
✨ Add personFinder reducer function
1 parent f4dde74 commit 02450b5

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

examples/react-chayns-personfinder/Example.jsx

+23
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ export default class PersonFinderExample extends PureComponent {
2525
render() {
2626
return (
2727
<div style={{ marginBottom: '300px' }}>
28+
<PersonFinder
29+
ref={(ref) => { this.relationFinder = ref; }}
30+
dynamic
31+
placeholder="Users with reducer: show only persons with an 'e' in the name"
32+
onChange={PersonFinderExample.handleSelect}
33+
reducerFunction={state => new Promise((resolve) => {
34+
console.log(state);
35+
const newState = {
36+
...state,
37+
persons: {
38+
related: state.persons.related.map((person) => {
39+
console.log(person);
40+
return person.firstName.indexOf('e') >= 0 || person.lastName.indexOf('e') >= 0;
41+
}),
42+
unrelated: state.persons.unrelated.filter((person) => {
43+
console.log(person);
44+
return person.firstName.indexOf('e') >= 0 || person.lastName.indexOf('e') >= 0;
45+
}),
46+
},
47+
};
48+
resolve(newState);
49+
})}
50+
/>
2851
<PersonFinder
2952
defaultValue="Smith"
3053
ref={(ref) => { this.relationFinder = ref; }}

src/react-chayns-personfinder/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Then it can be used like in the following example:
4141
| boxClassName | ClassName of the box-overlay | string | |
4242
| uacId | ID of the UAC-Group to search in | number | |
4343
| locationId | LocationId of the UAC-Group to search in | number | |
44+
| reducerFunction | Function to reduce the results (see example) | Function | |
4445

4546
## Clear the PersonFinder
4647
If you want to clear the PersonFinder, you need to have a reference to the ReactElement:

src/react-chayns-personfinder/component/PersonFinder.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class PersonFinder extends Component {
1111
showSites: PropTypes.bool,
1212
uacId: PropTypes.number,
1313
locationId: PropTypes.number,
14+
reducerFunction: PropTypes.func,
1415
};
1516

1617
static defaultProps = {
@@ -19,6 +20,7 @@ export default class PersonFinder extends Component {
1920
showSites: false,
2021
uacId: null,
2122
locationId: null,
23+
reducerFunction: null,
2224
};
2325

2426
personFinder = React.createRef();

src/react-chayns-personfinder/component/PersonFinderData.jsx

+15-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default class PersonFinderData extends Component {
3232
uacId: PropTypes.number,
3333
locationId: PropTypes.number,
3434
parent: PropTypes.instanceOf(Element),
35+
reducerFunction: PropTypes.func,
3536
};
3637

3738
static defaultProps = {
@@ -47,6 +48,7 @@ export default class PersonFinderData extends Component {
4748
uacId: null,
4849
locationId: null,
4950
parent: document.querySelector('.tapp'),
51+
reducerFunction: null,
5052
};
5153

5254
resultList = null;
@@ -180,7 +182,7 @@ export default class PersonFinderData extends Component {
180182
}
181183

182184
const {
183-
persons: enablePersons, sites: enableSites, includeOwn, uacId, locationId,
185+
persons: enablePersons, sites: enableSites, includeOwn, uacId, locationId, reducerFunction,
184186
} = this.props;
185187

186188
const promises = [];
@@ -233,9 +235,14 @@ export default class PersonFinderData extends Component {
233235
sites = { ...sites }; // Forces rerendering
234236
}
235237

238+
let newState = { persons, sites };
239+
240+
if (reducerFunction) {
241+
newState = await reducerFunction(newState);
242+
}
243+
236244
this.setState({
237-
persons,
238-
sites,
245+
...newState,
239246
lazyLoading: clear ? false : lazyLoading,
240247
});
241248
} catch (ex) {
@@ -323,7 +330,11 @@ export default class PersonFinderData extends Component {
323330
hasEntries() {
324331
const { persons, sites, friends } = this.state;
325332

326-
return persons.related.length > 0 || persons.unrelated.length > 0 || sites.related.length > 0 || sites.unrelated.length > 0 || friends.length > 0;
333+
return (persons.related && persons.related.length > 0)
334+
|| (persons.unrelated && persons.unrelated.length > 0)
335+
|| (sites.related && sites.related.length > 0)
336+
|| (sites.unrelated && sites.unrelated.length > 0)
337+
|| (friends && friends.length > 0);
327338
}
328339

329340
renderChildren() {

0 commit comments

Comments
 (0)