Skip to content

Commit

Permalink
add submit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 28, 2025
1 parent 422cad7 commit 94dee4a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
2 changes: 2 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/models/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public int hashCode() {

@NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
@Size(max = 4096, min = 3, message = MessageKey.ATTRIBUTE_SIZE_BETWEEN)
@NotNull(message = MessageKey.ATTRIBUTE_NOT_NULL)
@Column(nullable = false)
private String unitName;

@CreatedBy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ CREATE TABLE unit
unit_name TEXT NOT NULL,
created_by_id BIGINT,
is_default boolean NOT NULL,
PRIMARY KEY (id),
UNIQUE (unit_name)
PRIMARY KEY (id)
);

create table if not exists person
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ CREATE TABLE unit
unit_name TEXT NOT NULL,
is_default boolean NOT NULL,
created_by_id BIGINT,
PRIMARY KEY (id),
UNIQUE (unit_name)
PRIMARY KEY (id)
);

CREATE SEQUENCE sequence_unit START 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<app-dialog-template-core [title]="'Enheiten verwalten'">
<app-dialog-template-core [title]="'Einheiten verwalten'">
<ng-container content>
<ng-container *ngFor="let unit of allUnits | async; let i = index" class="row mb-3">
<div *ngIf="unit.isDefault">
Expand All @@ -18,9 +18,10 @@
<button
[attr.data-testId]="'submit'"
type="submit"
(click)="saveUnits()"
(click)="submit()"
color="primary"
mat-flat-button
[disabled]="fg.invalid"
>
Speichern
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { UnitService } from '../../services/unit.service';
import { Unit } from '../../shared/types/enums/unit';
import { map, Observable, ReplaySubject } from 'rxjs';
import { forkJoin, map, Observable, ReplaySubject } from 'rxjs';
import { FormArray, FormGroup } from '@angular/forms';
import { FormControlsOf, Item } from '../action-plan/action-plan.component';
import { MatDialogRef } from '@angular/material/dialog';

@Component({
selector: 'app-manage-units-dialog',
Expand All @@ -20,17 +21,31 @@ export class ManageUnitsDialogComponent implements OnInit {
unitFormArray: new FormArray<FormGroup<FormControlsOf<Item>>>([])
});

constructor(private unitService: UnitService) {
constructor(private unitService: UnitService, private dialogRef: MatDialogRef<ManageUnitsDialogComponent>) {
}

saveUnits() {
/*
* this.unitService.checkForNewUnit(this.unitSearchTerm)
* .subscribe((result:Unit)=> this.unitService.createUnit(result)
* .subscribe((unit)=> this.keyResultForm.get('metric')
* ?.get('unit')
* ?.setValue(unit)));
*/
submit() {
const items = this.fg.get('unitFormArray')?.value as Item[];
const units = items.map((i) => {
return { id: i.id,
unitName: i.item } as Unit;
});

forkJoin(this.getNewUnits(units)
.concat(this.getUpdatableUnits(units)))
.subscribe((complete) => {
this.dialogRef.close();
});
}

getUpdatableUnits(units: Unit[]) {
return units.filter((u) => u.id)
.map((u) => this.unitService.updateUnit(u));
}

getNewUnits(units: Unit[]) {
return units.filter((u) => !u.id)
.map((u) => this.unitService.createUnit(u));
}

ngOnInit(): void {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/app/services/unit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class UnitService {
return this.httpClient.post<Unit>(this.API_URL, unit);
}

updateUnit(unit: Unit) {
return this.httpClient.put<Unit>(this.API_URL + '/' + unit.id, unit);
}

checkForNewUnit(unitName: string): Observable<Unit> {
const newUnit = { unitName: unitName,
isDefault: false } as Unit;
Expand Down

0 comments on commit 94dee4a

Please sign in to comment.