Self hosted FLOSS fitness/workout, nutrition and weight tracker
Delete weight entries from the database
Roland Geiderf361251f2543
diff --git a/angular/src/app/exercise/models/weight/weight.model.ts b/angular/src/app/weight/models/weight.model.tssimilarity index 96%rename from angular/src/app/exercise/models/weight/weight.model.tsrename to angular/src/app/weight/models/weight.model.tsindex 16c281946..b804cd4c2 100644--- a/angular/src/app/exercise/models/weight/weight.model.ts+++ b/angular/src/app/weight/models/weight.model.ts@@ -17,7 +17,7 @@*/import {Injectable} from '@angular/core';-import {Adapter} from '../../../core/adapter';+import {Adapter} from '../../core/adapter';export class WeightEntry {diff --git a/angular/src/app/weight/weight-detail/weight-detail.component.html b/angular/src/app/weight/weight-detail/weight-detail.component.htmlindex 9048e20d5..8b888f2de 100644--- a/angular/src/app/weight/weight-detail/weight-detail.component.html+++ b/angular/src/app/weight/weight-detail/weight-detail.component.html@@ -1 +1,6 @@-<li class="list-group-item">{{entry.date}} - {{entry.weight}}</li>+<li class="list-group-item">+ {{entry.date}} - {{entry.weight}}+ <button class="btn btn-danger" (click)="deleteEntry()">+ Delete+ </button>+</li>diff --git a/angular/src/app/weight/weight-detail/weight-detail.component.ts b/angular/src/app/weight/weight-detail/weight-detail.component.tsindex bbcada2a1..20f955e74 100644--- a/angular/src/app/weight/weight-detail/weight-detail.component.ts+++ b/angular/src/app/weight/weight-detail/weight-detail.component.ts@@ -1,5 +1,6 @@import {Component, Input, OnInit} from '@angular/core';-import {WeightEntry} from '../../exercise/models/weight/weight.model';+import {WeightEntry} from '../models/weight.model';+import {WeightService} from '../weight.service';@Component({selector: 'app-weight-detail',@@ -10,9 +11,18 @@ export class WeightDetailComponent implements OnInit {@Input() entry!: WeightEntry;- constructor() { }+ constructor(private weightService: WeightService) {+ }ngOnInit(): void {}+ /**+ * Deletes this weight entry+ */+ deleteEntry() {+ this.weightService.deleteWeightEntry(this.entry.id);+ }++}diff --git a/angular/src/app/weight/weight-list/weight-list.component.ts b/angular/src/app/weight/weight-list/weight-list.component.tsindex 73886bf86..295cbab63 100644--- a/angular/src/app/weight/weight-list/weight-list.component.ts+++ b/angular/src/app/weight/weight-list/weight-list.component.ts@@ -1,5 +1,5 @@import { Component, OnInit } from '@angular/core';-import {WeightEntry} from '../../exercise/models/weight/weight.model';+import {WeightEntry} from '../models/weight.model';import {WeightService} from '../weight.service';@Component({diff --git a/angular/src/app/weight/weight.service.ts b/angular/src/app/weight/weight.service.tsindex d6ce92b87..cdecaff35 100644--- a/angular/src/app/weight/weight.service.ts+++ b/angular/src/app/weight/weight.service.ts@@ -1,8 +1,7 @@-import {HttpClient} from '@angular/common/http';-import { HttpHeaders } from '@angular/common/http';+import {HttpClient, HttpHeaders} from '@angular/common/http';import {Injectable} from '@angular/core';import {environment} from '../../environments/environment';-import {WeightAdapter, WeightEntry} from '../exercise/models/weight/weight.model';+import {WeightAdapter, WeightEntry} from './models/weight.model';@Injectable({@@ -15,21 +14,14 @@ export class WeightService {constructor(private http: HttpClient,private weightAdapter: WeightAdapter,- ) {+ ) {}-async loadWeightEntries(): Promise<WeightEntry[]> {- const data = await this.http.get<any>(this.weightEntryUrl, {params: {limit: 10}, headers: new HttpHeaders({- 'Content-Type': 'application/json',- Authorization: 'Token d2e9db08e3c1eea2adb62e60e75fa8922af8bbd5'- })}).toPromise();-+ const data = await this.http.get<any>(this.weightEntryUrl, {params: {limit: 10}, headers: environment.headers}).toPromise();for (const weightData of data.results) {- console.log(weightData);-this.entries.push(this.weightAdapter.fromJson(weightData));}@@ -39,7 +31,27 @@ export class WeightService {updateWeightEntry(data: any) {}+addWeightEntry(data: any) {}++ /**+ * Deletes the weight entry with the given ID+ *+ * @param id: ID of the weight entry+ */+ deleteWeightEntry(id: number) {++ this.http.delete<any>(this.weightEntryUrl + '/' + id + '/', {+ headers: environment.headers+ }).subscribe();+++ this.entries.forEach((value: WeightEntry, index: number) => {+ if (value.id == id) {+ this.entries.splice(index, 1);+ }+ });+ }}diff --git a/angular/src/environments/environment.interface.ts b/angular/src/environments/environment.interface.tsindex 1c46f7243..ca45aa354 100644--- a/angular/src/environments/environment.interface.ts+++ b/angular/src/environments/environment.interface.ts@@ -1,8 +1,10 @@+import {HttpHeaders} from '@angular/common/http';export interface Environment {production: boolean;name: string;apiUrl: string;+ headers: HttpHeaders}diff --git a/angular/src/environments/environment.prod.ts b/angular/src/environments/environment.prod.tsindex 4704ed85d..12b590852 100644--- a/angular/src/environments/environment.prod.ts+++ b/angular/src/environments/environment.prod.ts@@ -1,8 +1,12 @@+import {HttpHeaders} from '@angular/common/http';import {Environment} from './environment.interface';export const environment: Environment = {production: false,name: 'prod',- apiUrl: '/api/v2/'+ apiUrl: '/api/v2/',+ headers: new HttpHeaders({+ 'Content-Type': 'application/json',+ }),};diff --git a/angular/src/environments/environment.ts b/angular/src/environments/environment.tsindex 53ec6e1f9..04c6d67bd 100644--- a/angular/src/environments/environment.ts+++ b/angular/src/environments/environment.ts@@ -1,3 +1,4 @@+import {HttpHeaders} from '@angular/common/http';import {Environment} from './environment.interface';/**@@ -7,7 +8,11 @@ import {Environment} from './environment.interface';export const environment: Environment = {production: false,name: 'dev',- apiUrl: 'http://localhost:8000/api/v2/'+ apiUrl: 'http://localhost:8000/api/v2/',+ headers: new HttpHeaders({+ 'Content-Type': 'application/json',+ Authorization: 'Token d2e9db08e3c1eea2adb62e60e75fa8922af8bbd5'+ }),};/*