Skip to content

Commit

Permalink
add user settings module
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Spahr committed Apr 27, 2021
1 parent 82b04a1 commit 46aea3a
Show file tree
Hide file tree
Showing 27 changed files with 8,719 additions and 14,170 deletions.
22,140 changes: 7,971 additions & 14,169 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const routes: Routes = [
loadChildren: () =>
import('./my-profile/my-profile.module').then((m) => m.MyProfileModule),
},
{
path: 'my-settings',
canActivate: [AuthGuard],
loadChildren: () =>
import('./my-settings/my-settings.module').then((m) => m.MySettingsModule),
},
{ path: 'unauthenticated', component: UnauthenticatedComponent },
{
path: '',
Expand Down
1 change: 1 addition & 0 deletions src/app/home.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('HomeComponent', () => {
'/evaluate-absences',
'/my-absences',
'/my-profile',
'/my-settings',
]);
});

Expand Down
1 change: 1 addition & 0 deletions src/app/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export class HomeComponent {
'evaluate-absences',
'my-absences',
'my-profile',
'my-settings'
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<div
class="erz-container erz-container-limited erz-container-padding-y erz-container-padding-x-responsive"
*erzLet="{
notificationFormGroup: notificationFormGroup$ | async
} as data"
>
<!-- setting-header -->
<h5 class="m-0" class="setting-header">
{{ 'my-settings.notifications.title' | translate }}
</h5>

<form
*ngIf="data.notificationFormGroup"
[formGroup]="data.notificationFormGroup"
(ngSubmit)="onSubmit()"
>

<!-- setting-entry -->
<div class="checkbox-setting">
<input
id="my-settings-notifications-gui"
type="checkbox"
class="checkbox-setting-input"
formControlName="notificationsGui"
/>
<label class="checkbox-setting-label" id="my-settings-notifications-gui">
{{ 'my-settings.notifications.gui' | translate }}
</label>
</div>

<!-- setting-entry -->
<div class="checkbox-setting">
<input
id="my-settings-notifications-mail"
type="checkbox"
class="checkbox-setting-input"
formControlName="notificationsMail"
/>
<label class="checkbox-setting-label" id="my-settings-notifications-mail">
{{ 'my-settings.notifications.mail' | translate }}
</label>
</div>

<!-- setting-entry -->
<div class="checkbox-setting">
<input
id="my-settings-notifications-phoneMobile"
type="checkbox"
class="checkbox-setting-input"
formControlName="notificationsPhoneMobile"
/>
<label
class="checkbox-setting-label"
id="my-settings-notifications-phoneMobile"
>
{{ 'my-settings.notifications.phoneMobile' | translate }}
</label>
</div>
<div class="d-flex justify-content-end">
<button
type="submit"
class="btn btn-primary ml-2"
[disabled]="saving$ | async"
>
{{ 'my-settings.notifications.save' | translate }}
<div
*ngIf="saving$ | async"
class="spinner-border spinner-border-sm align-middle"
role="status"
>
<span class="sr-only">Loading...</span>
</div>
</button>
</div>

</form>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@import "../../../../bootstrap-variables";

.setting-header {
padding-bottom: 1rem;
}

.checkbox-setting {
margin: 0;
padding: 0.6rem $spacer/2 0 0;
}

.checkbox-setting-label {
margin: 0;
padding: 0.6rem $spacer/2 0 0;
padding-left: 1.2rem;
}

.checkbox-setting input.checkbox-setting-input {
// Make the following statements !important since the are
// overwritten in Evento as such
position: static !important;
margin: 0 !important;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { of } from 'rxjs';
import { buildTestModuleMetadata } from 'src/spec-helpers';
import { MySettingsService } from '../../services/my-settings.service';
import { MySettingsNotificationsComponent } from './my-settings-notifications.component';

describe('MySettingsNotificationsComponent', () => {
let fixture: ComponentFixture<MySettingsNotificationsComponent>;
let element: HTMLElement;
let settingsService: jasmine.SpyObj<MySettingsService>;

settingsService = jasmine.createSpyObj('MySettingsService', ['getCurrentNotificationSettingsPropertyValue','updateCurrentNotificationSettingsPropertyValue']);
settingsService.getCurrentNotificationSettingsPropertyValue.and.returnValue(of({ mail: true, gui: true, phoneMobile: false }));
settingsService.updateCurrentNotificationSettingsPropertyValue.and.returnValue(of({}));

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule(
buildTestModuleMetadata({
declarations: [MySettingsNotificationsComponent],
providers: [
{
provide: MySettingsService,
useValue: settingsService,
}
],
})
).compileComponents();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(MySettingsNotificationsComponent);
element = fixture.debugElement.nativeElement;
fixture.detectChanges();
});

it('renders form fields with current values', () => {
expect(getInput('notificationsGui').checked).toBe(true);
expect(getInput('notificationsMail').checked).toBe(true);
expect(getInput('notificationsPhoneMobile').checked).toBe(false);
});

it('updates settings on submit and reloads', () => {
changeValue('notificationsGui', false);
changeValue('notificationsMail', false);
changeValue('notificationsPhoneMobile', true);
clickSubmitButton();

expect(settingsService.updateCurrentNotificationSettingsPropertyValue
).toHaveBeenCalled();
});

function getInput(name: string): HTMLInputElement {
const field = element.querySelector(`input[formControlName="${name}"]`);
expect(field).not.toBeNull();
return field as HTMLInputElement;
}

function changeValue(name: string, value: any): void {
const input = getInput(name);
input.value = value;
input.dispatchEvent(new Event('input'));
fixture.detectChanges();
}

function clickSubmitButton(): void {
const button = element.querySelector(
'.btn-primary'
) as Option<HTMLButtonElement>;
if (button) {
button.click();
fixture.detectChanges();
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { BehaviorSubject } from 'rxjs';
import {
NotificationPropertyValueType,
} from 'src/app/shared/models/user-setting.model';
import { MySettingsService } from '../../services/my-settings.service';
import {
shareReplay,
map,
take,
finalize,
} from 'rxjs/operators';
import { ToastrService } from 'ngx-toastr';
import { TranslateService } from '@ngx-translate/core';


@Component({
selector: 'erz-my-settings-notifications',
templateUrl: './my-settings-notifications.component.html',
styleUrls: ['./my-settings-notifications.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MySettingsNotificationsComponent implements OnInit {

notificationSettings$ = this.settingsService.getCurrentNotificationSettingsPropertyValue();
notificationFormGroup$ = this.notificationSettings$.pipe(
map(this.createNotificationFormGroup.bind(this)),
shareReplay(1)
);

saving$ = new BehaviorSubject(false);
private submitted$ = new BehaviorSubject(false);

constructor(
private settingsService: MySettingsService,
private formBuilder: FormBuilder,
private toastr: ToastrService,
private translate: TranslateService
) {}

ngOnInit(): void { }

private createNotificationFormGroup(notifications: NotificationPropertyValueType): FormGroup {
return this.formBuilder.group({
notificationsGui: [notifications.gui],
notificationsMail: [notifications.mail],
notificationsPhoneMobile: [notifications.phoneMobile],
});
}

onSubmit(): void {
this.submitted$.next(true);
this.notificationFormGroup$.pipe(take(1)).subscribe((formGroup) => {
if (formGroup.valid) {
const { notificationsGui, notificationsMail, notificationsPhoneMobile } = formGroup.value;
this.save(notificationsGui, notificationsMail, notificationsPhoneMobile);
}
});
}

private save(
gui: boolean,
mail: boolean,
phoneMobile: boolean
): void {
this.saving$.next(true);
this.settingsService.updateCurrentNotificationSettingsPropertyValue(
gui, mail, phoneMobile
).pipe(
finalize(() => this.saving$.next(false))
)
.subscribe(this.onSaveSuccess.bind(this));
}

private onSaveSuccess(): void {
this.toastr.success(this.translate.instant('my-settings.notifications.save-success'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<erz-my-settings-notifications></erz-my-settings-notifications>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { MySettingsShowComponent } from './my-settings-show.component';

describe('MySettingsShowComponent', () => {
let component: MySettingsShowComponent;
let fixture: ComponentFixture<MySettingsShowComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ MySettingsShowComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(MySettingsShowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'erz-my-settings-show',
templateUrl: './my-settings-show.component.html',
styleUrls: ['./my-settings-show.component.scss']
})
export class MySettingsShowComponent implements OnInit {

ngOnInit(): void { }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<router-outlet></router-outlet>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';

import { MySettingsComponent } from './my-settings.component';
import { buildTestModuleMetadata } from '../../../../spec-helpers';

describe('MySettingsComponent', () => {
let component: MySettingsComponent;
let fixture: ComponentFixture<MySettingsComponent>;

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule(
buildTestModuleMetadata({
declarations: [MySettingsComponent],
})
).compileComponents();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(MySettingsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { MySettingsService } from '../../services/my-settings.service';

@Component({
selector: 'erz-my-setting',
templateUrl: './my-settings.component.html',
styleUrls: ['./my-settings.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [MySettingsService],
})
export class MySettingsComponent implements OnInit {

constructor() { }

ngOnInit(): void {
}

}
Loading

0 comments on commit 46aea3a

Please sign in to comment.