Skip to content

Commit

Permalink
Problem with not-found briefly showing is solved. Also, made the sear…
Browse files Browse the repository at this point in the history
…ch pagination work
  • Loading branch information
LuisLeandro94 committed Jun 19, 2021
1 parent 795f928 commit eaff7b7
Show file tree
Hide file tree
Showing 10 changed files with 16,398 additions and 69 deletions.
16,314 changes: 16,294 additions & 20 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<app-navbar></app-navbar>
<app-error
*ngIf="dataStore.Images.length === 0 && imageService.isSearch == true"
></app-error>
<router-outlet></router-outlet>
6 changes: 5 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { DatastoreService } from './services/datastore.service';
import { ImageService } from './services/image.service';

@Component({
selector: 'app-root',
Expand All @@ -9,7 +10,10 @@ import { DatastoreService } from './services/datastore.service';
export class AppComponent implements OnInit {
title = 'finalProject';

constructor(public dataStore: DatastoreService) {}
constructor(
public dataStore: DatastoreService,
public imageService: ImageService
) {}

ngOnInit() {
this.dataStore.getImages(1);
Expand Down
19 changes: 0 additions & 19 deletions src/app/image-list/image-list.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,3 @@ a {
.padding-bottom {
padding-bottom: 5px;
}

.pagination {
display: flex;
justify-content: center;
}

.page-link {
color: var(--clr-accent);
background-color: var(--clr-dark);
}

.pagination {
position: fixed;
left: 0;
bottom: 10px;
width: 100%;
text-align: center;
z-index: 999;
}
19 changes: 0 additions & 19 deletions src/app/image-list/image-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,4 @@ <h5 *ngIf="image.description == ''">
</a>
</div>
</div>
<nav aria-label="pagination" class="pagination">
<ul class="pagination">
<li class="page-item" [ngClass]="{ disabled: dataStore.Page == 1 }">
<a class="page-link" aria-label="Previous" (click)="paginationL()">
<fa-icon [icon]="faArrowLeft"></fa-icon>
<span class="sr-only">Previous</span>
</a>
</li>
<li
class="page-item"
[ngClass]="{ disabled: dataStore.Images.length < 24 }"
>
<a class="page-link" aria-label="Next" (click)="paginationR()">
<fa-icon [icon]="faArrowRight"></fa-icon>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</nav>
</div>
19 changes: 19 additions & 0 deletions src/app/navbar/navbar.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ a:hover {
.cursor {
cursor: pointer;
}

.pagination {
position: fixed;
left: 0;
bottom: 10px;
width: 100%;
text-align: center;
z-index: 999;
}

.page-link {
color: var(--clr-accent);
background-color: var(--clr-dark);
}

.pagination {
display: flex;
justify-content: center;
}
24 changes: 23 additions & 1 deletion src/app/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,26 @@ <h6 class="modal-title">Search Error</h6>
</button>
</div>
</ng-template>
<app-error *ngIf="dataStore.Images.length == 0"></app-error>
<nav
aria-label="pagination"
class="pagination"
[ngClass]="{ hidden: dataStore.Images.length == 0 }"
>
<ul class="pagination">
<li class="page-item" [ngClass]="{ disabled: dataStore.Page == 1 }">
<a class="page-link" aria-label="Previous" (click)="paginationL()">
<fa-icon [icon]="faArrowLeft"></fa-icon>
<span class="sr-only">Previous</span>
</a>
</li>
<li
class="page-item"
[ngClass]="{ disabled: dataStore.Images.length < 24 }"
>
<a class="page-link" aria-label="Next" (click)="paginationR()">
<fa-icon [icon]="faArrowRight"></fa-icon>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
</nav>
36 changes: 33 additions & 3 deletions src/app/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { faSearch, faCameraRetro } from '@fortawesome/free-solid-svg-icons';
import {
faSearch,
faCameraRetro,
faArrowRight,
faArrowLeft,
} from '@fortawesome/free-solid-svg-icons';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { DatastoreService } from '../services/datastore.service';
import { ImageService } from '../services/image.service';
import { Image } from '../shared/image.model';

@Component({
Expand All @@ -13,6 +19,8 @@ import { Image } from '../shared/image.model';
export class NavbarComponent implements OnInit {
faSearch = faSearch;
faCameraRetro = faCameraRetro;
faArrowLeft = faArrowLeft;
faArrowRight = faArrowRight;
images: Image[];
searchInput: '';

Expand All @@ -21,13 +29,14 @@ export class NavbarComponent implements OnInit {
constructor(
public dataStore: DatastoreService,
private modalService: NgbModal,
private router: Router
private router: Router,
private imageService: ImageService
) {}

ngOnInit(): void {}

search(): void {
this.dataStore.getSearch(this.searchInput);
this.dataStore.getSearch(this.searchInput, this.dataStore.PageSearch);
}

modalOpen(content: any) {
Expand All @@ -38,4 +47,25 @@ export class NavbarComponent implements OnInit {
this.dataStore.getImages(1);
this.router.navigate(['']);
}

paginationR() {
if (this.imageService.isSearch == false) {
this.dataStore.Page += 1;
this.dataStore.getImages(this.dataStore.Page);
} else {
debugger;
this.dataStore.PageSearch += 1;
this.dataStore.getSearch(this.searchInput, this.dataStore.PageSearch);
}
}

paginationL() {
if (this.imageService.isSearch == false) {
this.dataStore.Page -= 1;
this.dataStore.getImages(this.dataStore.Page);
} else {
this.dataStore.PageSearch -= 1;
this.dataStore.getSearch(this.searchInput, this.dataStore.PageSearch);
}
}
}
15 changes: 13 additions & 2 deletions src/app/services/datastore.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class DatastoreService {
page = new BehaviorSubject<any>(1);
page$ = this.page.asObservable();

pageSearch = new BehaviorSubject<any>(1);
pageSearch$ = this.page.asObservable();

constructor(private imageService: ImageService) {}

get Images(): Image[] {
Expand All @@ -42,12 +45,20 @@ export class DatastoreService {
this.page.next(value);
}

get PageSearch(): any {
return this.pageSearch.getValue();
}

set PageSearch(value: any) {
this.pageSearch.next(value);
}

async getImages(page: number) {
this.Images = await this.imageService.convertAnsw(page);
}

async getSearch(queryString: string) {
this.Images = await this.imageService.searchAPI(queryString);
async getSearch(queryString: string, page: number) {
this.Images = await this.imageService.searchAPI(queryString, page);
}

async getFav(id: string | null) {
Expand Down
12 changes: 8 additions & 4 deletions src/app/services/image.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Image } from '../shared/image.model';

@Injectable()
export class ImageService {
isSearch: boolean = false;
images: Image[] = [];
favorites = [];
clientID: string = 'hsntDSGlKnHUNoVJgTTvMw14mHh9GgNAEwRrwLP_j_0';
Expand All @@ -25,6 +26,7 @@ export class ImageService {
}

convertAnsw(page: number) {
this.isSearch = false;
this.images = [];
this.getData(page).subscribe((data) => {
for (const item of data as any) {
Expand All @@ -36,9 +38,10 @@ export class ImageService {
return this.images;
}

searchAPI(queryString: string) {
searchAPI(queryString: string, page: number) {
this.isSearch = true;
this.images = [];
this.getSearch(queryString).subscribe((data: any) => {
this.getSearch(queryString, page).subscribe((data: any) => {
for (const item of data.results as any) {
this.createImage(item).then((data: any) => {
this.images.push(data.image);
Expand All @@ -48,8 +51,9 @@ export class ImageService {
return this.images;
}

getSearch(queryString: string) {
return this.http.get(this.baseURL + queryString);
getSearch(queryString: string, page: number) {
let url = `https://api.unsplash.com/search/photos?per_page=24&page=${page}&order_by=latest&client_id=hsntDSGlKnHUNoVJgTTvMw14mHh9GgNAEwRrwLP_j_0&query=${queryString}`;
return this.http.get(url);
}

createImage(image: any): Promise<any> {
Expand Down

0 comments on commit eaff7b7

Please sign in to comment.