From fc7f68f7dbf4da94bc162a1932b430368d984129 Mon Sep 17 00:00:00 2001 From: Sean Engelhardt Date: Thu, 23 Aug 2018 10:35:05 +0200 Subject: [PATCH] Read the motion workflow by state --- .../src/app/shared/models/motions/motion.ts | 60 ++++++++++--------- .../shared/models/motions/workflow-state.ts | 16 +++++ .../src/app/shared/models/motions/workflow.ts | 19 ++++++ .../motion-detail.component.html | 15 ++--- .../motion-detail/motion-detail.component.ts | 2 + .../motion-list/motion-list.component.html | 2 +- .../motion-list/motion-list.component.ts | 4 ++ 7 files changed, 83 insertions(+), 35 deletions(-) diff --git a/client/src/app/shared/models/motions/motion.ts b/client/src/app/shared/models/motions/motion.ts index f78e69542..38de71b0a 100644 --- a/client/src/app/shared/models/motions/motion.ts +++ b/client/src/app/shared/models/motions/motion.ts @@ -37,9 +37,7 @@ export class Motion extends BaseModel { agenda_item_id: number; log_messages: MotionLog[]; - // read from config - workflow_id: number; - // by the config above + // dynamic values workflow: Workflow; // for request @@ -100,31 +98,31 @@ export class Motion extends BaseModel { } /** - * sets the workflow_id and the workflow + * sets the and the workflow from either dataStore or WebSocket */ initDataStoreValues() { - const motionsWorkflowConfig = this.DS.filter(Config, config => config.key === 'motions_workflow')[0] as Config; - if (motionsWorkflowConfig) { - this.workflow_id = +motionsWorkflowConfig.value; - } else { - this.DS.getObservable().subscribe(newConfig => { - if (newConfig instanceof Config && newConfig.key === 'motions_workflow') { - this.workflow_id = +newConfig.value; - } - }); - } + // check the containing Workflows in DataStore + const allWorkflows = this.DS.get(Workflow) as Workflow[]; + allWorkflows.forEach(localWorkflow => { + if (localWorkflow.isStateContained(this.state_id)) { + this.workflow = localWorkflow as Workflow; + } + }); - this.workflow = this.DS.get(Workflow, this.workflow_id) as Workflow; - if (!this.workflow.id) { - this.DS.getObservable().subscribe(newModel => { - if (newModel instanceof Workflow && newModel.id === this.workflow_id) { - this.workflow = newModel; + // observe for new models + this.DS.getObservable().subscribe(newModel => { + if (newModel instanceof Workflow) { + if (newModel.isStateContained(this.state_id)) { + this.workflow = newModel as Workflow; } - }); - } + } + }); } - /** add a new motionSubmitter from user-object */ + /** + * add a new motionSubmitter from user-object + * @param user the user + */ addSubmitter(user: User) { const newSubmitter = new MotionSubmitter(null, user.id); this.submitters.push(newSubmitter); @@ -142,6 +140,11 @@ export class Motion extends BaseModel { } } + /** + * Patch the current version + * + * TODO: Altering the current version should be avoided. + */ set currentTitle(newTitle: string) { if (this.versions[0]) { this.versions[0].title = newTitle; @@ -221,9 +224,8 @@ export class Motion extends BaseModel { * return the workflow state */ get state(): any { - if (this.state_id && this.workflow && this.workflow.id) { - const state = this.workflow.state_by_id(this.state_id); - return state; + if (this.workflow) { + return this.workflow.state_by_id(this.state_id); } else { return ''; } @@ -232,8 +234,12 @@ export class Motion extends BaseModel { /** * returns possible states for the motion */ - get possible_states(): WorkflowState[] { - return this.workflow.states; + get nextStates(): WorkflowState[] { + if (this.workflow && this.state) { + return this.state.getNextStates(this.workflow); + } else { + return null; + } } /** diff --git a/client/src/app/shared/models/motions/workflow-state.ts b/client/src/app/shared/models/motions/workflow-state.ts index acc85b614..395ca6fda 100644 --- a/client/src/app/shared/models/motions/workflow-state.ts +++ b/client/src/app/shared/models/motions/workflow-state.ts @@ -1,4 +1,6 @@ import { Deserializable } from '../deserializable.model'; +import { Workflow } from './workflow'; +import { MotionLog } from './motion-log'; /** * Representation of a workflow state @@ -79,6 +81,20 @@ export class WorkflowState implements Deserializable { this.workflow_id = workflow_id; } + /** + * return a list of the next possible states. + * Also adds the current state. + */ + getNextStates(workflow: Workflow): WorkflowState[] { + const nextStates = []; + workflow.states.forEach(state => { + if (this.next_states_id.includes(state.id)) { + nextStates.push(state as WorkflowState); + } + }); + return nextStates; + } + deserialize(input: any): this { Object.assign(this, input); return this; diff --git a/client/src/app/shared/models/motions/workflow.ts b/client/src/app/shared/models/motions/workflow.ts index f1f8bb20f..bc83afbb5 100644 --- a/client/src/app/shared/models/motions/workflow.ts +++ b/client/src/app/shared/models/motions/workflow.ts @@ -21,6 +21,25 @@ export class Workflow extends BaseModel { this.first_state = first_state; } + /** + * Check if the containing @link{WorkflowState}s contain a given ID + * @param id The State ID + */ + isStateContained(obj: number | WorkflowState): boolean { + let id: number; + if (obj instanceof WorkflowState) { + id = obj.id; + } else { + id = obj; + } + + return this.states.some(state => { + if (state.id === id) { + return true; + } + }); + } + state_by_id(id: number): WorkflowState { let targetState; this.states.forEach(state => { diff --git a/client/src/app/site/motions/motion-detail/motion-detail.component.html b/client/src/app/site/motions/motion-detail/motion-detail.component.html index 0a8e475d1..f8ceda6f2 100644 --- a/client/src/app/site/motions/motion-detail/motion-detail.component.html +++ b/client/src/app/site/motions/motion-detail/motion-detail.component.html @@ -73,18 +73,19 @@ -
+

State

- {{motion.state.name}} + {{motion.state}}
- + - {{state}} + {{motion.state}} + + {{state}} - - Reset State + Reset State @@ -99,7 +100,7 @@
- {{state}} + {{state}} diff --git a/client/src/app/site/motions/motion-detail/motion-detail.component.ts b/client/src/app/site/motions/motion-detail/motion-detail.component.ts index cf9e99ab0..300060e03 100644 --- a/client/src/app/site/motions/motion-detail/motion-detail.component.ts +++ b/client/src/app/site/motions/motion-detail/motion-detail.component.ts @@ -133,6 +133,8 @@ export class MotionDetailComponent extends BaseComponent implements OnInit { * http:post the motion to the server. * The AutoUpdate-Service should see a change once it arrives and show it * in the list view automatically + * + * TODO: state is not yet saved. Need a special "put" command */ saveMotion() { const newMotionValues = { ...this.metaInfoForm.value, ...this.contentForm.value }; diff --git a/client/src/app/site/motions/motion-list/motion-list.component.html b/client/src/app/site/motions/motion-list/motion-list.component.html index 9a2868567..2817d6e03 100644 --- a/client/src/app/site/motions/motion-list/motion-list.component.html +++ b/client/src/app/site/motions/motion-list/motion-list.component.html @@ -53,7 +53,7 @@ State -
+
diff --git a/client/src/app/site/motions/motion-list/motion-list.component.ts b/client/src/app/site/motions/motion-list/motion-list.component.ts index 456ea6356..53844a229 100644 --- a/client/src/app/site/motions/motion-list/motion-list.component.ts +++ b/client/src/app/site/motions/motion-list/motion-list.component.ts @@ -121,6 +121,10 @@ export class MotionListComponent extends BaseComponent implements OnInit { } } + isDisplayIcon(state): boolean { + return state.name === 'accepted' || state.name === 'rejected' || state.name === 'not decided'; + } + /** * Download all motions As PDF and DocX *