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}} {{motion.identifier}}
</div> </div>
<mat-form-field *ngIf="editMotion"> <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> </mat-form-field>
</div> </div>
@ -80,9 +80,9 @@
</div> </div>
<mat-form-field *ngIf="editMotion && !newMotion"> <mat-form-field *ngIf="editMotion && !newMotion">
<mat-select placeholder='State' formControlName='state_id'> <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-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-divider></mat-divider>
<mat-option> <mat-option>
<fa-icon icon='exclamation-triangle'></fa-icon> <span translate>Reset State</span> <fa-icon icon='exclamation-triangle'></fa-icon> <span translate>Reset State</span>
@ -100,8 +100,10 @@
</div> </div>
<mat-form-field *ngIf="motion && editMotion"> <mat-form-field *ngIf="motion && editMotion">
<mat-select placeholder='Recommendation' formControlName='recommendation_id'> <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> <mat-divider></mat-divider>
<!-- TODO has no effect -->
<mat-option> <mat-option>
<fa-icon icon='exclamation-triangle'></fa-icon> <fa-icon icon='exclamation-triangle'></fa-icon>
<span translate>Reset recommendation</span> <span translate>Reset recommendation</span>
@ -132,7 +134,7 @@
{{motion.origin}} {{motion.origin}}
</div> </div>
<mat-form-field *ngIf="editMotion"> <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> </mat-form-field>
</div> </div>
@ -171,7 +173,7 @@
<h2>{{motion.currentTitle}}</h2> <h2>{{motion.currentTitle}}</h2>
</div> </div>
<mat-form-field *ngIf="editMotion" class='wide-text'> <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> </mat-form-field>
</div> </div>
@ -182,7 +184,7 @@
<div [innerHtml]='motion.currentText'></div> <div [innerHtml]='motion.currentText'></div>
</div> </div>
<mat-form-field *ngIf="motion && editMotion" class='wide-text'> <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> </mat-form-field>
<!-- Reason --> <!-- Reason -->
@ -192,7 +194,7 @@
<div [innerHtml]='motion.currentReason'></div> <div [innerHtml]='motion.currentReason'></div>
</div> </div>
<mat-form-field *ngIf="editMotion" class='wide-text'> <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> </mat-form-field>
</div> </div>

View File

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