Skip to content

Commit c6029cb

Browse files
lmmrssapaulbert
authored andcommitted
Indicators for parent/local doc diff (fixes #1560) (#1620)
1 parent 7e6b73a commit c6029cb

10 files changed

+97
-18
lines changed

src/app/courses/courses.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<h3 class="header"><a [routerLink]="['view', element._id]">{{element.courseTitle}}</a>
6868
</h3>
6969
<div class="content"><p>{{ element.description }}</p></div>
70+
<planet-local-status [status]="element.localStatus"></planet-local-status>
7071
<button *ngIf="!parent" class="menu" mat-icon-button [matMenuTriggerFor]="resourceMenu">
7172
<mat-icon>more_vert</mat-icon>
7273
</button>

src/app/courses/courses.component.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { SelectionModel } from '@angular/cdk/collections';
77
import { Router, ActivatedRoute, } from '@angular/router';
88
import { FormBuilder, FormGroup, } from '@angular/forms';
99
import { UserService } from '../shared/user.service';
10-
import { Subject } from 'rxjs';
11-
import { switchMap, takeUntil } from 'rxjs/operators';
10+
import { Subject, of } from 'rxjs';
11+
import { switchMap, takeUntil, map } from 'rxjs/operators';
1212
import { filterDropdowns, filterSpecificFields, composeFilterFunctions } from '../shared/table-helpers';
1313
import * as constants from './constants';
1414
import { debug } from '../debug-operator';
@@ -86,10 +86,17 @@ export class CoursesComponent implements OnInit, AfterViewInit, OnDestroy {
8686
this.userShelf = this.userService.shelf;
8787
this.courses.filterPredicate = composeFilterFunctions([ filterDropdowns(this.filter), filterSpecificFields([ 'courseTitle' ]) ]);
8888
this.courses.sortingDataAccessor = (item, property) => item[property].toLowerCase();
89-
this.coursesService.coursesUpdated$.pipe(takeUntil(this.onDestroy$)).subscribe((courses) => {
90-
// Sort in descending createdDate order, so the new courses can be shown on the top
91-
courses.sort((a, b) => b.createdDate - a.createdDate);
92-
this.courses.data = this.setupList(courses, this.userShelf.courseIds);
89+
this.coursesService.coursesUpdated$.pipe(
90+
takeUntil(this.onDestroy$),
91+
map((courses: any) => {
92+
// Sort in descending createdDate order, so the new courses can be shown on the top
93+
courses.sort((a, b) => b.createdDate - a.createdDate);
94+
this.userShelf = this.userService.shelf;
95+
return this.setupList(courses, this.userShelf.courseIds);
96+
}),
97+
switchMap((courses: any) => this.parent ? this.couchService.localComparison(this.dbName, courses) : of(courses))
98+
).subscribe((courses: any) => {
99+
this.courses.data = courses;
93100
});
94101
}
95102

src/app/courses/courses.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { CoursesProgressLeaderComponent } from './progress-courses/courses-progr
1717
import { CoursesProgressBarComponent } from './progress-courses/courses-progress-bar.component';
1818
import { CoursesProgressChartComponent } from './progress-courses/courses-progress-chart.component';
1919
import { CoursesProgressLearnerComponent } from './progress-courses/courses-progress-learner.component';
20+
import { SharedComponentsModule } from '../shared/shared-components.module';
2021

2122
@NgModule({
2223
imports: [
@@ -28,7 +29,8 @@ import { CoursesProgressLearnerComponent } from './progress-courses/courses-prog
2829
PlanetDialogsModule,
2930
MaterialModule,
3031
ResourcesModule,
31-
ExamsModule
32+
ExamsModule,
33+
SharedComponentsModule
3234
],
3335
declarations: [
3436
CoursesComponent,

src/app/resources/resources.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ <h3 class="header">
6161
{{tag}}
6262
</mat-chip>
6363
</mat-chip-list>
64+
<planet-local-status [status]="element.localStatus"></planet-local-status>
6465
<button *ngIf="!parent" class="menu" mat-icon-button [matMenuTriggerFor]="resourceMenu">
6566
<mat-icon>more_vert</mat-icon>
6667
</button>

src/app/resources/resources.component.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { MatTableDataSource, MatPaginator, MatSort, MatDialog, PageEvent } from
55
import { SelectionModel } from '@angular/cdk/collections';
66
import { Router, ActivatedRoute } from '@angular/router';
77
import { HttpClient } from '@angular/common/http';
8-
import { takeUntil } from 'rxjs/operators';
9-
import { Subject } from 'rxjs';
8+
import { takeUntil, map, switchMap } from 'rxjs/operators';
9+
import { Subject, of } from 'rxjs';
1010
import { PlanetMessageService } from '../shared/planet-message.service';
1111
import { UserService } from '../shared/user.service';
1212
import { filterSpecificFields, composeFilterFunctions, filterArrayField, filterTags } from '../shared/table-helpers';
@@ -79,12 +79,15 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
7979
) {}
8080

8181
ngOnInit() {
82-
this.resourcesService.resourcesUpdated$.pipe(takeUntil(this.onDestroy$))
83-
.subscribe((resources) => {
84-
// Sort in descending createdDate order, so the new resource can be shown on the top
85-
resources.sort((a, b) => b.createdDate - a.createdDate);
82+
this.resourcesService.resourcesUpdated$.pipe(takeUntil(this.onDestroy$)).pipe(
83+
map((resources) => {
84+
// Sort in descending createdDate order, so the new resource can be shown on the top
85+
resources.sort((a, b) => b.createdDate - a.createdDate);
86+
return this.setupList(resources, this.userService.shelf.resourceIds);
87+
}),
88+
switchMap((resources) => this.parent ? this.couchService.localComparison(this.dbName, resources) : of(resources))
89+
).subscribe((resources) => {
8690
this.resources.data = resources;
87-
this.setupList(this.resources.data, this.userService.shelf.resourceIds);
8891
});
8992
this.resourcesService.updateResources({ opts: this.getOpts });
9093
this.resources.filterPredicate = composeFilterFunctions(
@@ -109,11 +112,11 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
109112
}
110113

111114
setupList(resourcesRes, myLibrarys) {
112-
resourcesRes.forEach((resource: any) => {
115+
return resourcesRes.map((resource: any) => {
113116
const myLibraryIndex = myLibrarys.findIndex(resourceId => {
114117
return resource._id === resourceId;
115118
});
116-
resource.libraryInfo = myLibraryIndex > -1;
119+
return { ...resource, libraryInfo: myLibraryIndex > -1 };
117120
});
118121
}
119122

src/app/resources/resources.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ResourcesRouterModule } from './resources-router.module';
1010
import { MaterialModule } from '../shared/material.module';
1111
import { HttpClientModule, HttpClientJsonpModule } from '@angular/common/http';
1212
import { PlanetDialogsModule } from '../shared/dialogs/planet-dialogs.module';
13+
import { SharedComponentsModule } from '../shared/shared-components.module';
1314

1415
@NgModule({
1516
imports: [
@@ -21,7 +22,8 @@ import { PlanetDialogsModule } from '../shared/dialogs/planet-dialogs.module';
2122
MaterialModule,
2223
HttpClientModule,
2324
HttpClientJsonpModule,
24-
PlanetDialogsModule
25+
PlanetDialogsModule,
26+
SharedComponentsModule
2527
],
2628
declarations: [
2729
ResourcesComponent,

src/app/shared/couchdb.service.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Observable, of } from 'rxjs';
55
import { catchError, map, expand, takeWhile, toArray, flatMap } from 'rxjs/operators';
66
import { debug } from '../debug-operator';
77
import { PlanetMessageService } from './planet-message.service';
8+
import { findDocuments, inSelector } from './mangoQueries';
89

910
@Injectable()
1011
export class CouchService {
@@ -76,7 +77,21 @@ export class CouchService {
7677
}));
7778
}
7879

80+
localComparison(db: string, parentDocs: any[]) {
81+
const ids = parentDocs.map((parentDoc: any) => parentDoc._id);
82+
return this.findAll(db, findDocuments({ '_id': inSelector(ids) })).pipe(map((localDocs) => {
83+
return parentDocs.map((parentDoc) => {
84+
const localDoc: any = localDocs.find((doc: any) => doc._id === parentDoc._id);
85+
return {
86+
...parentDoc,
87+
localStatus: localDoc !== undefined ? this.compareRev(parentDoc._rev, localDoc._rev) : 0
88+
};
89+
});
90+
}));
91+
}
92+
7993
findAll(db: string, query: any, opts?: any) {
94+
console.log(query);
8095
return this.post(db + '/_find', query, opts).pipe(expand((res) => {
8196
return this.post(db + '/_find', { ...query, bookmark: res.bookmark }, opts);
8297
}), takeWhile((res) => {
@@ -105,4 +120,13 @@ export class CouchService {
105120
return this.http.get(urlPrefix + '/' + url, opts);
106121
}
107122

123+
private compareRev = (parent, local) => {
124+
if (parent === local) {
125+
return 'match';
126+
}
127+
local = parseInt(local.split('-')[0], 10);
128+
parent = parseInt(parent.split('-')[0], 10);
129+
return (local < parent) ? 'newerAvailable' : (local > parent) ? 'parentOlder' : 'mismatch';
130+
}
131+
108132
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Component, Input } from '@angular/core';
2+
3+
@Component({
4+
'selector': 'planet-local-status',
5+
'template': `
6+
<ng-container [ngSwitch]="status">
7+
<mat-icon *ngSwitchCase="'match'" i18n-title title="Upto date">done_all</mat-icon>
8+
<mat-icon *ngSwitchCase="'newerAvailable'" i18n-title title="Newer">fiber_new</mat-icon>
9+
<mat-icon *ngSwitchCase="'parentOlder'" i18n-title title="Older">timelapse</mat-icon>
10+
<mat-icon *ngSwitchCase="'mismatch'" i18n-title title="Does not match">priority_high</mat-icon>
11+
</ng-container>
12+
`
13+
})
14+
export class PlanetLocalStatusComponent {
15+
@Input() status: string;
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { PlanetLocalStatusComponent } from './planet-local-status.component';
4+
import { MaterialModule } from './material.module';
5+
6+
@NgModule({
7+
imports: [
8+
CommonModule, MaterialModule
9+
],
10+
exports: [
11+
PlanetLocalStatusComponent
12+
],
13+
declarations: [
14+
PlanetLocalStatusComponent
15+
]
16+
})
17+
export class SharedComponentsModule {}

src/styles.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ body {
118118
display: grid;
119119
grid-template-areas:
120120
'hd mn'
121-
'cn .';
121+
'cn .'
122+
'tags tags';
122123
grid-template-columns: auto 50px;
123124

124125
.header {
@@ -136,6 +137,11 @@ body {
136137
grid-area: mn;
137138
}
138139

140+
.tags-list {
141+
grid-area: tags;
142+
margin-bottom: 0.5rem;
143+
}
144+
139145
}
140146

141147
// the colored stars are placed on top of the uncolored ones

0 commit comments

Comments
 (0)