fixed search

master
Max 5 years ago
parent 05008da865
commit 51ae0a81af

@ -1,29 +1,23 @@
<div id="search"> <div id="search">
<!--<mat-toolbar><span>Search</span></mat-toolbar>--> <mat-toolbar>
<mat-card > <mat-form-field id="input"
<mat-card-content> floatLabel="never" >
<mat-form-field id="input"> <input matInput #searchWord
<textarea matInput #searchWord type="search" placeholder="search something"
mat-autosize="false" [ngModel]="searchWord.value"
matAutosizeMaxRows="1" (ngModelChange)="search(searchWord.value)">
placeholder="search something" <button mat-button matSuffix mat-icon-button>
[ngModel]="searchWord.value" <mat-icon>search </mat-icon>
(ngModelChange)="search(searchWord.value)"></textarea> </button>
<button mat-button matSuffix mat-icon-button> </mat-form-field>
<mat-icon>search </mat-icon> <!--<mat-form-field id="category-chooser">
</button> <mat-label>category</mat-label>
</mat-form-field> <mat-select value="user" #field (selectionChange)="changeCategory($event.value)">
<mat-form-field id="category-chooser"> <mat-option value="user">user</mat-option>
<mat-label>category</mat-label> <mat-option value="group" disabled>group</mat-option>
<mat-select value="username" #field (selectionChange)="changeCategory($event.value)"> </mat-select>
<mat-option value="username">username</mat-option> </mat-form-field>-->
<mat-option value="handle">handle</mat-option> </mat-toolbar>
<mat-option value="groupname" disabled>groupname</mat-option>
<mat-option value="grouphandle" disabled>grouphandle</mat-option>
</mat-select>
</mat-form-field>
</mat-card-content>
</mat-card>
<div id="friendlist"> <div id="friendlist">
<!--<div class="frienditem" *ngFor="let friend of friends" <!--<div class="frienditem" *ngFor="let friend of friends"
[class.selected]="friend === selectedFriend" (click)="showFriendProfile(friend)"> [class.selected]="friend === selectedFriend" (click)="showFriendProfile(friend)">

@ -4,7 +4,7 @@
height: 100% height: 100%
#input #input
min-width: 60% width: 100%
padding-left: 0.5em padding-left: 0.5em
padding-right: 0.5em padding-right: 0.5em

@ -12,7 +12,7 @@ import { Router } from '@angular/router';
}) })
export class SearchComponent implements OnInit { export class SearchComponent implements OnInit {
searchValue = ' '; searchValue = ' ';
category = 'username'; category = 'user';
foundUsers: Array<User>; foundUsers: Array<User>;
constructor(private searchService: SearchService, private http: Http, private router: Router) { } constructor(private searchService: SearchService, private http: Http, private router: Router) { }
@ -27,29 +27,23 @@ export class SearchComponent implements OnInit {
search(searchWord: string) { search(searchWord: string) {
this.foundUsers = Array<User>(); this.foundUsers = Array<User>();
this.searchValue = searchWord; this.searchValue = searchWord;
if (searchWord) { if (searchWord) { // if not null or empty
if (this.category === 'username') { if (this.category === 'user') {
this.findUserByName(searchWord); this.findUser(searchWord);
} else if (this.category === 'handle') { } else if (this.category === 'groupe') {
this.findUserByHandle(searchWord); // this.findUserByHandle(searchWord);
console.log('search group');
} }
} }
} }
findUserByName(name: String) { findUser(name: String) {
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.searchService.buildJsonName(name)) this.http.post(environment.graphQLUrl, this.searchService.buildJsonUser(name))
.subscribe(response => {
this.foundUsers = this.searchService.renderUsers(response.json());
});
}
findUserByHandle(name: String) {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.searchService.buildJsonHandle(name))
.subscribe(response => { .subscribe(response => {
console.log('response received');
console.log(response);
this.foundUsers = this.searchService.renderUsers(response.json()); this.foundUsers = this.searchService.renderUsers(response.json());
}); });
} }

@ -14,34 +14,9 @@ export class SearchService {
constructor(private http: Http, private data: DatasharingService, private router: Router) { constructor(private http: Http, private data: DatasharingService, private router: Router) {
} }
public findUserByName(name: String): Array<User> {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.buildJsonName(name))
.subscribe(response => {
this.users = this.renderUsers(response.json());
});
return this.users;
}
public findUserByHandle(handle: string) {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
return this.http.post(environment.graphQLUrl, this.buildJsonHandle(handle))
.subscribe(response => {
console.log(response.text());
}
);
}
public renderUsers(pResponse: any): Array<User> { public renderUsers(pResponse: any): Array<User> {
const users = new Array<User>(); const users = new Array<User>();
if (pResponse.data.findUser === 'null') { for (const user of pResponse.data.search.users) {
console.log('no user found');
return null;
} else {
for (const user of pResponse.data.findUser) {
const pUser = new User(); const pUser = new User();
pUser.profilePicture = user.profilePicture; pUser.profilePicture = user.profilePicture;
pUser.username = user.name; pUser.username = user.name;
@ -53,21 +28,22 @@ export class SearchService {
users.push(pUser); users.push(pUser);
} }
return users; return users;
}
} }
public buildJsonName(name_: String): any { public buildJsonUser(name_: String): any {
const body = { const body = {
query: `query($name: String) { query: `query($name: String!) {
findUser(name: $name, first: 100, offset: 0) { search(query:$name, first: 100, offset: 0) {
profilePicture, users{
name, profilePicture,
id, name,
handle, id,
points, handle,
level, points,
friends { level,
id friends {
id
}
} }
} }
}` }`
@ -77,25 +53,4 @@ export class SearchService {
}; };
return body; return body;
} }
public buildJsonHandle(handle_: String): any {
const body = {
query: `query($handle: String) {
findUser(handle: $handle, first: 100, offset: 0) {
profilePicture,
name,
id,
handle,
points,
level,
friends {
id
}
}
}`
, variables: {
handle: handle_
}
};
return body;
}
} }

Loading…
Cancel
Save