From 1523dd4b12bcb22bad3a467b528270d8776c3dd4 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Wed, 11 Jul 2018 12:54:39 +0200
Subject: [PATCH] FIX #8101 add autocomplete users in ajax

---
 .../js/angular/plugins/autocomplete.plugin.ts | 34 +++++++++++--------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/apps/maarch_entreprise/js/angular/plugins/autocomplete.plugin.ts b/apps/maarch_entreprise/js/angular/plugins/autocomplete.plugin.ts
index d76410ace45..14def064738 100644
--- a/apps/maarch_entreprise/js/angular/plugins/autocomplete.plugin.ts
+++ b/apps/maarch_entreprise/js/angular/plugins/autocomplete.plugin.ts
@@ -1,8 +1,8 @@
 import { HttpClient } from '@angular/common/http';
 import { ActivatedRoute, Router } from '@angular/router';
 import { FormControl } from '@angular/forms';
-import { Observable } from 'rxjs';
-import { startWith ,  map } from 'rxjs/operators';
+import { Observable, empty } from 'rxjs';
+import { startWith, map, debounceTime, filter, distinctUntilChanged, switchMap } from 'rxjs/operators';
 
 declare const angularGlobals: any;
 
@@ -26,17 +26,21 @@ export class AutoCompletePlugin {
 
         if (target.indexOf('users') != -1) {
             this.userCtrl = new FormControl();
-            this.http.get(this.coreUrl + 'rest/autocomplete/users')
-                .subscribe((data: any) => {
-                    this.userList = data;
-                    this.filteredUsers = this.userCtrl.valueChanges
-                        .pipe(
-                            startWith(''),
-                            map(user => user ? this.autocompleteFilterUser(user) : this.userList.slice())
-                        );
-                }, () => {
-                    location.href = "index.php";
-                });
+            this.userCtrl.valueChanges.pipe(
+                debounceTime(300),
+                filter(value => value.length > 2),
+                distinctUntilChanged(),
+                switchMap(data => this.http.get(this.coreUrl + 'rest/autocomplete/users', { params: { "search": data } }))
+            ).subscribe((response: any) => {
+                if (response.length == 0) {
+                    this.userCtrl.setErrors({'noResult': true})
+                }
+                this.filteredUsers = this.userCtrl.valueChanges
+                    .pipe(
+                        startWith(''),
+                        map(user => user ? this.autocompleteFilterUser(user) : response.slice())
+                    );
+            });
         }
         if (target.indexOf('adminUsers') != -1) {
             this.userCtrl = new FormControl();
@@ -52,8 +56,8 @@ export class AutoCompletePlugin {
                     location.href = "index.php";
                 });
         }
-        
-        if (target.indexOf('statuses')  != -1) {
+
+        if (target.indexOf('statuses') != -1) {
             this.statusCtrl = new FormControl();
             this.http.get(this.coreUrl + 'rest/autocomplete/statuses')
                 .subscribe((data: any) => {
-- 
GitLab