Skip to content

Commit 79b429d

Browse files
committed
#75 basic pagination added on job history component
1 parent 3d1b0e3 commit 79b429d

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

app/job/job-history/job-history.component.html

+14
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,18 @@
5151
</table>
5252
</div>
5353
</widget-body>
54+
<widget-footer>
55+
<ul class="pagination pagination-sm pull-right" *ngIf="status=='SUCCESSFUL'">
56+
<li>
57+
<a>&laquo;</a>
58+
</li>
59+
<li *ngFor="let page of paginationArray">
60+
<a (click)="getJobsWithPageNumber(page)">{{page}}</a>
61+
</li>
62+
<li>
63+
<a>&raquo;</a>
64+
</li>
65+
</ul>
66+
<div class=clearfix></div>
67+
</widget-footer>
5468
</div>

app/job/job-history/job-history.component.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class JobHistoryComponent implements OnInit {
3131
accessMode: AccessMode = "DEFAULT";
3232
componentMode: ComponentMode = "WIDGET";
3333
statusMessage: string;
34+
paginationArray: number[];
3435

3536
constructor(private jobService: JobService, private router: Router) {
3637
}
@@ -48,18 +49,42 @@ export class JobHistoryComponent implements OnInit {
4849
this.jobs = new Array<Job>();
4950
this.jobService.getHistory()
5051
.subscribe((pagedJob) => {
51-
this.status = "SUCCESSFUL";
52-
this.jobs = pagedJob.data;
53-
if (!this.jobs.length) {
54-
this.status = "EMPTY";
55-
this.statusMessage = "It looks lonely here. Why don't you put an order?";
56-
}
52+
this.manageHistory(pagedJob);
5753
}, (error) => {
5854
this.statusMessage = error.Message || "Failed to fetch data from server";
5955
this.status = "FAILED";
6056
});
6157
}
6258

59+
60+
61+
getJobsWithPageNumber(page: number){
62+
this.status = "IN_PROGRESS"
63+
this.jobService.getHistoryWithPageNumber(page)
64+
.subscribe((pagedJob) => {
65+
this.manageHistory(pagedJob)
66+
},(error) => {
67+
this.statusMessage = error.Message || "Failed to fetch data from server";
68+
this.status = "FAILED";
69+
})
70+
}
71+
72+
private manageHistory(pagedJob){
73+
this.status = "SUCCESSFUL";
74+
this.jobs = pagedJob.data;
75+
76+
// FIXME: This is an ugly code I confess
77+
this.paginationArray = new Array();
78+
for (var i = 0; i < pagedJob.pagination.TotalPages; i++) {
79+
this.paginationArray.push(i + 1);
80+
}
81+
82+
if (!this.jobs.length) {
83+
this.status = "EMPTY";
84+
this.statusMessage = "It looks lonely here. Why don't you put an order?";
85+
}
86+
}
87+
6388
setJobStatusLabelClass(state: string) {
6489
switch (state) {
6590
case "COMPLETED":

app/job/shared/job.service.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,35 @@ export class JobService {
2525
private jobUrl = AppSettings.TASKCAT_API_BASE + 'job';
2626
private assetLocationUrl = AppSettings.SHADOWCAT_API_BASE + "location/";
2727

28+
getJobs(jobUrl): Observable<PageEnvelope<Job>> {
29+
return this.shttp.secureGet(jobUrl)
30+
.map(this._extractData)
31+
.catch(error => {
32+
let errMsg = error.message || 'Exception when fetching job history';
33+
console.error(errMsg); // log to console instead
34+
return Observable.throw(errMsg);
35+
});
36+
}
37+
2838
getHistory(): Observable<PageEnvelope<Job>> {
2939
let queryString: string = this._queryBuilder.orderBy([
3040
{
3141
propName: "CreateTime",
3242
orderDirection: "desc"
3343
}]).toQueryString();
44+
let historyUrl = this.jobUrl + '/odata' + queryString
3445

35-
return this.shttp.secureGet(this.jobUrl + '/odata' + queryString)
36-
.map(this._extractData)
37-
.catch(error => {
38-
let errMsg = error.message || 'Exception when fetching job history';
39-
console.error(errMsg); // log to console instead
40-
return Observable.throw(errMsg);
41-
});
46+
return this.getJobs(historyUrl);
47+
}
48+
49+
getHistoryWithPageNumber(page:number): Observable<PageEnvelope<Job>> {
50+
let queryString: string = this._queryBuilder.toQueryString();
51+
let historyUrl = this.jobUrl + '/odata' + queryString + "&page=" + page;
52+
console.log(this.jobUrl);
53+
console.log(queryString);
54+
console.log(historyUrl);
55+
56+
return this.getJobs(historyUrl)
4257
}
4358

4459

app/shared/query-builder/query-builder.ts

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export class QueryBuilder {
2020
return this;
2121
}
2222

23+
public page(props: number): QueryBuilder {
24+
let querySegment: string = "page";
25+
let pageSegment = props.toString();
26+
this._querySegments.push(querySegment.concat("=", pageSegment));
27+
28+
return this;
29+
}
30+
2331
public toQueryString(): string {
2432
return "?".concat(this._querySegments.join("&"));
2533
}

0 commit comments

Comments
 (0)