[HIMA-131] Created mock library & moved all the mock data of the app inside the mock library

This commit is contained in:
Eraldo Hasanaj
2019-03-06 11:02:51 +01:00
parent 8e60b0cd39
commit 9dbe44bb35
31 changed files with 471 additions and 373 deletions

View File

@@ -1,13 +1,16 @@
import { DataSource, CollectionViewer } from '@angular/cdk/collections';
import { procuctsMock } from 'mocks/products.mock';
import { procuctsMock } from 'mock';
import { Product } from '../../core/models/product.model';
import { BehaviorSubject, Subscription, Observable, of } from 'rxjs';
import { ProductMapping } from '../../core/mappings/product.mapping';
import { ProductService } from '../../core/services/product.service';
import { Store } from '@ngxs/store';
import { debounceTime, take } from 'rxjs/operators';
import { SetProducts, CurrentPageLoaded } from '../../core/store/actions/product.actions';
import { ItemDTO } from 'dist/cat-service/lib/dtos';
import {
SetProducts,
CurrentPageLoaded
} from '../../core/store/actions/product.actions';
import { ItemDTO } from 'cat-service';
export class SearchDataSource extends DataSource<Product | undefined> {
private pageSize = 10;
@@ -50,12 +53,9 @@ export class SearchDataSource extends DataSource<Product | undefined> {
})
);
this.dssub.add(
this.dataStreamDTO
.pipe(debounceTime(1000))
.subscribe(i => {
this.store.dispatch(new SetProducts([...i], this.search));
}
)
this.dataStreamDTO.pipe(debounceTime(1000)).subscribe(i => {
this.store.dispatch(new SetProducts([...i], this.search));
})
);
this.fetchPage(0);
return this.dataStream;
@@ -88,13 +88,17 @@ export class SearchDataSource extends DataSource<Product | undefined> {
const cachedItems = this.cachcedProducts.filter((item: ItemDTO) => item);
if (page === 0) {
this.cachedData = Array.from<Product>({ length: this.cachcedProducts.length });
this.cachedItemsDTO = Array.from<ItemDTO>({ length: this.cachcedProducts.length });
this.cachedData = Array.from<Product>({
length: this.cachcedProducts.length
});
this.cachedItemsDTO = Array.from<ItemDTO>({
length: this.cachcedProducts.length
});
}
this.cachedItemsDTO.splice(
page * this.pageSize,
this.pageSize,
... cachedItems
...cachedItems
);
this.cachedData.splice(
page * this.pageSize,
@@ -122,39 +126,39 @@ export class SearchDataSource extends DataSource<Product | undefined> {
this.pageSize,
this.filters
)
.pipe(take(1))
.subscribe(data => {
this.loading = false;
this.results = true;
.pipe(take(1))
.subscribe(data => {
this.loading = false;
this.results = true;
if (page === 0) {
this.cachedData = Array.from<Product>({ length: data.hits });
this.cachedItemsDTO = Array.from<ItemDTO>({ length: data.hits });
}
this.cachedItemsDTO.splice(
page * this.pageSize,
this.pageSize,
...data.result
);
this.cachedData.splice(
page * this.pageSize,
this.pageSize,
...data.result.map((item, i) => {
if (i === 3) {
return procuctsMock[3];
}
return this.productMapping.fromItemDTO(item);
})
);
this.dataStream.next(this.cachedData);
this.dataStreamDTO.next(this.cachedItemsDTO);
if (page === 0) {
// dispatch immediately on first page load
this.store.dispatch(
new SetProducts([...this.cachedItemsDTO], this.search)
if (page === 0) {
this.cachedData = Array.from<Product>({ length: data.hits });
this.cachedItemsDTO = Array.from<ItemDTO>({ length: data.hits });
}
this.cachedItemsDTO.splice(
page * this.pageSize,
this.pageSize,
...data.result
);
}
});
this.cachedData.splice(
page * this.pageSize,
this.pageSize,
...data.result.map((item, i) => {
if (i === 3) {
return procuctsMock[3];
}
return this.productMapping.fromItemDTO(item);
})
);
this.dataStream.next(this.cachedData);
this.dataStreamDTO.next(this.cachedItemsDTO);
if (page === 0) {
// dispatch immediately on first page load
this.store.dispatch(
new SetProducts([...this.cachedItemsDTO], this.search)
);
}
});
}
}
}

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Filter } from '../models/filter.model';
import { filterMock } from 'mocks/filters.mock';
import { filterMock } from 'mock';
import { CatSearchService, ApiResponse, UISettingsDTO } from 'cat-service';
import { FilterItem } from '../models/filter-item.model';
import { FilterMapping } from '../mappings/filter.mapping';
@@ -14,14 +14,14 @@ export class FilterService {
constructor(
private service: CatSearchService,
private filterMapping: FilterMapping
) { }
) {}
selectFilterById(filters: Filter[], id: string): Observable<Filter[]> {
const newFilterState = filters.map((filter: Filter) => {
if (filter.id === id) {
return { ...filter, expanded: true };
}
return { ...filter, expanded: false };
return { ...filter, expanded: false };
});
return of(newFilterState);
}
@@ -47,7 +47,10 @@ export class FilterService {
return of(newFilterState);
}
toggleFilterItemsByName(filters: Filter[], name: string): Observable<Filter[]> {
toggleFilterItemsByName(
filters: Filter[],
name: string
): Observable<Filter[]> {
const newFilterState = filters.map((filter: Filter) => {
if (filter.expanded === true) {
const newItemsState = this.toggleItemByName(filter.items, name);
@@ -78,19 +81,23 @@ export class FilterService {
// service method to get the first 3 filters
getFilters(): Observable<Filter[]> {
return this.service.settings().pipe(
map(
(data: ApiResponse<UISettingsDTO>) =>
return this.service
.settings()
.pipe(
map((data: ApiResponse<UISettingsDTO>) =>
this.filterMapping.fromUiSettingsDto(data.result).slice(0, 3)
));
)
);
}
// service method to get filters metadata
getFullFilter(): Observable<Filter[]> {
return this.service.settings().pipe(
map(
(data: ApiResponse<UISettingsDTO>) =>
return this.service
.settings()
.pipe(
map((data: ApiResponse<UISettingsDTO>) =>
this.filterMapping.fromUiSettingsDto(data.result)
));
)
);
}
}

View File

@@ -1,5 +1,4 @@
import { Injectable } from '@angular/core';
import { procuctsMock } from 'mocks/products.mock';
import { of, Observable } from 'rxjs';
import { Product } from '../models/product.model';
import { RecentArticleSearch } from '../models/recent-article-search.model';
@@ -13,6 +12,7 @@ import {
import { map } from 'rxjs/operators';
import { Search } from '../models/search.model';
import { FilterMapping } from '../mappings/filter.mapping';
import { procuctsMock } from 'mock';
@Injectable({
providedIn: 'root'

View File

@@ -1,31 +1,38 @@
import { Injectable } from '@angular/core';
import { User, Address } from '../../core/models/user.model';
import { usersMock } from 'mocks/users.mock';
import { usersMock } from 'mock';
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor() {
constructor() {
localStorage.setItem('users', JSON.stringify(usersMock));
}
searchUser(params: string): Observable<User[]> {
const mockedUsers = JSON.parse(localStorage.getItem('users'));
let foundUsers: User[] = [];
const foundUsers: User[] = [];
const splitedParams = params.split(' ');
mockedUsers.forEach((user: User) => {
splitedParams.forEach((word: string) => {
if (word) {
if (user.name.toLowerCase().indexOf(word.toLowerCase()) >= 0 ||
user.delivery_addres.city.toString().toLowerCase().indexOf(word.toLowerCase()) >= 0 ||
user.delivery_addres.zip.toString().toLowerCase().indexOf(params.toLowerCase()) >= 0
if (
user.name.toLowerCase().indexOf(word.toLowerCase()) >= 0 ||
user.delivery_addres.city
.toString()
.toLowerCase()
.indexOf(word.toLowerCase()) >= 0 ||
user.delivery_addres.zip
.toString()
.toLowerCase()
.indexOf(params.toLowerCase()) >= 0
) {
const userExists = foundUsers.filter((userAdded: User) => userAdded.id === user.id);
const userExists = foundUsers.filter(
(userAdded: User) => userAdded.id === user.id
);
if (userExists.length === 0) {
foundUsers.push(user);
}
@@ -33,14 +40,12 @@ export class UserService {
}
});
});
return of(foundUsers);
}
addUser(user: User) {
const mockedUsers = JSON.parse(localStorage.getItem('users'));
localStorage.setItem('users',
JSON.stringify([...mockedUsers, user])
);
localStorage.setItem('users', JSON.stringify([...mockedUsers, user]));
}
}

View File

@@ -1,6 +1,6 @@
import { State, Selector, Action, StateContext } from '@ngxs/store';
import { LoadFeed } from '../actions/feed.actions';
import { feedMock } from 'mocks/feed.mock';
import { feedMock } from 'mock';
import { FeedCard } from '../../models/feed-card.model';
import { FeedService, FeedDTO } from 'feed-service';
import { map } from 'rxjs/operators';