Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Allow editing and adding weight entries

Roland Geider131db76eae88
diff --git a/angular/package.json b/angular/package.json
index a34317fb6..3de08ecf9 100644
--- a/angular/package.json
+++ b/angular/package.json
@@ -1,6 +1,6 @@
{
- "name": "angular",
- "version": "0.0.0",
+ "name": "wger-angular",
+ "version": "0.0.1",
"scripts": {
"ng": "ng",
"start": "ng serve",
diff --git a/angular/src/app/weight/models/weight.model.ts b/angular/src/app/weight/models/weight.model.ts
index b804cd4c2..80532bb8b 100644
--- a/angular/src/app/weight/models/weight.model.ts
+++ b/angular/src/app/weight/models/weight.model.ts
@@ -22,9 +22,9 @@ import {Adapter} from '../../core/adapter';
export class WeightEntry {
constructor(
- public id: number,
public date: Date,
- public weight: number
+ public weight: number,
+ public id?: number,
) { }
}
@@ -34,9 +34,9 @@ export class WeightEntry {
export class WeightAdapter implements Adapter<WeightEntry> {
fromJson(item: any): WeightEntry {
return new WeightEntry(
- item.id,
item.date,
- item.weight
+ item.weight,
+ item.id,
);
}
diff --git a/angular/src/app/weight/weight-detail/weight-detail.component.html b/angular/src/app/weight/weight-detail/weight-detail.component.html
index 8b888f2de..ae6fffde7 100644
--- a/angular/src/app/weight/weight-detail/weight-detail.component.html
+++ b/angular/src/app/weight/weight-detail/weight-detail.component.html
@@ -1,6 +1,11 @@
<li class="list-group-item">
- {{entry.date}} - {{entry.weight}}
<button class="btn btn-danger" (click)="deleteEntry()">
Delete
</button>
+ <button class="btn btn-primary" (click)="toggleForm()">Edit</button>
+
+ <div *ngIf="!showForm">
+ {{entry.date}} - {{entry.weight}}
+ </div>
+ <app-weight-edit *ngIf="showForm" [weight]="entry"></app-weight-edit>
</li>
diff --git a/angular/src/app/weight/weight-detail/weight-detail.component.ts b/angular/src/app/weight/weight-detail/weight-detail.component.ts
index 20f955e74..bdb7a277f 100644
--- a/angular/src/app/weight/weight-detail/weight-detail.component.ts
+++ b/angular/src/app/weight/weight-detail/weight-detail.component.ts
@@ -10,6 +10,7 @@ import {WeightService} from '../weight.service';
export class WeightDetailComponent implements OnInit {
@Input() entry!: WeightEntry;
+ showForm = false;
constructor(private weightService: WeightService) {
}
@@ -17,11 +18,15 @@ export class WeightDetailComponent implements OnInit {
ngOnInit(): void {
}
+ toggleForm() {
+ this.showForm = !this.showForm;
+ }
+
/**
* Deletes this weight entry
*/
deleteEntry() {
- this.weightService.deleteWeightEntry(this.entry.id);
+ this.weightService.deleteWeightEntry(this.entry.id!);
}
diff --git a/angular/src/app/weight/weight-edit/weight-edit.component.css b/angular/src/app/weight/weight-edit/weight-edit.component.css
new file mode 100644
index 000000000..e69de29bb
diff --git a/angular/src/app/weight/weight-edit/weight-edit.component.html b/angular/src/app/weight/weight-edit/weight-edit.component.html
new file mode 100644
index 000000000..43557cf93
--- /dev/null
+++ b/angular/src/app/weight/weight-edit/weight-edit.component.html
@@ -0,0 +1,28 @@
+<form [formGroup]="group" (ngSubmit)="onSubmit()">
+
+ <div class="form-group">
+ <div class="form-group">
+ <label for="date">Date</label>
+ <input
+ type="date"
+ id="date"
+ formControlName="date"
+ class="form-control">
+ </div>
+
+ <div class="form-group">
+ <label for="weight">Weight</label>
+ <input
+ type="text"
+ id="weight"
+ formControlName="weight"
+ class="form-control">
+ </div>
+
+ <button
+ type="submit"
+ class="btn btn-success"
+ [disabled]="group.invalid">Save
+ </button>
+ </div>
+</form>
diff --git a/angular/src/app/weight/weight-edit/weight-edit.component.spec.ts b/angular/src/app/weight/weight-edit/weight-edit.component.spec.ts
new file mode 100644
index 000000000..f06ad2ca0
--- /dev/null
+++ b/angular/src/app/weight/weight-edit/weight-edit.component.spec.ts
@@ -0,0 +1,25 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { WeightEditComponent } from './weight-edit.component';
+
+describe('WeightEditComponent', () => {
+ let component: WeightEditComponent;
+ let fixture: ComponentFixture<WeightEditComponent>;
+
+ beforeEach(async () => {
+ await TestBed.configureTestingModule({
+ declarations: [ WeightEditComponent ]
+ })
+ .compileComponents();
+ });
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(WeightEditComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
diff --git a/angular/src/app/weight/weight-edit/weight-edit.component.ts b/angular/src/app/weight/weight-edit/weight-edit.component.ts
new file mode 100644
index 000000000..b46f51b46
--- /dev/null
+++ b/angular/src/app/weight/weight-edit/weight-edit.component.ts
@@ -0,0 +1,63 @@
+import {Component, Input, OnInit} from '@angular/core';
+import {FormBuilder, FormGroup, Validators} from '@angular/forms';
+import {WeightEntry} from '../models/weight.model';
+import {WeightService} from '../weight.service';
+
+@Component({
+ selector: 'app-weight-edit',
+ templateUrl: './weight-edit.component.html',
+ styleUrls: ['./weight-edit.component.css']
+})
+export class WeightEditComponent implements OnInit {
+
+ @Input() weight?: WeightEntry;
+
+ group!: FormGroup;
+
+ constructor(private service: WeightService,
+ private formBuilder: FormBuilder,
+ ) { }
+
+ ngOnInit(): void {
+
+ if(!this.weight) {
+ console.log("thi weight is not defined");
+ this.weight = new WeightEntry(new Date(), 0);
+ }
+
+ this.initForm();
+ }
+
+ private initForm() {
+
+ this.group = this.formBuilder.group({
+ date: [this.weight!.date, Validators.required],
+ weight: [
+ this.weight!.weight,
+ [Validators.required, Validators.min(30), Validators.max(300)],
+ ],
+ });
+ }
+
+ onSubmit() {
+
+ if (this.group.invalid) {
+ return;
+ }
+
+ this.weight!.weight = this.group.value.weight;
+ this.weight!.date = this.group.value.date;
+
+
+ if(this.weight?.id) {
+ console.log('editing weight entry ' + this.weight.id);
+ this.service.updateWeightEntry(this.weight);
+ } else {
+ console.log('adding new weight entry');
+ this.service.addWeightEntry(this.group.value);
+ }
+
+ this.group.reset();
+ }
+
+}
diff --git a/angular/src/app/weight/weight-list/weight-list.component.html b/angular/src/app/weight/weight-list/weight-list.component.html
index 5382bbf28..21161a395 100644
--- a/angular/src/app/weight/weight-list/weight-list.component.html
+++ b/angular/src/app/weight/weight-list/weight-list.component.html
@@ -3,3 +3,6 @@
class="list-group">
<app-weight-detail [entry]="entry"></app-weight-detail>
</ul>
+
+<button class="btn btn-primary" (click)="toggleForm()">Add entry</button>
+<app-weight-edit *ngIf="showForm"></app-weight-edit>
diff --git a/angular/src/app/weight/weight-list/weight-list.component.ts b/angular/src/app/weight/weight-list/weight-list.component.ts
index 295cbab63..1941b79a8 100644
--- a/angular/src/app/weight/weight-list/weight-list.component.ts
+++ b/angular/src/app/weight/weight-list/weight-list.component.ts
@@ -9,6 +9,7 @@ import {WeightService} from '../weight.service';
})
export class WeightListComponent implements OnInit {
weightEntries: WeightEntry[] = [];
+ showForm = false;
constructor(private service: WeightService) { }
@@ -16,6 +17,10 @@ export class WeightListComponent implements OnInit {
this.getWeightEntries();
}
+ toggleForm() {
+ this.showForm = !this.showForm;
+ }
+
async getWeightEntries(): Promise<void> {
this.weightEntries = await this.service.loadWeightEntries();
}
diff --git a/angular/src/app/weight/weight.service.ts b/angular/src/app/weight/weight.service.ts
index cdecaff35..adc7cea3b 100644
--- a/angular/src/app/weight/weight.service.ts
+++ b/angular/src/app/weight/weight.service.ts
@@ -1,4 +1,4 @@
-import {HttpClient, HttpHeaders} from '@angular/common/http';
+import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from '../../environments/environment';
import {WeightAdapter, WeightEntry} from './models/weight.model';
@@ -8,7 +8,7 @@ import {WeightAdapter, WeightEntry} from './models/weight.model';
providedIn: 'root',
})
export class WeightService {
- weightEntryUrl = environment.apiUrl + 'weightentry';
+ weightEntryUrl = environment.apiUrl + 'weightentry/';
entries: WeightEntry[] = [];
@@ -17,23 +17,44 @@ export class WeightService {
) {
}
+ sortEntries() {
+ this.entries.sort((a, b) => (a.date > b.date ? -1 : 1));
+ }
+
async loadWeightEntries(): Promise<WeightEntry[]> {
- const data = await this.http.get<any>(this.weightEntryUrl, {params: {limit: 10}, headers: environment.headers}).toPromise();
+ const data = await this.http.get<any>(this.weightEntryUrl, {params: {limit: 500}, headers: environment.headers}).toPromise();
for (const weightData of data.results) {
this.entries.push(this.weightAdapter.fromJson(weightData));
}
-
+ this.sortEntries();
return this.entries;
}
- updateWeightEntry(data: any) {
-
+ /**
+ * Updates an existing weight entry
+ *
+ * @param weight a [WeightEntry] instance
+ */
+ updateWeightEntry(weight: WeightEntry) {
+ this.http.patch<any>(this.weightEntryUrl + weight.id + '/', this.weightAdapter.toJson(weight), {
+ headers: environment.headers
+ }).subscribe(value => { });
}
- addWeightEntry(data: any) {
-
+ /**
+ * Inserts a new weight entry into the database
+ *
+ * @param data: the weight entry data
+ */
+ addWeightEntry(data: {weight: number, date: Date}) {
+ this.http.post<any>(this.weightEntryUrl, data, {
+ headers: environment.headers
+ }).subscribe(value => {
+ this.entries.push(this.weightAdapter.fromJson(value));
+ this.sortEntries();
+ });
}
/**
@@ -43,7 +64,7 @@ export class WeightService {
*/
deleteWeightEntry(id: number) {
- this.http.delete<any>(this.weightEntryUrl + '/' + id + '/', {
+ this.http.delete<any>(this.weightEntryUrl + id + '/', {
headers: environment.headers
}).subscribe();