Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Commit current state of exercise edit component

Roland Geidere1f3a7d06d04
diff --git a/angular/src/app/exercise/exercise-edit/exercise-edit.component.css b/angular/src/app/exercise/exercise-edit/exercise-edit.component.css
new file mode 100644
index 000000000..e69de29bb
diff --git a/angular/src/app/exercise/exercise-edit/exercise-edit.component.html b/angular/src/app/exercise/exercise-edit/exercise-edit.component.html
new file mode 100644
index 000000000..3d60952a6
--- /dev/null
+++ b/angular/src/app/exercise/exercise-edit/exercise-edit.component.html
@@ -0,0 +1,69 @@
+<form [formGroup]="group" (ngSubmit)="onSubmit()">
+
+ <div class="form-group">
+ <div class="form-group">
+ <label for="name">Name</label>
+ <input
+ type="text"
+ id="name"
+ formControlName="name"
+ class="form-control">
+ </div>
+
+ <div class="form-group">
+ <label for="description">Description</label>
+ <textarea
+ type="text"
+ id="description"
+ class="form-control"
+ formControlName="description"
+ rows="6"></textarea>
+ </div>
+
+ <div class="form-group">
+ <label for="category">Categories dropdown:</label>
+ <select formControlName="category" class="form-control" id="category">
+ <option *ngFor="let category of categories"
+ value="{{category.id}}"
+ [selected]="category.id === exercise.category.id">
+ {{category.name}}
+ </option>
+ </select>
+
+ </div>
+
+
+
+ <p>Muscles</p>
+ <div formArrayName="muscles" *ngFor="let order of musclesFormArray.controls; let i = index" class="form-check">
+ <input type="checkbox" [formControlName]="i" [value]="muscles[i].id" class="form-check-input" id="muscle-{{i}}">
+ <label class="form-check-label" for="muscle-{{i}}">
+ {{muscles[i].name}}
+ </label>
+ </div>
+
+ <p>Secondary Muscles</p>
+ <div formArrayName="musclesSecondary" *ngFor="let order of musclesSecondaryFormArray.controls; let i = index" class="form-check">
+ <input type="checkbox" [formControlName]="i" [value]="muscles[i].id" class="form-check-input" id="secondary-muscle-{{i}}">
+ <label class="form-check-label" for="secondary-muscle-{{i}}">
+ {{muscles[i].name}}
+ </label>
+ </div>
+
+ <p>Equipment</p>
+ <div formArrayName="equipment" *ngFor="let order of equipmentFormArray.controls; let i = index" class="form-check">
+ <input type="checkbox" [formControlName]="i" [value]="equipment[i].id" class="form-check-input">
+ <label class="form-check-label">
+ {{equipment[i].name}}
+ </label>
+ </div>
+
+
+ <button
+ type="submit"
+ class="btn btn-success"
+ [disabled]="!group.valid">Save</button>
+ </div>
+
+
+</form>
diff --git a/angular/src/app/exercise/exercise-edit/exercise-edit.component.spec.ts b/angular/src/app/exercise/exercise-edit/exercise-edit.component.spec.ts
new file mode 100644
index 000000000..b1a5b9cbc
--- /dev/null
+++ b/angular/src/app/exercise/exercise-edit/exercise-edit.component.spec.ts
@@ -0,0 +1,25 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+
+import { ExerciseEditComponent } from './exercise-edit.component';
+
+describe('ExerciseEditComponent', () => {
+ let component: ExerciseEditComponent;
+ let fixture: ComponentFixture<ExerciseEditComponent>;
+
+ beforeEach(async () => {
+ await TestBed.configureTestingModule({
+ declarations: [ ExerciseEditComponent ]
+ })
+ .compileComponents();
+ });
+
+ beforeEach(() => {
+ fixture = TestBed.createComponent(ExerciseEditComponent);
+ component = fixture.componentInstance;
+ fixture.detectChanges();
+ });
+
+ it('should create', () => {
+ expect(component).toBeTruthy();
+ });
+});
diff --git a/angular/src/app/exercise/exercise-edit/exercise-edit.component.ts b/angular/src/app/exercise/exercise-edit/exercise-edit.component.ts
new file mode 100644
index 000000000..a625e288a
--- /dev/null
+++ b/angular/src/app/exercise/exercise-edit/exercise-edit.component.ts
@@ -0,0 +1,135 @@
+/*
+ * This file is part of wger Workout Manager <https://github.com/wger-project>.
+ * Copyright (C) 2020, 2021 wger Team
+ *
+ * wger Workout Manager is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * wger Workout Manager is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+import {Component, Input, OnInit} from '@angular/core';
+import {FormArray, FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
+import {ExerciseService} from '../exercise.service';
+import {Category} from '../models/category.model';
+import {Equipment} from '../models/equipment.model';
+import {Exercise} from '../models/exercise.model';
+import {Muscle} from '../models/muscle.model';
+
+@Component({
+ selector: 'app-exercise-edit',
+ templateUrl: './exercise-edit.component.html',
+ styleUrls: ['./exercise-edit.component.css']
+})
+export class ExerciseEditComponent implements OnInit {
+
+ @Input() exercise!: Exercise;
+
+ group!: FormGroup;
+ categories: Category[] = [];
+ muscles: Muscle[] = [];
+ equipment: Equipment[] = [];
+
+ constructor(private exerciseService: ExerciseService,
+ private formBuilder: FormBuilder) {
+
+ }
+
+ ngOnInit(): void {
+ this.categories = this.exerciseService.categories;
+ this.muscles = this.exerciseService.muscles;
+ this.equipment = this.exerciseService.equipment;
+
+ this.initForm();
+ }
+
+
+ get musclesFormArray() {
+ return this.group.controls.muscles as FormArray;
+ }
+
+ get musclesSecondaryFormArray() {
+ return this.group.controls.musclesSecondary as FormArray;
+ }
+
+ get equipmentFormArray() {
+ return this.group.controls.equipment as FormArray;
+ }
+
+ private addCheckboxes() {
+
+
+ // Equipment
+ this.equipment.forEach((value) => {
+ this.equipmentFormArray.push(new FormControl(!!this.exercise.equipment.find(value1 => value1.id == value.id)));
+ });
+
+ // Muscles
+ this.muscles.forEach((value) => {
+ this.musclesFormArray.push(new FormControl(!!this.exercise.muscles.find(value1 => value1.id == value.id)));
+ });
+
+ // Secondary Muscles
+ this.muscles.forEach((value) => {
+ this.musclesSecondaryFormArray.push(new FormControl(!!this.exercise.musclesSecondary.find(value1 => value1.id == value.id)));
+ });
+ }
+
+
+ onSubmit() {
+
+ //console.log(this.group.value);
+
+ const selectedEquipmentIds = this.group.value.equipment
+ .map((checked: boolean, i: number) => checked ? this.equipment[i].id : null)
+ .filter((v: null) => v !== null);
+
+ //console.log(selectedEquipmentIds);
+
+ const selectedMusclesIds = this.group.value.muscles
+ .map((checked: boolean, i: number) => checked ? this.muscles[i].id : null)
+ .filter((v: null) => v !== null);
+
+ //console.log(selectedMusclesIds);
+
+ const selectedSecondaryMusclesIds = this.group.value.musclesSecondary
+ .map((checked: boolean, i: number) => checked ? this.muscles[i].id : null)
+ .filter((v: null) => v !== null);
+
+ //console.log(selectedSecondaryMusclesIds);
+
+
+ //if (this.editMode) {
+ if (this.exercise.id != null) {
+ this.exerciseService.updateExercise(this.group.value);
+ } else {
+ this.exerciseService.addExercise(this.group.value);
+ }
+
+
+ }
+
+ private initForm() {
+
+ this.group = this.formBuilder.group({
+ name: this.exercise.name,
+ description: this.exercise.description,
+ category: this.exercise.category.id,
+ muscles: new FormArray([]),
+ musclesSecondary: new FormArray([]),
+ equipment: new FormArray([]),
+ });
+
+ this.addCheckboxes();
+ }
+
+
+}
diff --git a/angular/src/app/exercise/exercise.service.ts b/angular/src/app/exercise/exercise.service.ts
index 350d69c03..78105159e 100644
--- a/angular/src/app/exercise/exercise.service.ts
+++ b/angular/src/app/exercise/exercise.service.ts
@@ -117,4 +117,11 @@ export class ExerciseService {
getEquipmentById(id: number): Equipment {
return this.equipment.find(value => value.id === id)!;
}
+
+ updateExercise(data: any) {
+
+ }
+ addExercise(data: any) {
+
+ }
}