Work on local motion copy

This commit is contained in:
Sean Engelhardt 2018-08-24 12:21:56 +02:00
parent 52df9dee68
commit 5fc85f2621
2 changed files with 42 additions and 25 deletions

View File

@ -56,7 +56,7 @@
{{motion.identifier}}
</div>
<mat-form-field *ngIf="editMotion">
<input matInput placeholder='Identifier' formControlName='identifier' [value]='motion.identifier'>
<input matInput placeholder='Identifier' formControlName='identifier' [value]='motionCopy.identifier'>
</mat-form-field>
</div>
@ -80,9 +80,9 @@
</div>
<mat-form-field *ngIf="editMotion && !newMotion">
<mat-select placeholder='State' formControlName='state_id'>
<mat-option [value]="motion.state.id">{{motion.state}}</mat-option>
<mat-option [value]="motionCopy.state.id">{{motionCopy.state}}</mat-option>
<mat-divider></mat-divider>
<mat-option *ngFor="let state of motion.nextStates" [value]="state.id">{{state}}</mat-option>
<mat-option *ngFor="let state of motionCopy.nextStates" [value]="state.id">{{state}}</mat-option>
<mat-divider></mat-divider>
<mat-option>
<fa-icon icon='exclamation-triangle'></fa-icon> <span translate>Reset State</span>
@ -100,8 +100,10 @@
</div>
<mat-form-field *ngIf="motion && editMotion">
<mat-select placeholder='Recommendation' formControlName='recommendation_id'>
<mat-option *ngFor="let state of motion.nextStates" [value]="state.id">{{state}}</mat-option>
<mat-option *ngFor="let state of motionCopy.nextStates" [value]="state.id">{{state}}</mat-option>
<mat-divider></mat-divider>
<!-- TODO has no effect -->
<mat-option>
<fa-icon icon='exclamation-triangle'></fa-icon>
<span translate>Reset recommendation</span>
@ -132,7 +134,7 @@
{{motion.origin}}
</div>
<mat-form-field *ngIf="editMotion">
<input matInput placeholder='Origin' formControlName='origin' [value]='motion.origin'>
<input matInput placeholder='Origin' formControlName='origin' [value]='motionCopy.origin'>
</mat-form-field>
</div>
@ -171,7 +173,7 @@
<h2>{{motion.currentTitle}}</h2>
</div>
<mat-form-field *ngIf="editMotion" class='wide-text'>
<input matInput placeholder='Title' formControlName='currentTitle' [value]='motion.currentTitle'>
<input matInput placeholder='Title' formControlName='currentTitle' [value]='motionCopy.currentTitle'>
</mat-form-field>
</div>
@ -182,7 +184,7 @@
<div [innerHtml]='motion.currentText'></div>
</div>
<mat-form-field *ngIf="motion && editMotion" class='wide-text'>
<textarea matInput placeholder='Motion Text' formControlName='currentText' [value]='motion.currentText'></textarea>
<textarea matInput placeholder='Motion Text' formControlName='currentText' [value]='motionCopy.currentText'></textarea>
</mat-form-field>
<!-- Reason -->
@ -192,7 +194,7 @@
<div [innerHtml]='motion.currentReason'></div>
</div>
<mat-form-field *ngIf="editMotion" class='wide-text'>
<textarea matInput placeholder="Reason" formControlName='currentReason' [value]='motion.currentReason'></textarea>
<textarea matInput placeholder="Reason" formControlName='currentReason' [value]='motionCopy.currentReason'></textarea>
</mat-form-field>
</div>

View File

@ -3,7 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router';
import { BaseComponent } from '../../../base.component';
import { Motion } from '../../../shared/models/motions/motion';
import { Category } from '../../../shared/models/motions/category';
import { FormGroup, FormBuilder } from '@angular/forms';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MatExpansionPanel } from '@angular/material';
import { DataSendService } from '../../../core/services/data-send.service';
@ -31,6 +31,11 @@ export class MotionDetailComponent extends BaseComponent implements OnInit {
*/
motion: Motion;
/**
* Copy of the motion that the user might edit
*/
motionCopy: Motion;
/**
* Motions meta-info
*/
@ -70,7 +75,10 @@ export class MotionDetailComponent extends BaseComponent implements OnInit {
if (route.snapshot.url[0].path === 'new') {
this.newMotion = true;
this.editMotion = true;
// Both are (temporarily) necessary until submitter and supporters are implemented
this.motion = new Motion();
this.motionCopy = new Motion();
} else {
// load existing motion
this.route.params.subscribe(params => {
@ -92,18 +100,21 @@ export class MotionDetailComponent extends BaseComponent implements OnInit {
/**
* Async load the values of the motion in the Form.
*/
patchForm() {
patchForm(formMotion: Motion) {
console.log('Motion: ', this.motion);
console.log('category_id: ', formMotion);
this.metaInfoForm.patchValue({
category_id: this.motion.category.id,
state_id: this.motion.state.id,
recommendation_id: this.motion.recommendation.id,
identifier: this.motion.identifier,
origin: this.motion.origin
category_id: formMotion.category.id,
state_id: formMotion.state.id,
recommendation_id: formMotion.recommendation.id,
identifier: formMotion.identifier,
origin: formMotion.origin
});
this.contentForm.patchValue({
currentTitle: this.motion.currentTitle,
currentText: this.motion.currentText,
currentReason: this.motion.currentReason
currentTitle: formMotion.currentTitle,
currentText: formMotion.currentText,
currentReason: formMotion.currentReason
});
}
@ -121,8 +132,8 @@ export class MotionDetailComponent extends BaseComponent implements OnInit {
origin: ['']
});
this.contentForm = this.formBuilder.group({
currentTitle: [''],
currentText: [''],
currentTitle: ['', Validators.required],
currentText: ['', Validators.required],
currentReason: ['']
});
}
@ -138,13 +149,14 @@ export class MotionDetailComponent extends BaseComponent implements OnInit {
*/
saveMotion() {
const newMotionValues = { ...this.metaInfoForm.value, ...this.contentForm.value };
this.motion.patchValues(newMotionValues);
this.motionCopy.patchValues(newMotionValues);
// TODO: This is DRAFT. Reads out Motion version directly. Potentially insecure.
this.motion.title = this.motion.currentTitle;
this.motion.text = this.motion.currentText;
this.motionCopy.title = this.motionCopy.currentTitle;
this.motionCopy.text = this.motionCopy.currentText;
this.dataSend.saveModel(this.motion).subscribe(answer => {
// TODO: send to normal motion to verify
this.dataSend.saveModel(this.motionCopy).subscribe(answer => {
if (answer && answer.id && this.newMotion) {
this.router.navigate(['./motions/' + answer.id]);
}
@ -165,7 +177,10 @@ export class MotionDetailComponent extends BaseComponent implements OnInit {
editMotionButton() {
this.editMotion ? (this.editMotion = false) : (this.editMotion = true);
if (this.editMotion) {
this.patchForm();
// copy the motion
this.motionCopy = new Motion();
this.motionCopy.patchValues(this.motion);
this.patchForm(this.motionCopy);
this.metaInfoPanel.open();
this.contentPanel.open();
} else {