Skip to content

Commit

Permalink
Merge pull request fossasia#730 from simsausaurabh/image
Browse files Browse the repository at this point in the history
Fixes fossasia#729: Fix slow loading and high frequency of api call
  • Loading branch information
hemantjadon committed May 31, 2018
2 parents 9197ce3 + ae5e7e1 commit 7eaf03f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 30 deletions.
8 changes: 1 addition & 7 deletions src/app/effects/api-user-search.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'rxjs/add/operator/takeUntil';
import { UserService } from '../services';
import * as userApiAction from '../actions/user-api';
import { Query } from '../models';

/**
* Effects offer a way to isolate and easily test side-effects within your
* application. StateUpdates is an observable of the latest state and
Expand All @@ -28,7 +27,6 @@ import { Query } from '../models';
* At the end, what’s really happening is `ngrx/effects` is an `action generator` that dispatches
* a `new action` as a result of a different action.
*/

@Injectable()
export class ApiUserSearchEffects {

Expand All @@ -40,10 +38,7 @@ export class ApiUserSearchEffects {
.map((action: userApiAction.UserSearchAction) => action.payload)
.switchMap(query => {
const nextSearch$ = this.actions$.ofType(userApiAction.ActionTypes.USER_SEARCH).skip(1);

const follow_count = 10;

return this.apiUserService.fetchQuery(query.screen_name, follow_count)
return this.apiUserService.fetchQuery(query.screen_name)
.takeUntil(nextSearch$)
.map(response => new userApiAction.UserSearchCompleteSuccessAction(response))
.catch(() => of(new userApiAction.UserSearchCompleteFailAction('')));
Expand All @@ -53,5 +48,4 @@ export class ApiUserSearchEffects {
private actions$: Actions,
private apiUserService: UserService
) { }

}
2 changes: 0 additions & 2 deletions src/app/feed/feed.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[doCloseSuggestBox$]="doCloseSuggestBox$()"
(searchEvent)="search($event)"
(relocateEvent)="relocateURL($event)"></feed-header>

<div *ngIf="(isSearching$ | async)">
<div class="searching">
<!-- Hook Up the Material Design Progress Bar. -->
Expand All @@ -15,7 +14,6 @@
</div>
</div>
<br>

<div *ngIf="!(isSearching$ | async)" class="feed-wrapper">
<div *ngIf="(areResultsAvailable$ | async)" class="wrapper feed-results">
<div *ngFor="let item of (apiResponseResults$ | async); let i = index ">
Expand Down
15 changes: 6 additions & 9 deletions src/app/feed/user-info-box/user-info-box.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<div *ngIf="(!isUserResponseLoading)" class="wrapper">
<div class="image-gallery">
<div class="profile-banner-image">
<img src="{{ apiResponseUser.profile_banner_url }}/600x200"/>
<img class="banner" src="{{ apiResponseUser.profile_banner_url }}"/>
</div>
<div class="profile-image">
<img src="{{ apiResponseUser.profile_image_url }}"/>
</div>
</div>
<div class="description">
<div>
<div class="heading">
<h2>{{ apiResponseUser.name }}</h2>
<span>@{{ apiResponseUser.screen_name }}</span>
</div>
<div class="desc">
<div class="desc" *ngIf="apiResponseUser.description">
<feed-linker
[text]="apiResponseUser.description"
[useAll]="true"></feed-linker>
Expand Down Expand Up @@ -45,15 +45,15 @@ <h2>{{ apiResponseUser.name }}</h2>
</li>
<li class="list-item">
<span class="item-label">Website:</span>
<span class="item-content" *ngIf="apiResponseUser.entities.url"><a href="{{ apiResponseUser.entities.url.urls[0].url }}">{{ apiResponseUser.entities.url.urls[0].expanded_url }}</a></span>
<span class="item-content" *ngIf="apiResponseUser.entities"><a *ngIf="apiResponseUser.entities.url" href="{{ apiResponseUser.entities.url.urls[0].url }}">{{ apiResponseUser.entities.url.urls[0].expanded_url }}</a></span>
</li>
</ul>
</div>
</div>
<div class="followers gallery" *ngIf="apiResponseUserFollowers.length > 0">
<h3>Followers</h3>
<ul class="gallery-container">
<li class="gallery-item" *ngFor="let item of (sortedApiResponseUserFollowers.slice(0,12)); let i = index ">
<li class="gallery-item" *ngFor="let item of (sortedApiResponseUserFollowers.slice(0,3)); let i = index ">
<a [routerLink]="['/search']" [queryParams]="{ query : 'from:' + item.screen_name }">
<div class="item-image">
<img src="{{ item.profile_image_url }}"/>
Expand All @@ -76,7 +76,7 @@ <h3>Followers</h3>
<div class="following gallery" *ngIf="apiResponseUserFollowing.length > 0">
<h3>Following</h3>
<ul class="gallery-container">
<li class="gallery-item" *ngFor="let item of (sortedApiResponseUserFollowing.slice(0,12)); let i = index ">
<li class="gallery-item" *ngFor="let item of (sortedApiResponseUserFollowing.slice(0,3)); let i = index ">
<a [routerLink]="['/search']" [queryParams]="{ query : 'from:' + item.screen_name }">
<div class="item-image">
<img src="{{ item.profile_image_url }}"/>
Expand All @@ -98,6 +98,3 @@ <h3>Following</h3>
</div>
</div>
</div>



4 changes: 3 additions & 1 deletion src/app/feed/user-info-box/user-info-box.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
}
}

.description {
.banner {
height: 200px;
width: 600px;
}

.heading {
Expand Down
5 changes: 1 addition & 4 deletions src/app/services/search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ describe('Service: Search', () => {
`&startRecord=${searchServiceConfig.startRecord}` +
`&fields=created_at,screen_name,mentions,hashtags&limit=10`);
});

service
.fetchQuery(query, searchServiceConfig)
.subscribe((res) => {
expect(res).toEqual(result);
done();
});
});
});

});

1 change: 1 addition & 0 deletions src/app/services/suggest.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('Service: Suggest', () => {
expect(connection.request.url).toEqual(
`https://api.loklak.org/api/suggest.json` +
`?q=${query}` +
'&count=4' +
`&callback=JSONP_CALLBACK` +
`&minified=true` +
`&order=desc` +
Expand Down
3 changes: 2 additions & 1 deletion src/app/services/suggest.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class SuggestService {
private static minified_results = true;
private static order = 'desc';
private static orderby = 'query_count';

private static count = '4';

constructor(
private jsonp: Jsonp
Expand All @@ -24,6 +24,7 @@ export class SuggestService {
public fetchQuery(query: string): Observable<SuggestResponse> {
const searchParams = new URLSearchParams();
searchParams.set('q', query);
searchParams.set('count', SuggestService.count);
searchParams.set('callback', 'JSONP_CALLBACK');
searchParams.set('minified', SuggestService.minified_results.toString());
searchParams.set('order', SuggestService.order);
Expand Down
4 changes: 2 additions & 2 deletions src/app/services/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Service: UserSearch', () => {

const result = MockApiResponseResult;
const user = 'Fossasia';
const follow_count = 10;
const follow_count = 4;


it('should create an instance of user search service',
Expand All @@ -60,7 +60,7 @@ describe('Service: UserSearch', () => {
});

service
.fetchQuery(user, follow_count)
.fetchQuery(user)
.subscribe((res) => {
expect(res).toEqual(result);
done();
Expand Down
9 changes: 5 additions & 4 deletions src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ import { UserResponse } from '../models/api-user-response';
export class UserService {
private static readonly apiUrl: URL = new URL('https://api.loklak.org/api/user.json');
private static minified_results = true;
private static followers = '4';
private static following = '4';

constructor(
private jsonp: Jsonp
) { }

// TODO: make the searchParams as configureable model rather than this approach.
public fetchQuery(user: string, follow_count: number): Observable<UserResponse> {
public fetchQuery(user: string): Observable<UserResponse> {
const screen_name = user.charAt(0).toUpperCase() + user.slice(1);
const searchParams = new URLSearchParams();
searchParams.set('screen_name', screen_name);
searchParams.set('followers', follow_count.toString());
searchParams.set('following', follow_count.toString());
searchParams.set('followers', UserService.followers);
searchParams.set('following', UserService.following);
searchParams.set('callback', 'JSONP_CALLBACK');
searchParams.set('minified', UserService.minified_results.toString());
return this.jsonp.get(UserService.apiUrl.toString(), {search : searchParams})
.map(this.extractData)
.retry(2)
.catch(this.handleError);

}

private extractData(res: Response): UserResponse {
Expand Down

0 comments on commit 7eaf03f

Please sign in to comment.