Skip to content

Commit

Permalink
Merge pull request #299 from bkd-mba-fbi/feature/238-ansicht-speichern
Browse files Browse the repository at this point in the history
238: save viewMode of presenceControl
  • Loading branch information
spahrson authored Jun 9, 2021
2 parents 042d271 + 2846936 commit 8bf5e3f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Lesson } from 'src/app/shared/models/lesson.model';
import { LessonPresencesUpdateService } from 'src/app/shared/services/lesson-presences-update.service';
import { PresenceType } from 'src/app/shared/models/presence-type.model';
import { LessonPresence } from 'src/app/shared/models/lesson-presence.model';
import { UserSettingsRestService } from 'src/app/shared/services/user-settings-rest.service';

describe('PresenceControlListComponent', () => {
let component: PresenceControlListComponent;
Expand Down Expand Up @@ -66,6 +67,7 @@ describe('PresenceControlListComponent', () => {
.createSpy('getBlockLessonPresences')
.and.callFake(() => of(blockLessons)),
hasUnconfirmedAbsences: () => of(false),
viewMode$: of(),
} as unknown) as PresenceControlStateService;

lessonPresencesUpdateServiceMock = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import {
} from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { BehaviorSubject, combineLatest, Subject } from 'rxjs';
import { map, shareReplay, take, takeUntil } from 'rxjs/operators';
import { BehaviorSubject, combineLatest, Observable, of, Subject } from 'rxjs';
import {
distinctUntilChanged,
filter,
first,
map,
mergeAll,
shareReplay,
switchMap,
take,
takeUntil,
} from 'rxjs/operators';

import { LessonPresence } from 'src/app/shared/models/lesson-presence.model';
import { LessonPresencesUpdateService } from 'src/app/shared/services/lesson-presences-update.service';
Expand All @@ -18,12 +28,21 @@ import { PresenceControlEntry } from '../../models/presence-control-entry.model'
import {
PresenceControlStateService,
VIEW_MODES,
ViewMode,
} from '../../services/presence-control-state.service';
import { PresenceControlBlockLessonComponent } from '../presence-control-block-lesson/presence-control-block-lesson.component';
import { ScrollPositionService } from 'src/app/shared/services/scroll-position.service';
import { parseISOLocalDate } from 'src/app/shared/utils/date';
import { PresenceControlIncidentComponent } from '../presence-control-incident/presence-control-incident.component';
import { PresenceTypesService } from '../../../shared/services/presence-types.service';
import {
BaseProperty,
UserSetting,
ViewModeType,
} from 'src/app/shared/models/user-setting.model';
import { buildUserSetting } from 'src/spec-builders';
import { UserSettingsRestService } from 'src/app/shared/services/user-settings-rest.service';
import { decode } from 'src/app/shared/utils/decode';

@Component({
selector: 'erz-presence-control-list',
Expand All @@ -42,13 +61,21 @@ export class PresenceControlListComponent
private destroy$ = new Subject();

constructor(
private settingsService: UserSettingsRestService,
public state: PresenceControlStateService,
private lessonPresencesUpdateService: LessonPresencesUpdateService,
private presenceTypesService: PresenceTypesService,
private modalService: NgbModal,
private scrollPosition: ScrollPositionService,
private route: ActivatedRoute
) {}
) {
this.state.viewMode$
.pipe(
distinctUntilChanged(),
switchMap((v) => this.updateSavedViewMode(v))
)
.subscribe();
}

ngOnInit(): void {
this.route.queryParams
Expand Down Expand Up @@ -143,6 +170,45 @@ export class PresenceControlListComponent

if (params.viewMode && VIEW_MODES.includes(params.viewMode)) {
this.state.setViewMode(params.viewMode);
} else {
this.getSavedViewMode()
.pipe(take(1))
.subscribe((v) => {
this.state.setViewMode(v);
});
}
}

private updateSavedViewMode(viewMode: ViewMode): Observable<any> {
const propertyBody: ViewModeType = {
presenceControl: viewMode,
};
const body: BaseProperty = {
Key: 'presenceControlViewMode',
Value: JSON.stringify(propertyBody),
};
const cst = Object.assign({}, buildUserSetting());
cst.Settings.push(body);
return this.settingsService.updateUserSettingsCst(cst);
}

private getSavedViewMode(): Observable<ViewMode> {
return this.settingsService.getUserSettingsCst().pipe(
map<UserSetting, BaseProperty[]>((i) => i.Settings),
mergeAll(),
filter((i) => i.Key === 'presenceControlViewMode'),
first(),
map((v) => JSON.parse(v.Value)),
switchMap(decode(ViewModeType)),
map((v) => this.getViewModeForString(v.presenceControl))
);
}

private getViewModeForString(viewMode: string): ViewMode {
if (viewMode === ViewMode.List) {
return ViewMode.List;
} else {
return ViewMode.Grid; // default
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
extractLessonEntries,
getCurrentLessonEntry,
} from '../utils/lesson-entries';
import { isEmptyArray } from '../../shared/utils/array';
import { LessonTeachersRestService } from '../../shared/services/lesson-teachers-rest.service';
import { PersonsRestService } from '../../shared/services/persons-rest.service';

Expand All @@ -66,7 +65,6 @@ export class PresenceControlStateService
private selectedDateSubject$ = new BehaviorSubject(new Date());
private selectLesson$ = new Subject<Option<LessonEntry>>();
private viewModeSubject$ = new BehaviorSubject(ViewMode.Grid);

private updateLessonPresences$ = new Subject<ReadonlyArray<LessonPresence>>();

private lessonPresences$ = merge(
Expand Down
6 changes: 6 additions & 0 deletions src/app/shared/models/user-setting.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ const NotificationDataPropertyValueType = t.type({
body: t.string,
});

const ViewModeType = t.type({
presenceControl: t.string,
});

//
// END Special Types used for Module Services
//
Expand All @@ -56,10 +60,12 @@ type NotificationSettingPropertyValueType = t.TypeOf<
type NotificationDataPropertyValueType = t.TypeOf<
typeof NotificationDataPropertyValueType
>;
type ViewModeType = t.TypeOf<typeof ViewModeType>;

export {
UserSetting,
BaseProperty,
NotificationSettingPropertyValueType,
NotificationDataPropertyValueType,
ViewModeType,
};

0 comments on commit 8bf5e3f

Please sign in to comment.